# Limit script\_score rescoring to the top query matches

**URL:** <https://discuss.elastic.co/t/limit-script-score-rescoring-to-the-top-query-matches/251247>\
**Category:** Elasticsearch\
**Created:** [October 7, 2020, 9:26am UTC](https://discuss.elastic.co/t/limit-script-score-rescoring-to-the-top-query-matches/251247 "2020-10-07T09:26:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![fpug](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/fpug/32/79135_2.png) [@fpug](https://discuss.elastic.co/u/fpug)\
**Post date:** [October 7, 2020, 9:26am UTC](https://discuss.elastic.co/t/limit-script-score-rescoring-to-the-top-query-matches/251247/1 "2020-10-07T09:26:08Z")

</div>

Hi!

I'm trying to score some documents via their a dense\_vector field, via a script\_score query.

The cosine computation is relatively expensive, so I'd like to limit it to the best scoring results of the "query" clause. This would allow me to set an upper bound to the documents that will be rescored.

Right now I can't find any option for that, because the "size" parameter - in my understanding - gets applied to the results of the "script\_score" itself, and not to the results of the "query" clause within it.

Any ideas? Thanks!

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [October 7, 2020, 11:00am UTC](https://discuss.elastic.co/t/limit-script-score-rescoring-to-the-top-query-matches/251247/2 "2020-10-07T11:00:25Z")

</div>

Take a look at [rescoring](https://www.elastic.co/guide/en/elasticsearch/reference/7.9/filter-search-results.html#rescore), that might be what you are after.

---

<div class="post-metadata">

**Author:** ![fpug](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/fpug/32/79135_2.png) [@fpug](https://discuss.elastic.co/u/fpug)\
**Post date:** [October 8, 2020, 8:32am UTC](https://discuss.elastic.co/t/limit-script-score-rescoring-to-the-top-query-matches/251247/3 "2020-10-08T08:32:44Z")

</div>

Thank you for the pointer!

So, as far as I understand it, the way to go would be to have the query structured as follows:

```auto
    {
        "query" : {
            # actual query goes here
        },
        "rescore":{
            "window_size": 100, # maximum number of rescored docs per shard
            "query": {
                "rescore_query": {
                    "script_score": {
                        "query": {
                            "match_all": {} # dummy query to catch all previous results
                        },
                        "script": {
                            # actual rescoring happens here
                        }
                    }
                }
            }
        }
    }

```

So I can not have a hard cap on the maximum documents to be rescored, but I can have a hard cap on the maximum number of documents rescored _per shard_.

Correct? 🙂

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [October 8, 2020, 8:35am UTC](https://discuss.elastic.co/t/limit-script-score-rescoring-to-the-top-query-matches/251247/4 "2020-10-08T08:35:15Z")

</div>

indeed, rescoring happens on the shard level.

---

<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:** [November 5, 2020, 8:35am UTC](https://discuss.elastic.co/t/limit-script-score-rescoring-to-the-top-query-matches/251247/5 "2020-11-05T08:35:24Z")

</div>

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