1

我正在按照此处的示例使用 Elasticsearch 中的部分更新来更新一组标签。

以下是我的脚本:

{
  "script": {
    "lang": "painless",
    "inline": "ctx._source.deviceTags.add(params.tags)",
    "params": {
      "tags": "search"
    }
  }
}

请求网址是:

https://aws-es-service-url/devices/device/123/_update

但我收到以下回复:

{
    "error": {
        "root_cause": [
            {
                "type": "remote_transport_exception",
                "reason": "[fBaExM8][x.x.x.x:9300][indices:data/write/update[s]]"
            }
        ],
        "type": "illegal_argument_exception",
        "reason": "failed to execute script",
        "caused_by": {
            "type": "script_exception",
            "reason": "runtime error",
            "script_stack": [
                "ctx._source.deviceTags.add(params.tags)",
                "                                 ^---- HERE"
            ],
            "script": "ctx._source.deviceTags.add(params.tags)",
            "lang": "painless",
            "caused_by": {
                "type": "null_pointer_exception",
                "reason": null
            }
        }
    },
    "status": 400
}

知道我做错了什么吗?

4

1 回答 1

2

由于您的deviceTags数组最初为空,因此您有两种方法可以解决此问题

A. 用于upsert确保deviceTags最初添加到您的文档中

{
  "script": {
    "lang": "painless",
    "inline": "ctx._source.deviceTags.add(params.tags)",
    "params": {
      "tags": "search"
    }
  },
  "upsert": {
    "deviceTags": ["search"]
  }
}

B. 保护您的代码免受 NPE

{
  "script": {
    "lang": "painless",
    "inline": "(ctx._source.deviceTags = ctx._source.deviceTags ?: []).add(params.tags)",
    "params": {
      "tags": "search"
    }
  }
}
于 2018-01-03T06:26:29.807 回答