# Update documents with painless - nested and not nested objects with the same name

**URL:** https://discuss.elastic.co/t/update-documents-with-painless-nested-and-not-nested-objects-with-the-same-name/184617
**Category:** Elasticsearch
**Created:** [June 6, 2019, 3:12pm UTC](https://discuss.elastic.co/t/update-documents-with-painless-nested-and-not-nested-objects-with-the-same-name/184617 "2019-06-06T15:12:40Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Martin\_Becker](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/martin_becker/32/22885_2.png) [@Martin\_Becker](https://discuss.elastic.co/u/Martin_Becker)
#### Post date: [June 6, 2019, 3:12pm UTC](https://discuss.elastic.co/t/update-documents-with-painless-nested-and-not-nested-objects-with-the-same-name/184617/1 "2019-06-06T15:12:40Z")

</div>

I messed up my documents and want to clean them up. I want to do this using \_update\_by\_query and painless script.

I run Elastic version 5.6 and my documents hold nested objects which hold tracking info as a nested object like this:

```
"tracking": {
  "triggered": false,
  "member": false
},

```

Due to wrong use of the Java API I ended up with documents holding next to the nested object some additional variables as follows:

```
"tracking": {
  "triggered": false,
  "member": false
},
"tracking.member": true,
"tracking.triggered": true

```

The problem now is that Kibana uses both of these fields. However we filter the data (true or false) we always get an answer because there are both variants. It seems Kibana does not differentiate between the nested and the non-nested variables.

I want to get rid of the variables of the not-nested format. But I do not know how to identify the documents.

Here is my update script which updates the _nested objects_:

```
POST my-tracking_index/_update_by_query?conflicts=proceed
{
  "query": {
    "match": {
      "_id": "17231723681231923"
    }
  },
        "script": {
          "source": """
ctx._source.tracking.member = false;
ctx._source.tracking.triggered = false;
"""
        }
}

```

But I want to remove the _not-nested objects_ (or set their value null). How do I do this?

Thank you.

---

<div class="post-metadata">

### Author: ![Martin\_Becker](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/martin_becker/32/22885_2.png) [@Martin\_Becker](https://discuss.elastic.co/u/Martin_Becker)
#### Post date: [June 7, 2019, 9:43am UTC](https://discuss.elastic.co/t/update-documents-with-painless-nested-and-not-nested-objects-with-the-same-name/184617/2 "2019-06-07T09:43:55Z")

</div>

I found the solution for this problem.

```
// removing the unwanted not-nested fields
if (ctx._source['tracking.member'] != null) {
    ctx._source.remove("tracking.member");
}

// editing the nested fields
ctx._source.tracking.member = true;

```

So basically one needs to put the non-nested fields in brackets .

I still wonder how the update mechanism of the Java API messed it up and will investigate further.

---

<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: [July 5, 2019, 9:43am UTC](https://discuss.elastic.co/t/update-documents-with-painless-nested-and-not-nested-objects-with-the-same-name/184617/3 "2019-07-05T09:43:58Z")

</div>

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