# Building a Hybrid Search Query

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

**To build a hybrid search query –**

Define the Hybrid Search query schema by specifying the following –

```
hybrid_query_schema = {
                         'params': data_point
                      }
```

## Running the Hybrid Search Query

Copy the following code snippet to run the lexical search query –

<pre class="language-python"><code class="lang-python"><strong>results = hyperspace_client.search(hybrid_query_schema, 
</strong><strong>                                   size=5,
</strong>                                   function_name='score_function',               
                                   collection_name=collection_name)
</code></pre>

**Where**–

* <mark style="color:purple;">lexical\_query\_schema</mark> – Specifies the document for similarity search and the multiplier of the return score, as described in Step 3, Defining the Classic 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 Classic 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.

## Assigning Score Weights

By default, all query components, vector and lexical, are assigned with weight = 1.0. To change that, 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.

```python
hybrid_query_schema = {
              'params': data_point,
              'knn': [{'field':'query','boost': 0.05}, 
                      {'field':'vector_field_1','boost': 0.6}] 
}
```

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> in <mark style="color:purple;">data\_point</mark> will be included in the vector search. Unless specified otherwise in a relevant 'boost; key under the '<mark style="color:purple;">knn</mark>' key, the corresponding weight will be assigned the default value of 1.0.
{% endhint %}

## Building a Hybrid Score Function

The KNN score can be included in the lexical score function. To do that, use the function [`distance`](https://docs.hyper-space.io/hyperspace-docs/api-documentation/score-function/distance)('vector\_field\_1') or [knn\_filter](https://docs.hyper-space.io/hyperspace-docs/api-documentation/score-function/knn_filter)('vector\_field\_1', min\_score=params\['min\_score']). The distance function returns the KNN distance, while the knn\_filter function returns 1 if the KNN score is above min\_score and 0 otherwise. Both functions can only be used in the last return statement.

```python
def score_function_hybrid( params , doc ):
    score0 = 1.0
    boost = 1.0
    if match('genres') and match('adult') and not match('title'):
       if doc['rating'] > 7.0:
          boost = 2.0
       if match('genres'):
          score0 += boost * rarity_sum('production_companies')
    
    return score0 * knn_filter('vector_field_1', min_score=0.3)
```

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

```python
def score_function_hybrid( params , doc ):
    score0 = 1.0
    boost = 1.0
    if match('genres') and match('adult') and not match('title'):
       if doc['rating'] > 7.0:
          boost = 2.0
       if match('genres'):
          score0 += boost * rarity_sum('production_companies')
    
    return score0 * knn_filter('vector_field_1', min_score=0.3)
```


---

# 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/~/changes/uCQNjcW7J3OXknfYJaVa/projects/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.
