【问题标题】:How does elasticsearch handle returns inside a scripted update query?elasticsearch 如何处理脚本更新查询中的返回?
【发布时间】:2018-06-26 12:35:35
【问题描述】:

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

【问题讨论】:

标签: elasticsearch elasticsearch-painless


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多