如何预防“Too many dynamic script compilations within”错误与搜索模板?

huangapple go评论50阅读模式
英文:

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

问题

我使用一个使用"mustache"语言的搜索模板来根据不同的参数构建动态查询。当我经常修改这个请求的参数值时,我会收到以下错误消息:

[script] Too many dynamic script compilations within, max: [150/5m];

我认为每当参数值更改时,脚本都会重新编译,但如果值相同,Elasticsearch会使用缓存,以免重新编译脚本。

在我们的情况下,无法使用缓存,因为每次请求时,值都是不同的(本地时间戳、变量距离、客户端生成的随机种子等)。

为了防止这个错误,我更改了集群设置,以增加max_compilations_rate的值,代价是增加了服务器负载。

有没有办法限制重新编译?

我的"大"脚本根据许多参数计算分数,并使用Elasticsearch 8.2。

脚本的结构如下:

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

源代码如下:

{
  "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}}
  ],
  ...
}

我在脚本中的各处都使用"mustache"变量(双大括号):

  • 在计算字段中("is_opened","distance")
  • 在查询和过滤器中
  • 在脚本分数中

有没有办法"优化"内部脚本(计算字段和分数脚本),以便每次参数值更改时不重新启动编译?

英文:

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 ?

答案1

得分: 1

为了避免编译错误,在嵌入式运行时字段脚本和查询评分脚本中需要使用 "params"。

的确,我已经在主脚本中使用了 "mustache" 编写的参数,但对于用 "painless" 编写的嵌入式脚本还没有这样做。

感谢 @Val 给我一些提示。

英文:

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

Thanks @Val for giving me a hint.

huangapple
  • 本文由 发表于 2023年2月14日 22:24:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75449208.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定