# Building a Hybrid Search Query

The following describes how to build and run a Hybrid Search query. A Hybrid search performs both a Lexical Search and a Vector Search. It then assigns a multiplier (weight) to the resulting matches and retrieves the documents with the top highest scores for retrieval.&#x20;

## Running the Hybrid Search Query

If you are using a score function, copy the following code snippet to run the hybrid search query –

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

```python
results = hyperspace_client.search(query, 
                                   size=5, 
                                   function_name='score_function',               
                                   collection_name=collection_name)
```

{% endcode %}
{% endtab %}

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

```javascript
Object results = hyperspace_client.search(query, 
                                          size=5, 
                                         'score_function',               
                                          collection_name);
```

{% endcode %}
{% endtab %}

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

```javascript
let results = hyperspaceClient.search(query, 
                                   size=5, 
                                   'score_function',               
                                  collection_name);
```

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

**Where**–

* <mark style="color:purple;">document</mark> – Specifies the document for similarity search and the multiplier of the return score, as described in Step 3, Defining the Lexical Query Schema.
* <mark style="color:purple;">size</mark> – Specifies the number of results to return.
* <mark style="color:purple;">function\_name</mark> – Specifies the scoring function to be used in the Lexical Search query as described in Step 1, [Creating the Scoring Function](https://docs.hyper-space.io/hyperspace-docs/projects/setting-up/building-and-running-queries/building-a-lexical-classic-search-query).
* <mark style="color:purple;">collection\_name</mark> – Specifies the Collection in which to search.

**Alternatively, if you use DSL syntax, copy the following code snippet**

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

```python
results = hyperspace_client.search_dsl(query,
                                    size=5,
                                    collection_name=collection_name)    
```

{% endcode %}
{% endtab %}

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

```java
let results = hyperspaceClient.searchDSL(query,
                                    size=5,
                                    collection_name=collection_name);
```

{% endcode %}
{% endtab %}

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

```javascript
const results = hyperspaceClient.searchDSL(query,
                                    size=5,
                                    collection_name=collection_name);
```

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

**Where**–

* &#x20;<mark style="color:purple;">query\_string</mark> is your query logic, see example below.

## Creating the Hybrid Search Query

You can create Hybrid Search queries in two methods

1. Linear combination of vector and lexical search (default)
2. Hybrid score function

## Linear Combination of Scores

The query will be a hybrid search query with the score being a linear combination of lexical and vector scores. By default, query components are assigned with weight = 1.0.&#x20;

### Assigning Weights

To change the weights, add a key named "<mark style="color:purple;">knn</mark><mark style="color:purple;">**"**</mark> that includes a key "<mark style="color:purple;">query</mark><mark style="color:purple;">**"**</mark> for the lexical search and designated keys with <mark style="color:purple;">vector field name</mark> ('vector\_field\_1' in the example) for the vector search.

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

```python
hybrid_query_schema = {
                        'params':  {"name": "John", "Age": 30},
                        'knn': [{'field':'query','boost': 0.05}, 
                                {'field':'vector_field_1','boost': 0.6}] 
                      }
```

{% endcode %}
{% endtab %}

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

```javascript
JsonObject params = new JsonObject();
params.add("name", new JsonPrimitive("John"));
params.add("age", new JsonPrimitive(30));
params.add("vector_field", new Gson().toJsonTree(vector).getAsJsonArray());

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

{% endcode %}
{% endtab %}

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

```javascript
const hybridQuerySchema = {
                        'params': {"name": "John", "Age": 30},
                        'knn': [{'field':'query','boost': 0.05}, 
                                {'field':'vector_field_1','boost': 0.6}] 
                      };
```

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

In the above example, the vector\_field\_1 score will be multiplied by 0.6 in the overall score and the lexical query score by 0.05.

{% hint style="info" %}
All fields of type <mark style="color:purple;">dense\_vector</mark> under '<mark style="color:purple;">params</mark>' will be included in the vector search, unless the corresponding '<mark style="color:purple;">boost</mark>' key is set to 0. The weight will be assigned the default value of 1.0.
{% endhint %}

## Building a Hybrid Score Function

You can also preform Hybrid Search using a hybrid score function, by including the KNN search function in the lexical score function.  The score in this case is determined by the score function logic and any weights assigned in the query schema will be ignored.

You can access the KNN score by using the function  [`distance`](https://docs.hyper-space.io/hyperspace-docs/api-documentation/score-function/distance)`('vector_field', min_score)` that returns the KNN distance, and the function

[`knn_filter`](https://docs.hyper-space.io/hyperspace-docs/api-documentation/score-function/knn_filter)`('vector_field', min_score)` that filters according to distance. The default value of min\_score is 1 for both functions.

Both functions can only be used in the last return statement. All other return statements must return 0, None, or False.

**Example 1**

{% code lineNumbers="true" %}

```python
def score_function_hybrid_1( params , doc ):
    score0 = 0.0
    if match('production_companies'):
        return 2 * knn_filter('vector_field_1', min_score=0.3)
    return 0.0
```

{% endcode %}

In the above example, the score function will return 2 if the KNN score of 'vector\_field\_1' is above 0.3, and 0 otherwise.

**Example 2**

<pre class="language-python" data-line-numbers><code class="lang-python"><strong>def score_function_hybrid_2( params , doc ):
</strong><strong>    score0 = 0.0
</strong><strong>    if match('production_companies'):
</strong><strong>        score0 = 2
</strong><strong>        return 2 + 0.5 * distance('vector_field_1', min_score=params['min_score'])
</strong><strong>    return 0.0
</strong></code></pre>

In the above example, the score function will return *2+ 0.5 \* KNN('vector\_field\_1')* if the KNN score of 'vector\_field\_1' is above params\['min\_score'], and 2 otherwise. You need to provide the key "min\_score" under the query "params" key.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.hyper-space.io/hyperspace-docs/flows/setting-up/building-and-running-queries/building-a-hybrid-search-query.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
