# Inconsistent first result when changing \`size\` in script sorted search

**URL:** <https://discuss.elastic.co/t/inconsistent-first-result-when-changing-size-in-script-sorted-search/313282>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [August 30, 2022, 7:22pm UTC](https://discuss.elastic.co/t/inconsistent-first-result-when-changing-size-in-script-sorted-search/313282 "2022-08-30T19:22:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![\_Matthew\_Pavlinsky](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/_matthew_pavlinsky/32/99007_2.png) [@\_Matthew\_Pavlinsky](https://discuss.elastic.co/u/_Matthew_Pavlinsky)\
**Post date:** [August 30, 2022, 7:22pm UTC](https://discuss.elastic.co/t/inconsistent-first-result-when-changing-size-in-script-sorted-search/313282/1 "2022-08-30T19:22:58Z")

</div>

I am trying to use a painless script sort and am experiencing different results when I change the `size` value of the query, I would expect that since sort happens before paging that the same query with a different `size` would always produce the same sort values.

In the context of an ecommerce marketplace the script sort is used to sort the result into chunks of products made my different sellers, then sort inside those chunks by "reputationScore".  
My sort looks like:

```auto
sort: [{
  _script: {
    script: {
      source: `
        def sellerId = doc['sellerId'].value.toString();
        def val = params.sellerMap[sellerId];
        if (val == null) {
          params.sellerMap[sellerId] = 0;
          return 0;
        } else {
          params.sellerMap[sellerId] = val + 1;
          return val + 1;
        }`,
      params: {
        sellerMap: {},
      },
    },
    type: 'number',
    order: 'asc',
  },
}, {
  reputationScore: 'desc',
}]

```

The full query is not much more complicated:

```auto
GET local_variant_search/_search
{
  "sort": [
    {
      "_script": {
        "script": {
          "source": "\n def sellerId = doc['sellerId'].value.toString();\n def val = params.sellerMap[sellerId];\n if (val == null) {\n params.sellerMap[sellerId] = 0;\n return 0;\n } else {\n params.sellerMap[sellerId] = val + 1;\n return val + 1;\n }",
          "params": {
            "sellerMap": {}
          }
        },
        "type": "number",
        "order": "asc"
      }
    },
    {
      "reputationScore": {
        "order": "desc"
      }
    }
  ],
  "size": 1000,
  "from": 0
}

```

Using `"size": 1`, `"size": 10`, and `"size": 1000` all produce different first results on my data set, notably results often have the script result in a score of `1` without another product with the same sellerId being assigned `0`, which stops happening when `size` is the same as the result set.

Is there any insight about how these script sorts are executed that can help me understand this behavior?

---

<div class="post-metadata">

**Author:** ![stu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stu/32/75063_2.png) [@stu](https://discuss.elastic.co/u/stu)\
**Post date:** [August 30, 2022, 8:51pm UTC](https://discuss.elastic.co/t/inconsistent-first-result-when-changing-size-in-script-sorted-search/313282/2 "2022-08-30T20:51:38Z")

</div>

Hi @_Matthew_Pavlinsky,  
Using `params` to pass values between invocations of the script is not supported. `params` should be treated as [read-only](https://www.elastic.co/guide/en/elasticsearch/painless/current/painless-sort-context.html). Sort operates on a per-document basis.

In order to support passing values between invocations of the script during sorting, Elasticsearch would have to move a lot of data between nodes and shards.

Our current implementation resets `params` between segments, which may be what you're running into, but that behavior is not guaranteed.

---

<div class="post-metadata">

**Author:** ![\_Matthew\_Pavlinsky](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/_matthew_pavlinsky/32/99007_2.png) [@\_Matthew\_Pavlinsky](https://discuss.elastic.co/u/_Matthew_Pavlinsky)\
**Post date:** [August 30, 2022, 9:31pm UTC](https://discuss.elastic.co/t/inconsistent-first-result-when-changing-size-in-script-sorted-search/313282/3 "2022-08-30T21:31:55Z")

</div>

Thanks for the quick and informative reply @stu !

All that sounds reasonable, I'll start looking for different approaches to rewrite the script sort. Basically I am trying to make an Elasticsearch sort that behaves like an SQL window function:

```auto
ORDER BY ROW_NUMBER() OVER (PARTITION BY "sellerId")

```

I'll do some more research.

---

<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 27, 2022, 9:32pm UTC](https://discuss.elastic.co/t/inconsistent-first-result-when-changing-size-in-script-sorted-search/313282/4 "2022-09-27T21:32:44Z")

</div>

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