elasticsearch 句柄如何在脚本化更新查询中返回?



我找不到描述 return 关键字的相关文档。这是在哪里记录的?

我正在运行以下查询

POST /myindex/mytype/FwOaGmQBdhLB1nuQhK1Q/_update
{
"script": {
"source": """
if (ctx._source.owner._id.equals(params.signedInUserId)){
for (int i = 0; i < ctx._source.managers.length; i++) {
if (ctx._source.managers[i].email.equals(params.managerEmail)) {
ctx._source.managers.remove(i);
return;
}
}
}
ctx.op = 'noop';
""",
"lang": "painless",
"params": {
"signedInUserId": "auth0|5a78c1ccebf64a46ecdd0d9c",
"managerEmail": "d@d.com"
}
},
"_source": true
}

但我收到错误

"type": "illegal_argument_exception",
"reason": "failed to execute script",
"caused_by": {
"type": "script_exception",
"reason": "compile error",
"script_stack": [
"... ve(i);n            return;n          }n        }n  ...",
"                             ^---- HERE"
],
"script": <the script here>,
"lang": "painless",
"caused_by": {
"type": "illegal_argument_exception",
"reason": "invalid sequence of tokens near [';'].",
"caused_by": {
"type": "no_viable_alt_exception",
"reason": null
}
}

如果我删除 return 关键字,则脚本会运行,但我按预期得到错误的行为。我可以通过使用布尔值来跟踪电子邮件删除来更正行为,但为什么我不能提前返回?

很难说,您可以通过将 lambda 比较器传递给retainAllremoveIf来避免 null/void 返回

ctx._source.managers.removeIf(m -> m.email.equals(params.managerEmail))

Lambda 表达式和方法引用的工作方式与 Java 相同。

最新更新