# "Too many dynamic script compilations" When using int literals

**URL:** <https://discuss.elastic.co/t/too-many-dynamic-script-compilations-when-using-int-literals/124835>\
**Category:** Elasticsearch\
**Created:** [March 20, 2018, 5:58pm UTC](https://discuss.elastic.co/t/too-many-dynamic-script-compilations-when-using-int-literals/124835 "2018-03-20T17:58:43Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![neoform](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/neoform/32/680_2.png) [@neoform](https://discuss.elastic.co/u/neoform)\
**Post date:** [March 20, 2018, 5:58pm UTC](https://discuss.elastic.co/t/too-many-dynamic-script-compilations-when-using-int-literals/124835/1 "2018-03-20T17:58:43Z")

</div>

> Too many dynamic script compilations within, max: [75/5m]; please use indexed, or scripts with parameters instead; this limit can be changed by the [script.max\_compilations\_rate]

The script I'm using has hardcoded integer literals, yet the documentation [How to write scripts | Elasticsearch Guide [master] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/master/modules-scripting-using.html) refers to such literals as being "variables".

Is there a reason why a hardcoded number needs to be passed as a param and blocks the script from being cached?

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [March 20, 2018, 8:26pm UTC](https://discuss.elastic.co/t/too-many-dynamic-script-compilations-when-using-int-literals/124835/2 "2018-03-20T20:26:55Z")

</div>

If your script is using the exact same value, always, then that should not be causing recompilation. A couple questions for you:

- Can you share your script?
- What parameters, if any, does it have?
- Are you dynamically generating the script?
- What type of script is it (eg filter script, scoring script, script field, etc)?

---

<div class="post-metadata">

**Author:** ![neoform](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/neoform/32/680_2.png) [@neoform](https://discuss.elastic.co/u/neoform)\
**Post date:** [March 21, 2018, 1:06pm UTC](https://discuss.elastic.co/t/too-many-dynamic-script-compilations-when-using-int-literals/124835/3 "2018-03-21T13:06:25Z")

</div>

`_score + (Math.log1p(doc['views'].value + 1) / 100) * (Math.log1p(100000 - doc['rank'].value) / 1000)`

I had to replace the hardcoded number values (they never change), the script is a script\_score and is passed along with the search query.

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [March 21, 2018, 4:21pm UTC](https://discuss.elastic.co/t/too-many-dynamic-script-compilations-when-using-int-literals/124835/4 "2018-03-21T16:21:42Z")

</div>

You shouldn't need to replace those constants if they never changed. Is this a production instance, or something you can play with? I would be interested to see trace logging enabled for the ScriptService so we can see exactly what the system thinks is different between your script invocations (if it is always the same script, it should be cached). You can turn this on dynamically: [https://www.elastic.co/guide/en/elasticsearch/reference/current/logging.html#configuring-logging-levels](https://www.elastic.co/guide/en/elasticsearch/reference/current/logging.html#configuring-logging-levels). I think the setting would be `logger.org.elasticsearch.script`.

---

<div class="post-metadata">

**Author:** ![neoform](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/neoform/32/680_2.png) [@neoform](https://discuss.elastic.co/u/neoform)\
**Post date:** [March 21, 2018, 4:31pm UTC](https://discuss.elastic.co/t/too-many-dynamic-script-compilations-when-using-int-literals/124835/5 "2018-03-21T16:31:24Z")

</div>

This is a new production instance that has been causing these errors, we upgraded from 2.4 to 6.2 and were inundated with errors and had to immediately switch back to the old cluster.

I'll talk to our sysadmins to see what they say about adding more logging.

---

<div class="post-metadata">

**Author:** ![neoform](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/neoform/32/680_2.png) [@neoform](https://discuss.elastic.co/u/neoform)\
**Post date:** [March 21, 2018, 4:52pm UTC](https://discuss.elastic.co/t/too-many-dynamic-script-compilations-when-using-int-literals/124835/6 "2018-03-21T16:52:26Z")

</div>

```
GET /testindex/_search
{
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "filter": [
            {
              "term": {
                "privacy": "public"
              }
            },
            {
              "terms": {
                "status": [
                  "active",
                  "pendingactive"
                ]
              }
            }
          ],
          "must_not": [
            {
              "terms": {
                "user_id": [
                  13,
                  14
                ]
              }
            }
          ]
        }
      },
      "functions": [
        {
          "script_score": {
            "script": "_score + 1 + Math.log1p(doc['a'].value * doc['b'].value) * Math.log1p(doc['c'].value) * Math.log1p(doc['d'].value)"
          }
        }
      ]
    }
  },
  "sort": {
    "_score": "desc",
    "q": "desc"
  }
}

```

Result in logs:

```
[2018-03-21T16:37:24,519][TRACE][o.e.s.ScriptService] [ded6964] compiling script, type: [inline], lang: [painless], options: [{}]
[2018-03-21T16:37:24,519][TRACE][o.e.s.ScriptService] [ded6964] compiling lang: [painless] type: [inline] script: _score + 1 + Math.log1p(doc['a'].value * doc['b'].value) * Math.log1p(doc['c'].value) * Math.log1p(doc['d'].value)
[2018-03-21T16:49:46,767][TRACE][o.e.s.ScriptService] [ded6010] compiling lang: [painless] type: [inline] script: _score + 1 + Math.log1p(doc['a'].value * doc['b'].value) * Math.log1p(doc['c'].value) * Math.log1p(doc['d'].value)
[2018-03-21T16:49:46,767][TRACE][o.e.s.ScriptService] [ded6010] compiling lang: [painless] type: [inline] script: _score + 1 + Math.log1p(doc['a'].value * doc['b'].value) * Math.log1p(doc['c'].value) * Math.log1p(doc['d'].value)

```

Every time I run the query it shows to be recompiling.

---

<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:** [April 18, 2018, 4:52pm UTC](https://discuss.elastic.co/t/too-many-dynamic-script-compilations-when-using-int-literals/124835/7 "2018-04-18T16:52:54Z")

</div>

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