Elasticsearch 执行脚本失败

Elasticsearch failed to execute script

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": {
      ...
    }
  }
}