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

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 :

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

The source code looks like this :

{
  "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 ?

can you share this query?

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".

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