# Building a Vector Search Query

The following describes how to build and run a Vector Search query. This type of search matches the similarity of documents in a Collection to a document that you provide by measuring the proximity (or similarity) between vectors rather than relying on traditional keyword matching or exact matches. This Vector search assigns a numerical score to each document according to the type of vector scoring method that you specified in the metric field, such as Hamming. This provides the identifiers of the documents with the top highest scores for retrieval. A multiplier (weight) option can be assigned to the score values.

{% hint style="info" %}
Note – While uploading data into a Collection, '**dense\_vector**' value assigned to the '**type**' attribute signifies that the data is suitable for a Vector Search. Therefore, only data that has been uploaded in this manner will be matched in a Vector search, as described in [Creating a Database Schema Configuration File](https://docs.hyper-space.io/hyperspace-docs/projects/setting-up/creating-a-database-schema-configuration-file).
{% endhint %}

## Defining the Vector Query Schema

Define the Vector Search query schema by specifying the following –

{% tabs %}
{% tab title="Python" %}
{% code lineNumbers="true" %}

```python
vector_query_schema = {paramsument,
                         'knn': {
                         'vector_field_name_1': {'boost': 0.5},
                         'vector_field_name_2': {'boost': 0.5}      
                         }                   
                       }
```

{% endcode %}
{% endtab %}

{% tab title="Java" %}
{% code lineNumbers="true" %}

```java
JsonObject params = new JsonObject();
params.add("vector_field_name_1", new Gson().toJsonTree(vector1).getAsJsonArray());
params.add("vector_field_name_2", new Gson().toJsonTree(vector).getAsJsonArray());

JsonObject knn_vector = new JsonObject();
knn_vector.add("boost", new JsonPrimitive(0.5));
            
JsonObject knn_query = new JsonObject();
knn_query.add("boost", new JsonPrimitive(0.5));
            
JsonObject knn = new JsonObject();
knn.add("query", knn_query);
knn.add("vector", knn_vector);
            
JsonObject hybrid_search = new JsonObject();
vector_search.add("params", params);
vector_search.add("knn", knn);
```

{% endcode %}
{% endtab %}

{% tab title="JavaScript" %}
{% code lineNumbers="true" %}

```javascript
let vectorQuerySchema= {params,
                         'knn': {
                                'vector_field_name_1': {'boost': 0.5},
                                'vector_field_name_2': {'boost': 0.5}      
                          }                   
                         };
```

{% endcode %}
{% endtab %}
{% endtabs %}

**Where** –

* '<mark style="color:purple;">document</mark>'– specifies the document for which the query is searching for a match.
* '<mark style="color:purple;">vector\_field\_name\_1</mark>'– the name of the first vector field to be given a weight
* '<mark style="color:purple;">vector\_field\_name\_2</mark>'– the name of the second vector field to be given a weight
* <mark style="color:purple;">'boost'</mark> - the key states the value of the given weight per score type, which is 0.5 in the example. Default value is 1.0.

In the above example, the query will perform two KNN calculations and return the weighted sum of the score,&#x20;

*score = 0.5 \* score(*&#x76;ector\_field\_name\_&#x31;*) +  0.5 \* score(*&#x76;ector\_field\_name\_&#x32;*)*

Using multiple vectors in the same query requires all of the included vectors to be indexed in the same manner, for example HNSW for all fields in the [database schema configuration file.](https://docs.hyper-space.io/hyperspace-docs/projects/setting-up/creating-a-database-schema-configuration-file)

{% hint style="info" %}
Multi vector query can be integrated with lexical search to create a multi hybrid query.
{% endhint %}

## Running the Vector Query

Copy the following code snippet to run the Vector Search query –

{% tabs %}
{% tab title="Python" %}
{% code lineNumbers="true" %}

```python
results = hyperspace_client.search(vector_query_schema, 
                                   size=5,                 
                                   collection_name=collection_name)
```

{% endcode %}
{% endtab %}

{% tab title="Java" %}
{% code lineNumbers="true" %}

```java
Object results = hyperspaceClient.search(vector_query_schema, 
                                   size=5,                 
                                   collection_name=collection_name);
```

{% endcode %}
{% endtab %}

{% tab title="JavaScript" %}
{% code lineNumbers="true" %}

```javascript
const results = hyperspaceClient.search(vector_query_schema, 
                                   size=5,                 
                                   collection_name=collection_name);
```

{% endcode %}
{% endtab %}
{% endtabs %}

**Where –**

* <mark style="color:purple;">vector\_query\_schema</mark> – Specifies the document for similarity search and the multiplier of the return score, as described above, defining the Vector Query Schema.
* <mark style="color:purple;">size</mark> – Specifies the quantity of results to retrieve.
* <mark style="color:purple;">collection\_name</mark> – Specifies the Collection in which to search.
