# Update Nested object using painless script

**URL:** <https://discuss.elastic.co/t/update-nested-object-using-painless-script/169873>\
**Category:** Elasticsearch\
**Created:** [February 25, 2019, 4:27pm UTC](https://discuss.elastic.co/t/update-nested-object-using-painless-script/169873 "2019-02-25T16:27:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Venkata\_Naresh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/venkata_naresh/32/51355_2.png) [@Venkata\_Naresh](https://discuss.elastic.co/u/Venkata_Naresh)\
**Post date:** [February 25, 2019, 4:27pm UTC](https://discuss.elastic.co/t/update-nested-object-using-painless-script/169873/1 "2019-02-25T16:27:48Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Jack\_Conradson](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jack_conradson/32/47236_2.png) [@Jack\_Conradson](https://discuss.elastic.co/u/Jack_Conradson)\
**Post date:** [March 1, 2019, 4:30pm UTC](https://discuss.elastic.co/t/update-nested-object-using-painless-script/169873/2 "2019-03-01T16:30:57Z")

</div>

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](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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [March 29, 2019, 4:31pm UTC](https://discuss.elastic.co/t/update-nested-object-using-painless-script/169873/3 "2019-03-29T16:31:03Z")

</div>

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