日志报错:
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] setting]
出现这个问题是在ES的查询语句里面使用了script脚本,而且当前查询的次数超过了阈值。设置此阈值是ES的自我保护机制,script脚本会更消耗内存,如果无限制的大量使用,会导致ES内存溢出。
解决方法:
方法一:
修改ES配置,将限制放大。
PUT _cluster/settings
{
"transient" : {
"script.max_compilations_rate" : "100/1m"
}
}
此方法可暂时解决问题,但是对ES的风险也在累积。
方法二:
示例如下:
GET /_search
{
"query": {
"function_score": {
"query": {
"match": { "message": "elasticsearch" }
},
"script_score" : {
"script" : {
"params": {
"a": 5,
"b": 1.2
},
"source": "params.a / Math.pow(params.b, doc['likes'].value)"
}
}
}
}
}
script脚本使用参数的方式。一般script脚本会动态拼接一些参数,因为参数值不同,ES会认为是一个新的脚本,重新编译执行。现在ES支持将拼接的参数形式化,即形参,然后构造ScriptBuilder的时候将参数通过map的方式传入。