# How to prevent "Too many dynamic script compilations within" error with search templates?

**URL:** <https://discuss.elastic.co/t/how-to-prevent-too-many-dynamic-script-compilations-within-error-with-search-templates/325482>\
**Category:** Elasticsearch\
**Tags:** painless, runtime-fields\
**Created:** [February 14, 2023, 2:14pm UTC](https://discuss.elastic.co/t/how-to-prevent-too-many-dynamic-script-compilations-within-error-with-search-templates/325482 "2023-02-14T14:14:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ricou13](https://avatars.discourse-cdn.com/v4/letter/r/82dd89/32.png) [@Ricou13](https://discuss.elastic.co/u/Ricou13)\
**Post date:** [February 14, 2023, 2:14pm UTC](https://discuss.elastic.co/t/how-to-prevent-too-many-dynamic-script-compilations-within-error-with-search-templates/325482/1 "2023-02-14T14:14:13Z")

</div>

I use a search template with "mustache" language to build dynamic queries according to different parameters.  
When I often modify the values ​​of the parameters of this request, I get this error message :  
`[script] Too many dynamic script compilations within, max: [150/5m];`

I think that each time the values ​​of the parameters change, the script is recompiled but if the values ​​are identical then elasticsearch uses a cache so as not to recompile the script.

In our case, the cache cannot be used because at each request the values ​​are always different (local timestamp, variable distance, random seed generated by a client...)

To prevent this error, I change the cluster settings to increase the `max_compilations_rate` value at the cost of higher server load.

Is there a way to limit recompilation ?

My "big" script computes score according to many parameters and uses Elasticsearch 8.2.

The structure of the script is as follows :

```auto
{
  "script": {
    "lang": "mustache",
    "source": "...",
    "params": { ... }
  }
}

```

The source code looks like this :

```auto
{
  "runtime_mappings": {
    "is_opened": {
      "type": "long",
      "script": {
        "source": " ... "
        }
     }
    {{#user_location}}
    ,"distance": {
      "type": "long",
      "script": {
        "source": " ... "
        }
    }
    {{/user_location}}     
  },

  "query": {
    "script_score": {
        "query": { ... }
        },
        "script": {
            "source": " ... "
        }
    }
  },

  "fields": [
    "is_opened"
    {{#user_location}},"distance"{{/user_location}}
  ],
  ...
}

```

I use mustache variables (with double brackets) everywhere in the script :

- in the computed fields ("is\_opened", "distance")
- in query and filters
- in script score

Is there a way to "optimize" internal scripts (computed fields and score script) so as not to restart compilation each time the values for the parameters change ?

---

<div class="post-metadata">

**Author:** ![canelia](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/canelia/32/141857_2.png) [@canelia](https://discuss.elastic.co/u/canelia)\
**Post date:** [February 16, 2023, 2:39am UTC](https://discuss.elastic.co/t/how-to-prevent-too-many-dynamic-script-compilations-within-error-with-search-templates/325482/2 "2023-02-16T02:39:14Z")

</div>

> [@Ricou13](#):
>
> ```auto
> "query": {
> "script_score": {
> "query": { ... }
> },
> "script": {
> "source": " ... "
> }
> }
> }
> 
> ```

can you share this query?

---

<div class="post-metadata">

**Author:** ![Ricou13](https://avatars.discourse-cdn.com/v4/letter/r/82dd89/32.png) [@Ricou13](https://discuss.elastic.co/u/Ricou13)\
**Post date:** [February 16, 2023, 8:40am UTC](https://discuss.elastic.co/t/how-to-prevent-too-many-dynamic-script-compilations-within-error-with-search-templates/325482/3 "2023-02-16T08:40:07Z")

</div>

I found a solution.

To avoid compilations, I need to use "params" inside the embedded runtime fields scripts and inside the query score script.

I had indeed used the parameters for the main script written in "mustache" but I had not done so for the embedded scripts written in "painless".

---

<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 16, 2023, 8:40am UTC](https://discuss.elastic.co/t/how-to-prevent-too-many-dynamic-script-compilations-within-error-with-search-templates/325482/4 "2023-03-16T08:40:50Z")

</div>

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