homeboy
homeboy

Reputation: 171

Script update with array method unique() causing error

I need to update array value with another array and then remove intersections, so I found this method 'unique()' for elasticsearch scripts, but it's causing error:

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

Upvotes: 2

Views: 986

Answers (1)

A l w a y s S u n n y
A l w a y s S u n n y

Reputation: 38542

You should do it this way with distinct() instead of unique() for your inline painless script,

{
  "script" : {
      "inline": "ctx._source.arrFieldName=
                ctx._source.arrFieldName.stream().distinct().sorted()
                                      .collect(Collectors.toList())"
 }
}

Upvotes: 5

Related Questions