Update Nested object using painless script

Hi,

I have an ES nested object in the below format

"campaigns": [
  {
    "campaignId": 110,
    "clickedCount": 1,
    "openedCount": 2
  },
  {
    "campaignId": 1322,
    "clickedCount": 1,
    "openedCount": 0
  }
]

Now I want to iterate each nested object and create a new inline scripted field in the nested object based on the openedCount field in the object. if openedCount > 1 then newopenedCount = 1 else newopenedCount = 0 and the updated nested field should be in below format.

"campaigns": [
  {
    "campaignId": 110,
    "clickedCount": 1,
    "openedCount": 2,
    "newopenedCount" : 1
  },
  {
    "campaignId": 1322,
    "clickedCount": 1,
    "openedCount": 0,
    "newopenedCount" : 0
  }
]

I tried with painless scripted field, but I'm unable to do it. Please help me with this.

It sounds like what you want to use is actually an update script (https://www.elastic.co/guide/en/elasticsearch/painless/master/painless-update-by-query-context.html). To access nested objects, you'll have to use ctx['_source'] in this case.

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