# Rescoring the output of a knn-query hybrid retrieval

**URL:** https://discuss.elastic.co/t/rescoring-the-output-of-a-knn-query-hybrid-retrieval/336008
**Category:** Elasticsearch
**Tags:** vector-search
**Created:** [June 14, 2023, 6:31pm UTC](https://discuss.elastic.co/t/rescoring-the-output-of-a-knn-query-hybrid-retrieval/336008 "2023-06-14T18:31:40Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![rajivhs](https://avatars.discourse-cdn.com/v4/letter/r/fbc32d/32.png) [@rajivhs](https://discuss.elastic.co/u/rajivhs)
#### Post date: [June 14, 2023, 6:31pm UTC](https://discuss.elastic.co/t/rescoring-the-output-of-a-knn-query-hybrid-retrieval/336008/1 "2023-06-14T18:31:40Z")

</div>

[According to the kNN documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/knn-search.html#_combine_approximate_knn_with_other_features):

> You can perform hybrid retrieval by providing both the knn option and a query. This search finds the global top k = 5 vector matches, combines them with the matches from the match query, and finally returns the 10 top-scoring results. The knn and query matches are combined through a disjunction, as if you took a boolean or between them... The score of each hit is the sum of the knn and query scores.

[And according to the rescore documentation](https://www.elastic.co/guide/en/elasticsearch/reference/7.17/filter-search-results.html#rescore):

> The query rescorer executes a second query only on the Top-K results returned by the query and post\_filter phases

What is the expected behavior if we include `knn`, `query` and `rescore` all at the same time? Will the rescore take as input the top `window_size` results from the `query` alone? Or will it take as input the top results from the query-knn hybrid?

Our desired behavior is to rescore the top 400 results from the hybrid-output. If simply specifying `knn`, `query` and `rescore` does not do this, is there some other way to accomplish this?

---

<div class="post-metadata">

### Author: ![BenTrent](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bentrent/32/33915_2.png) [@BenTrent](https://discuss.elastic.co/u/BenTrent)
#### Post date: [June 14, 2023, 6:58pm UTC](https://discuss.elastic.co/t/rescoring-the-output-of-a-knn-query-hybrid-retrieval/336008/2 "2023-06-14T18:58:07Z")

</div>

Hey @rajivhs ,

Here are the steps that occur if using `rescore` in conjunction with `knn` and a `query`.

- First, the `K` nearest neighbors are found. This is a global `K` across all shards.
- Those document scores are combined with the `query` and the query is executed against all the shards
- Per shard, with the combined score, `rescore` is called on the TOP window\_size documents.

This has the following consequences:

- `k` is NOT dynamically increased to match `window_size`.
- `window_size` is PER shard, and `k` is a global top set of values. Meaning, it could be that a given shard didn't have any neighbors that were within the global top `k`, and do not contribute to the document score on that shard.
- Rescore will run on the results of `query` and `knn`. But it may be that the top scoring documents from `query` dominate the `knn` scores. Thus you don't really see any `knn` score contribution in the top documents.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [July 12, 2023, 6:59pm UTC](https://discuss.elastic.co/t/rescoring-the-output-of-a-knn-query-hybrid-retrieval/336008/3 "2023-07-12T18:59:03Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
