使用数组方法 unique() 更新脚本导致错误

hom*_*boy 2 elasticsearch

我需要用另一个数组更新数组值,然后删除交集,所以我发现这个方法“unique()”适用于elasticsearch脚本,但它导致错误:

{
  "error": {
    "root_cause": [
      {
        "type": "remote_transport_exception",
        "reason": "[probook][127.0.0.1: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.arrFieldName = ctx._source.arrFieldName.unique();",
        "                                                     ^---- HERE"
      ],
      "script": "ctx._source.arrFieldName.addAll([111, 222, 333]);ctx._source.arrFieldName = ctx._source.arrFieldName.unique();ctx._source.arrFieldNameCount = ctx._source.arrFieldName.length",
      "lang": "painless",
      "caused_by": {
        "type": "illegal_argument_exception",
        "reason": "dynamic method [java.util.ArrayList, unique/0] not found"
      }
    }
  },
  "status": 400
}
Run Code Online (Sandbox Code Playgroud)

Alw*_*nny 5

您应该这样做,distinct()而不是unique()使用内联无痛脚本,

{
  "script" : {
      "inline": "ctx._source.arrFieldName=
                ctx._source.arrFieldName.stream().distinct().sorted()
                                      .collect(Collectors.toList())"
 }
}
Run Code Online (Sandbox Code Playgroud)