# Script score query does not pass correct \_score to sort scripts when using field collapse

**URL:** https://discuss.elastic.co/t/script-score-query-does-not-pass-correct-score-to-sort-scripts-when-using-field-collapse/364980
**Category:** Elastic Search
**Tags:** painless
**Created:** [August 15, 2024, 6:40pm UTC](https://discuss.elastic.co/t/script-score-query-does-not-pass-correct-score-to-sort-scripts-when-using-field-collapse/364980 "2024-08-15T18:40:20Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![rschoenbeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rschoenbeck/32/136804_2.png) [@rschoenbeck](https://discuss.elastic.co/u/rschoenbeck)
#### Post date: [August 15, 2024, 6:40pm UTC](https://discuss.elastic.co/t/script-score-query-does-not-pass-correct-score-to-sort-scripts-when-using-field-collapse/364980/1 "2024-08-15T18:40:20Z")

</div>

When using Elasticsearch 8.7.1, if you add a `collapse` parameter to a `script_score` query, it seems like references to the `_score` field in subsequent `sort` scripts refer not to the `script_score`'s \_score value, but some value that is perhaps related to the `query` portion of the results.

This does not happen if you do not use a field collapse. There, the script score is passed as `_score` to subsequent scripts used in the `sort` parameter.

I couldn't find other reports or documentation of this issue and am wondering if it's a bug.

Here is an example below -- unfortunately not reproducible since it's on our data, but hopefully it's simple enough to illustrate on a variety of datasets.

Expected behavior: the value from `return _score` in the `sort` script matches the score in the `script_score` script regardless of whether the `collapse` parameter is present when `track_scores` is set to `true`.

Actual behavior: The value from `return _score` in the `sort` script is equivalent to the value of the `script_score` score value when no `collapse` is present (as expected), but differs when a `collapse` is present (unexpected). When the `collapse` is used, the score seems perhaps based on the `match_all` query instead (all `_score` values in the sort script are 1).

EDIT: it seems like it might be equivalent to the problem mentioned in this GitHub issue: [script sort with "collapse" failed to get\_ score variable · Issue #87772 · elastic/elasticsearch · GitHub](https://github.com/elastic/elasticsearch/issues/87772)

A possible fix is mentioned. Does anyone know the version with the fix? I tested the same query out on ver 8.7.1 and 8.11.1 and noticed that the latter version had the behavior that I expected (passed along the right `_score` to the sort scripts).

```auto
POST fundraising-v20/_search
{
  "_source": {
    "includes": [
      "loanId",
      "isActionable",
      "isoCode",
      "partnerId"
    ]
  },
  "collapse": {
    "field": "isoCode"
  },
  "query": {
    "script_score": {
      "query":{
        "match_all": {}
      },
      "script": {
        "params": {},
        "lang": "painless",
        "source": """
          long x = 0;
          if (!doc['partnerId'].empty) {
              x = doc['partnerId'].value;
          }
          x;
        """
      }
    }
  },
  "sort": [
        {
      "_script": {
        "order": "desc",
        "script": {
          "params": {},
          "lang": "painless",
          "source": "return _score;"
        },
        "type": "number"
      }
    }
  ],
  "track_scores": true,
  "track_total_hits": true,
  "version": true
}

```

---

<div class="post-metadata">

### Author: ![Samiul\_Monir](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/samiul_monir/32/121815_2.png) [@Samiul\_Monir](https://discuss.elastic.co/u/Samiul_Monir)
#### Post date: [August 15, 2024, 10:20pm UTC](https://discuss.elastic.co/t/script-score-query-does-not-pass-correct-score-to-sort-scripts-when-using-field-collapse/364980/2 "2024-08-15T22:20:09Z")

</div>

Hi @rschoenbeck , I played around a bit, and it looks like this has been fixed in `8.15`.

I hope that helps.

---

<div class="post-metadata">

### Author: ![rschoenbeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rschoenbeck/32/136804_2.png) [@rschoenbeck](https://discuss.elastic.co/u/rschoenbeck)
#### Post date: [August 15, 2024, 10:47pm UTC](https://discuss.elastic.co/t/script-score-query-does-not-pass-correct-score-to-sort-scripts-when-using-field-collapse/364980/3 "2024-08-15T22:47:08Z")

</div>

I later noticed it was fixed in 8.11.1, which is the ES version used in the Spring Data Elasticsearch release that's ahead of our current one, so I think that's the path we'll take. 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: [September 12, 2024, 10:47pm UTC](https://discuss.elastic.co/t/script-score-query-does-not-pass-correct-score-to-sort-scripts-when-using-field-collapse/364980/4 "2024-09-12T22:47:31Z")

</div>

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