首页 > 解决方案 > 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% 出现在日志中。

标签: elasticsearchelasticsearch-painless

解决方案


这样的空指针很难让人理解,但我的直觉是它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": {
      ...
    }
  }
}

推荐阅读