elasticsearch - Elasticsearch 执行脚本失败
问题描述
弹性搜索 7.7.0 版
这是映射的一部分:
const PROFILE_MAPPING = {
mappings: {
properties: {
_userLocation: {
type: "geo_point"
},
_ignoredBy: {
type: "nested"
}
}
}
};
_ignoredBy 数据示例:
[{
"until" : "2020-12-03T16:20:43.176Z",
"user" : <USER_ID>
}]
这是我正在运行以更新它的脚本:
await client.update({
index,
id: target,
refresh: "wait_for",
body: {
script: {
source:
"ctx._source._ignoredBy.removeIf(item -> item.user ==
params.by.user);ctx._source._ignoredBy.add(params.by)",
params: {
by: {
user: initiator,
until: addSeconds(new Date(), ignoreInterval)
}
}
}
}
});
这是我得到的错误:
{
"error": {
"root_cause": [
{
"type": "illegal_argument_exception",
"reason": "failed to execute script"
}
],
"type": "illegal_argument_exception",
"reason": "failed to execute script",
"caused_by": {
"type": "script_exception",
"reason": "runtime error",
"script_stack": ["item -> item.user == params.by.user);", "^---- HERE"],
"script": "ctx._source._ignoredBy.removeIf(item -> item.user == params.by.user);ctx._source._ignoredBy.add(params.by)",
"lang": "painless",
"position": { "offset": 32, "start": 32, "end": 69 },
"caused_by": { "type": "null_pointer_exception", "reason": null }
}
},
"status": 400
}
奇怪的是,这在 99% 的情况下都有效,但错误出现在日志中并且无法弄清楚原因是什么。传入的参数 100% 出现在日志中。
解决方案
这样的空指针很难让人理解,但我的直觉是它ctx._source._ignoredBy
本身有问题。
本着这种精神,我建议在调用.removeIf
它之前再添加一个检查 - 也许初始化它以防万一null
:
{
"script": {
"source": "if (ctx._source._ignoredBy == null) {ctx._source._ignoredBy = []; } ctx._source._ignoredBy.removeIf(item -> item.user == params.by.user); ctx._source._ignoredBy.add(params.by)",
"params": {
...
}
}
}
推荐阅读
- sql-server - 向现有表添加其他地址类型
- .net - NullReferenceException:对象引用未设置为对象的实例。Microsoft.AspNetCore.Http.Internal.DefaultHttpRequest.get_Query()
- javascript - 模块未找到 npm 脚本的导入功能
- android - 如何使用表情符号库在文本中添加表情符号?
- android - Android VTS 未在 Android-Oreo 8.1 上运行
- reactjs - Jest/Enzyme 单元测试
- android - 没有从数据库中获取行。SQLite、安卓、科特林
- java - java.lang.ExceptionInInitializerError 在 AndroidLauncher.oncreate();
- android - 让我们讨论一下 Oreo 的后台执行限制
- java - 将 Drools 添加到使用 gradle 构建的 java 项目 jar 中