Painless scripting: Update a OBJECT FIELD

We have a field called access2 that is currently mapped with object (json).

Now I want to add some data to it based on some logic. I want to check the value of another field access (which is mapped to nested/array) and update the object accordingly.

E.g.
Nested field - "access": ["client": "yes"]

I want the new access2 field to contain data like -

{"production": ["client": "yes"], "nonprod": ["client": "yes"]}

I have tried below:

POST json-index/_update_by_query { "script": { "source": """ ctx._source.access2 = { "production": ctx._source.access, "nonprod": ctx._source.access } """, "lang": "painless" } }

But it is failing with below error:

"lang": "painless",
"caused_by": {
  "type": "illegal_argument_exception",
  "reason": "invalid sequence of tokens near ['{'].",
  "caused_by": {
    "type": "no_viable_alt_exception",
    "reason": null
  }
}

},
"status": 500

hey,

please take the time to properly format your snippets. this is really hard to read and one could not copy & paste it into kibana.

I think the problem is the definition of your map using curly braces. Try

ctx._source.access2 = [ "production": ctx._source.access, "nonprod": ctx._source.access ]

--Alex

2 Likes

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.