# What's the preferred way to update part of a nested document

**URL:** https://discuss.elastic.co/t/whats-the-preferred-way-to-update-part-of-a-nested-document/243705
**Category:** Elasticsearch
**Created:** [August 4, 2020, 12:36pm UTC](https://discuss.elastic.co/t/whats-the-preferred-way-to-update-part-of-a-nested-document/243705 "2020-08-04T12:36:26Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![viebel](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/viebel/32/73304_2.png) [@viebel](https://discuss.elastic.co/u/viebel)
#### Post date: [August 4, 2020, 12:36pm UTC](https://discuss.elastic.co/t/whats-the-preferred-way-to-update-part-of-a-nested-document/243705/1 "2020-08-04T12:36:26Z")

</div>

My document has a nested structure like this:

```
PUT /customer/_doc/1
{
  "name": "hello",
  "body": {
    "module": "aa",
    "config": "bb"
  }
}

```

Now, I'd like to update `body` field to be:

```
{
    "module": "cc",
    "args": "dd"
}

```

Pay attention I need to:

1. update the value associated to `module`
2. remove the `config` field
3. add an `args` field

I tried to use update API

```
POST /customer/_update/1
{
  "doc": {
    "body": {
        "module": "cc",
        "args": "dd"
    }
  }
}

```

It almost works, but the problem is that the `config` field is not removed

`GET /customer/_doc/1`

```
{
 ...
  "_source" : {
    "name" : "hello",
    "body" : {
      "module" : "cc",
      "config" : "bb",
      "args" : "dd"
    }
  }

```

}

Is the exact merging algorithm used by update API documented somewhere?

Is there a way to achieve this without writing a script?  
I don't want to get the document, do the merge and ind it again ( I prefer to use the update API to avoid roundtrips and benefit from auto retry mechanism)

---

<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: [September 1, 2020, 12:36pm UTC](https://discuss.elastic.co/t/whats-the-preferred-way-to-update-part-of-a-nested-document/243705/2 "2020-09-01T12:36:29Z")

</div>

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