elasticsearch如何处理脚本化更新查询中的返回?

时间:2018-06-26 03:13:52

标签: elasticsearch elasticsearch-painless

我找不到描述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关键字,则脚本将运行,但是我得到的错误行为与预期的一样。我可以通过使用布尔值来跟踪电子邮件删除来纠正此行为,但是为什么我不能提早返回?

1 个答案:

答案 0 :(得分:1)

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

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

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