elasticsearch 如何防止搜索模板出现“动态脚本编译过多”错误?

zpqajqem  于 2023-02-21  发布在  ElasticSearch
关注(0)|答案(1)|浏览(237)

我使用一个带有“胡子”语言的搜索模板,根据不同的参数构建动态查询。当我经常修改值时 这个请求的参数,我得到这个错误消息:[script] Too many dynamic script compilations within, max: [150/5m];
我认为每一次价值观 的参数更改,脚本将重新编译,但如果 相同,则elasticsearch使用缓存,以免重新编译脚本。
在我们的示例中,无法使用该高速缓存,因为在每次请求时 总是不同的(本地时间戳、可变距离、客户端生成的随机种子......)
为了防止这个错误,我更改了集群设置,以增加max_compilations_rate值,但代价是服务器负载增加。
有没有限制重新编译的方法?
我的“big”脚本根据许多参数计算得分,并使用Elasticsearch 8.2。
脚本的结构如下所示:

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

源代码如下所示:

{
  "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”)
  • 在查询和筛选器中
  • 脚本中乐谱

是否有一种方法可以“优化”内部脚本(计算字段和评分脚本),以便在每次参数值更改时不重新启动编译?

xzlaal3s

xzlaal3s1#

为了避免编译,我需要在嵌入式运行时字段脚本和查询分数脚本中使用“params”。
我确实使用了用“Mustache”编写的主脚本的参数,但我没有对用“painless”编写的嵌入式脚本这样做。
谢谢“瓦尔”给我的提示。

相关问题