# Incrementing param value within a script

**URL:** <https://discuss.elastic.co/t/incrementing-param-value-within-a-script/264888>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [February 19, 2021, 4:41pm UTC](https://discuss.elastic.co/t/incrementing-param-value-within-a-script/264888 "2021-02-19T16:41:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marc\_Pereira](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marc_pereira/32/84210_2.png) [@Marc\_Pereira](https://discuss.elastic.co/u/Marc_Pereira)\
**Post date:** [February 19, 2021, 4:41pm UTC](https://discuss.elastic.co/t/incrementing-param-value-within-a-script/264888/1 "2021-02-19T16:41:28Z")

</div>

This is a simplified version of the problem.

```auto
         "should": [
        {
          "script_score": {
            "query": {
              "match_all": {}
            },
           "script": {
            "source": """
                    def score = 0;
                    if(params.ct1 < 3) {
                      score = 20;
                      params.ct1++;
                    }
                    return score;
                  """,
            "params": {
              "ct1": 0
            }
          }
          }
        }
      ],

```

Ideally I'd only get a 20 score 3 times but it's not working consistently - sometimes I get more than 3.  
Is this possible? Has anyone done anything similar?

---

<div class="post-metadata">

**Author:** ![Jack\_Conradson](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jack_conradson/32/47236_2.png) [@Jack\_Conradson](https://discuss.elastic.co/u/Jack_Conradson)\
**Post date:** [February 19, 2021, 5:20pm UTC](https://discuss.elastic.co/t/incrementing-param-value-within-a-script/264888/2 "2021-02-19T17:20:36Z")

</div>

params is intended to be read-only for score scripts as documented [here](https://www.elastic.co/guide/en/elasticsearch/painless/current/painless-score-context.html). With a score script there is no way to reliably pass state between multiple documents, and the behavior seen here is a side effect of Java using references for types in general. The reason that you see more than 3 at times is because the params gets reset for each Lucene segment that the query executes against.

---

<div class="post-metadata">

**Author:** ![Marc\_Pereira](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marc_pereira/32/84210_2.png) [@Marc\_Pereira](https://discuss.elastic.co/u/Marc_Pereira)\
**Post date:** [February 19, 2021, 5:42pm UTC](https://discuss.elastic.co/t/incrementing-param-value-within-a-script/264888/3 "2021-02-19T17:42:27Z")

</div>

Thank you for your quick response and I suspected what you said but wasn't 100% sure. Can you think of or is there another way to accomplish the same task? I'm really trying to bucket results even after they've been scored. A "group by" to the scoring - I'm doing this in rescore which I thought would do the trick. Or maybe there's some other feature I could be using.

---

<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 19, 2021, 5:42pm UTC](https://discuss.elastic.co/t/incrementing-param-value-within-a-script/264888/4 "2021-03-19T17:42:31Z")

</div>

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