# OpenSearch prevent script\_score from running more than once when \`query.bool.must\` is used along \`query.bool.should\`

**URL:** <https://forum.opensearch.org/t/opensearch-prevent-script-score-from-running-more-than-once-when-query-bool-must-is-used-along-query-bool-should/22952>\
**Category:** OpenSearch\
**Created:** [December 29, 2024, 2:17am UTC](https://forum.opensearch.org/t/opensearch-prevent-script-score-from-running-more-than-once-when-query-bool-must-is-used-along-query-bool-should/22952 "2024-12-29T02:17:01Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![amjed](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.opensearch.org/amjed/32/8543_2.png) [@amjed](https://forum.opensearch.org/u/amjed)\
**Post date:** [December 29, 2024, 2:17am UTC](https://forum.opensearch.org/t/opensearch-prevent-script-score-from-running-more-than-once-when-query-bool-must-is-used-along-query-bool-should/22952/1 "2024-12-29T02:17:01Z")

</div>

> 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`

```json
{
  "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);"
        }
      }
    }
  }
}

```

 ![Screenshot 2024-12-29 at 4.00.33 AM](https://us1.discourse-cdn.com/flex019/uploads/mauve_hedgehog/original/2X/1/170bedc2e95b9cfe5df994ae49ad965cc5d7ef23.jpeg)

---

<div class="post-metadata">

**Author:** ![amjed](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.opensearch.org/amjed/32/8543_2.png) [@amjed](https://forum.opensearch.org/u/amjed)\
**Post date:** [December 29, 2024, 8:39pm UTC](https://forum.opensearch.org/t/opensearch-prevent-script-score-from-running-more-than-once-when-query-bool-must-is-used-along-query-bool-should/22952/2 "2024-12-29T20:39:49Z")

</div>

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](https://stackoverflow.com/questions/79314862/opensearch-prevent-script-score-from-running-more-than-once/79316302#79316302)
