# Improving runtime for dense embedding rescoring

**URL:** <https://discuss.elastic.co/t/improving-runtime-for-dense-embedding-rescoring/304445>\
**Category:** Elasticsearch\
**Created:** [May 11, 2022, 9:41am UTC](https://discuss.elastic.co/t/improving-runtime-for-dense-embedding-rescoring/304445 "2022-05-11T09:41:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![armin20](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/armin20/32/105536_2.png) [@armin20](https://discuss.elastic.co/u/armin20)\
**Post date:** [May 11, 2022, 9:41am UTC](https://discuss.elastic.co/t/improving-runtime-for-dense-embedding-rescoring/304445/1 "2022-05-11T09:41:03Z")

</div>

Hello,

I'm using [script\_score](https://www.elastic.co/guide/en/elasticsearch/reference/7.17/query-dsl-script-score-query.html#vector-functions) queries for rescoring search results using query and document embeddings. As expected, the runtime is notably longer than my baseline `multi_match` queries and I'm looking into ways to work around it.

The `from` and `size` params don't affect the runtime as the rescoring seems to happen before pagination.

It's stated in the doc: "During vector functions' calculation, all matched documents are linearly scanned. Thus, expect the query time grow linearly with the number of matched documents.For this reason, we recommend to limit the number of matched documents with a `query` parameter."

Is there a query or index parameter/setting to limit the number of matched docs for the query, without changing the query logic (eg. making it more specific)?  
The max number of hits i get is 10k, but in my application i'm happy with 1k, which would also speed up the script\_score.

I've also tried running script\_score under the [rescore](https://www.elastic.co/guide/en/elasticsearch/reference/7.17/filter-search-results.html#multiple-rescores) which is notably faster. However, there in the docs it's stated: " when exposing pagination to your users, you should not change `window_size` as you step through each page (by passing different `from` values) since that can alter the top hits causing results to confusingly shift as the user steps through pages." But what other pagination options are available besides using `from`?

Thanks!

ps. I'm aware of the work on ANN in v8.x

---

<div class="post-metadata">

**Author:** ![mayya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mayya/32/83147_2.png) [@mayya](https://discuss.elastic.co/u/mayya)\
**Post date:** [May 25, 2022, 7:53pm UTC](https://discuss.elastic.co/t/improving-runtime-for-dense-embedding-rescoring/304445/2 "2022-05-25T19:53:07Z")

</div>

> The max number of hits i get is 10k, but in my application i'm happy with 1k, which would also speed up the script\_score.

I am not sure what it means. I assume you are interested in the top scored documents from the whole document collection. To find the top scored documents, Elasticsearch first finds all the matched documents and them uses `script` to score all matched documents to find the top scored documents among them. Are you not interested to calculate scores for all matched documents? [\_search](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-search.html) endpoint has a parameter `terminate_after`, but in this case, you are not guaranteed to find the true top scored documents.

About `rescore`, you can use it, just make sure you don't change your `window_size` as you paginate. So you need to think in advance how many pages of results you want to display and have `window_size` parameter big enough to accommodate all these results. Another alternative is to implement pagination on your application level, just get a big enough results set at once (with a big enough `size` parameter), and then paginate though it in your application. An advantage for it is that a query will be executed only once instead of multiple times: each time for each page.

---

<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:** [June 22, 2022, 7:53pm UTC](https://discuss.elastic.co/t/improving-runtime-for-dense-embedding-rescoring/304445/3 "2022-06-22T19:53:08Z")

</div>

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