OpenSearch prevent script_score from running more than once when `query.bool.must` is used along `query.bool.should`

I already asked this on stackoverflow but since this is an urgent question and I didn’t find answers yet in stackoverflow I posted it also in this fourm hoping there’s somebody here that can guide me how to fix this behavior THANK YOU!!

In OpenSearch (v2.18.0) I implemented a custom script_score using Painless scripting language. When I only use query.bool.should it is called once per document and the returned _score is correct

However, when I combine both query.bool.should and query.bool.must in my query, the script_score is called twice or three times per document, and the resulting score is the sum of all the calls. This causes the score to be higher than intended.

Why does this happen? and how can I ensure it is called only once per document when using both should and must in query? Or at least prevent OpenSearch from summing the results of all calls per document and only return the result of one of these calls?

E.g. see below query (which I simplified it here so the example is easy to understand) you’ll see the script_source source is return Integer.parseInt(doc['_id'].value); however because I used both should and must in my query the calculated _score for document 6148 is 18444 (i.e. 6148 * 3) instead of 6148

{
  "from": 0,
  "size": 10,
  "stored_fields": "_none_",
  "docvalue_fields": [
    "_id",
    "_score"
  ],
  "sort": [
    {
      "_score": {
        "order": "asc"
      }
    }
  ],
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "must": [
            {
              "term": { "category_ids": "2" }
            },
            {
              "terms": { "visibility": ["3", "4"] }
            }
          ],
          "should": [
            {
              "ids": {
                "values": [
                  "6148"
                ]
              }
            }
          ],
          "minimum_should_match": 1
        }
      },
      "script_score": {
        "script": {
          "lang": "painless",
          "source": "return Integer.parseInt(doc['_id'].value);"
        }
      }
    }
  }
}

I was able to fix this issue by myself at the end… in case anybody faced the same issue in the future you can look at my answer to my own question in StackOverflow elasticsearch - OpenSearch prevent script_score from running more than once - Stack Overflow

1 Like