# Script\_score behavior depends on number of requested documents

**URL:** <https://discuss.elastic.co/t/script-score-behavior-depends-on-number-of-requested-documents/324837>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [February 6, 2023, 10:54pm UTC](https://discuss.elastic.co/t/script-score-behavior-depends-on-number-of-requested-documents/324837 "2023-02-06T22:54:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gkleinereva](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gkleinereva/32/109555_2.png) [@Gkleinereva](https://discuss.elastic.co/u/Gkleinereva)\
**Post date:** [February 6, 2023, 10:54pm UTC](https://discuss.elastic.co/t/script-score-behavior-depends-on-number-of-requested-documents/324837/1 "2023-02-06T22:54:25Z")

</div>

TL;DR - I encountered a problem with `script_score` behavior. `script_score` seems to calculate scores differently depending on the `size` parameter in the query. Can anyone help me understand what's going on here (and whether or not this is a bug)? Suggestions of alternatives that may work better in this case are also appreciated (though I have looked at "Script" queries and "Runtime fields" and don't think these will work).

Suppose that I create the following index

```json
PUT /testindex
{
  "mappings": {
    "properties": {
      "priority": {
        "type": "float"
      }
    }
  }
}

```

and then index some documents

```json
POST /testindex/_doc/1
{
  "priority": 5.5
}
POST /testindex/_doc/2
{
  "priority": 3
}

```

For whatever reason (this example is far more contrived than my actual use case), I now would like to use the following `script_score` query to search for all documents that have a `"priority"` of at least 2.5:

```json
GET /testindex/_search
{
  "size": 5,
  "query": {
    "bool": {
      "minimum_should_match": 1,
      "should": [
        {
          "script_score": {
            "script": {
              "source": "return(doc['priority'].value)"
            },
            "query": {
              "exists": {
                "field": "priority"
              }
            },
            "boost": 2,
            "min_score": 5
          }
        }
      ]
    }
  }
}

```

As expected, the script calculates each document's score, then boosts it, and finally checks to ensure that the resultant score is higher than the `min_score`. In our case, this results in 2 hits being found by the query.

Now, run that same query again, but without retrieving any documents (set `"size"` to 0, as has been done in the following version of the query). In my case, I'm only interested in aggregation results in certain senarios.

```json
GET /testindex/_search 
{
  "size": 0,
  "query": {
    "bool": {
      "minimum_should_match": 1,
      "should": [
        {
          "script_score": {
            "script": {
              "source": "return(doc['priority'].value)"
            },
            "query": {
              "exists": {
                "field": "priority"
              }
            },
            "boost": 2,
            "min_score": 5
          }
        }
      ]
    }
  }
}

```

This query actually finds a different number of hits! It only matches the document with a `"priority"` of 5. It seems to me that there is a sequencing issue in the `script_score` calculation when no hits are being returned in the response.

If you made it through this and still feel like helping me, thanks a ton! 🙂

---

<div class="post-metadata">

**Author:** ![Gkleinereva](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gkleinereva/32/109555_2.png) [@Gkleinereva](https://discuss.elastic.co/u/Gkleinereva)\
**Post date:** [February 13, 2023, 9:03pm UTC](https://discuss.elastic.co/t/script-score-behavior-depends-on-number-of-requested-documents/324837/2 "2023-02-13T21:03:11Z")

</div>

Does anyone have thoughts on this? I can create a github issue instead if that's a better place for this kind of question/reproduction. Thanks!

---

<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:** [March 13, 2023, 9:03pm UTC](https://discuss.elastic.co/t/script-score-behavior-depends-on-number-of-requested-documents/324837/3 "2023-03-13T21:03:41Z")

</div>

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