# Update by query on nested mapping

**URL:** https://discuss.elastic.co/t/update-by-query-on-nested-mapping/243290
**Category:** Elasticsearch
**Created:** [July 30, 2020, 10:38pm UTC](https://discuss.elastic.co/t/update-by-query-on-nested-mapping/243290 "2020-07-30T22:38:04Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![e-rossi](https://avatars.discourse-cdn.com/v4/letter/e/ecb155/32.png) [@e-rossi](https://discuss.elastic.co/u/e-rossi)
#### Post date: [July 30, 2020, 10:38pm UTC](https://discuss.elastic.co/t/update-by-query-on-nested-mapping/243290/1 "2020-07-30T22:38:04Z")

</div>

So, I've successfully created a field and added a value for it in existing documents using the put mapping and update by query APIs with the following

```auto
    NEW_FIELD = {
        'properties': {
            'deleted': {
                'type': 'boolean'
            }
        }
    }

    UPDATE_QUERY = {
        'script': {
            'source': 'ctx._source.deleted = false',
            'lang': 'painless'
        },
        'query': {
            'match_all': {}
        }
    }

```

Now, the same won't work for a field that is inside an existing array, I was able to create the field but the update query doesn’t seem to be correct:

```auto
    NEW_FIELD = {
        'properties': {
            'customer': {
                'properties': {
                    'sensitive': {
                        'type': 'boolean'
                    }
                }
            }
        }
    }

    UPDATE_QUERY = {
        'script': {
            'source': 'ctx._source.customer.sensitive = false',
            'lang': 'painless'
        },
        'query': {
            'match_all': {}
        }
    }

```

---

<div class="post-metadata">

### Author: ![Vinayak\_Sapre](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vinayak_sapre/32/45939_2.png) [@Vinayak\_Sapre](https://discuss.elastic.co/u/Vinayak_Sapre)
#### Post date: [July 30, 2020, 11:14pm UTC](https://discuss.elastic.co/t/update-by-query-on-nested-mapping/243290/2 "2020-07-30T23:14:00Z")

</div>

If it's an array, you will have to specify index to locate specific entry to modify. To modify all entries you will need loop.

---

<div class="post-metadata">

### Author: ![e-rossi](https://avatars.discourse-cdn.com/v4/letter/e/ecb155/32.png) [@e-rossi](https://discuss.elastic.co/u/e-rossi)
#### Post date: [July 31, 2020, 1:03pm UTC](https://discuss.elastic.co/t/update-by-query-on-nested-mapping/243290/3 "2020-07-31T13:03:27Z")

</div>

Like this? `'ctx._source.customer['sensitive'] = false'`

---

<div class="post-metadata">

### Author: ![Vinayak\_Sapre](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vinayak_sapre/32/45939_2.png) [@Vinayak\_Sapre](https://discuss.elastic.co/u/Vinayak_Sapre)
#### Post date: [August 2, 2020, 9:44pm UTC](https://discuss.elastic.co/t/update-by-query-on-nested-mapping/243290/4 "2020-08-02T21:44:50Z")

</div>

No. Something like

`ctx._source.customer[0].sensitive = false`

---

<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: [August 30, 2020, 9:44pm UTC](https://discuss.elastic.co/t/update-by-query-on-nested-mapping/243290/5 "2020-08-30T21:44:57Z")

</div>

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