# Elastic Search update api - updating complex data including array objects and parts of document

**URL:** https://discuss.elastic.co/t/elastic-search-update-api-updating-complex-data-including-array-objects-and-parts-of-document/272644
**Category:** Elasticsearch
**Created:** [May 11, 2021, 8:02am UTC](https://discuss.elastic.co/t/elastic-search-update-api-updating-complex-data-including-array-objects-and-parts-of-document/272644 "2021-05-11T08:02:20Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![ab.umarjikar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ab.umarjikar/32/88572_2.png) [@ab.umarjikar](https://discuss.elastic.co/u/ab.umarjikar)
#### Post date: [May 11, 2021, 8:02am UTC](https://discuss.elastic.co/t/elastic-search-update-api-updating-complex-data-including-array-objects-and-parts-of-document/272644/1 "2021-05-11T08:02:20Z")

</div>

````auto
I have complex structure of data coming in as bellow:

{
    ​​​​​
    "id": 1,
    "other_details": [{
            ​​​​​
            "address_details": {
                ​​​​​
                "city": "pune",
                "country": "India",
                "state": "Maharashtra"
            }
            ​​​​​,
            "profession_details": [{
                    ​​​​​
                    "pfid": 1,
                    "post": "Trainee",
                    "start": "2010",
                    "end": "2011"
                }
                ​​​​​, {
                    ​​​​​
                    "pfid": 2,
                    "post": "Software Engineer",
                    "start": "2011",
                    "end": "2014"
                }
                ​​​​​
            ],
            "education": "BE"
        }
        ​​​​​
    ],
    "created": "2010",
    ....(some other fields)
}
​​​​```

Here..
I have an use case to update the elements of an array.
Below are the 3 different update events for the same id["id":1]

1) json 1
{
    ​​​​​
    "id": "1",
    "other_details": [{
            ​​​​​
            "address_details": {
                ​​​​​
                "city": "Pune",
                "country": "India",
                "state": "Maharashtra"
            }
            ​​​​​
        }
        ​​​​​
    ],
    "created": "2012"
}
​​​​​
2) json 2
{
    ​​​​​
    "id": "1",
    "other_details": [{
            ​​​​​
            "profession_details": [{
                    ​​​​​
                    "pfid": 1,
                    "post": "Trainee",
                    "start": "2010"
                }
                ​​​​​
            ],
            "education": "BE"
        }
        ​​​​​
    ]
}
​​​​​
3) json 3
{
    ​​​​​
    "id": "1",
    "other_details": [{
            ​​​​​
            "profession_details": [{
                    ​​​​​
                    "pfid": 2,
                    "post": "Software Engineer",
                    "start": "2011",
                    "end": "2014"
                }
                ​​​​​, {
                    ​​​​​
                    "pfid": 1,
                    "end": "2011"
                }
                ​​​​​
            ]
        }
        ​​​​​
    ]
}

The output which we are expecting is as below -

{
    ​​​​​
    "id": "1",
    "other_details": [{
            ​​​​​
            "address_details": {
                ​​​​​
                "city": "Pune",
                "country": "India",
                "state": "Maharashtra"
            }
            ​​​​​,
            "profession_details": [{
                    ​​​​​
                    "pfid": 1,
                    "post": "Trainee",
                    "start": "2010",
                    "end": "2011"
                }
                ​​​​​, {
                    ​​​​​
                    "pfid": 2,
                    "post": "Software Engineer",
                    "start": "2011",
                    "end": "2014"
                }
                ​​​​​
            ],
            "education": "BE"
        }
        ​​​​​
    ],
    "created": "2012"
}
​​​​
Basically whenever there is an update in the array elements(professional_details - in this example), that has to be updated in the source document.

Kindly help me out !
​
````

---

<div class="post-metadata">

### Author: ![sagarpatel](https://avatars.discourse-cdn.com/v4/letter/s/ebca7d/32.png) [@sagarpatel](https://discuss.elastic.co/u/sagarpatel)
#### Post date: [May 11, 2021, 9:20am UTC](https://discuss.elastic.co/t/elastic-search-update-api-updating-complex-data-including-array-objects-and-parts-of-document/272644/2 "2021-05-11T09:20:40Z")

</div>

If i understand your question correcttly you want to update array inside the existing document.

There are 2 option:

1. you can send entire new json/document (with updated array) to index and it will replace exsiting document.
2. you can use script with upsert to updating document. Please check official document for [upsert with script](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html#scripted_upsert).

Below are some other link which will help you to resolved this:

> <https://stackoverflow.com/questions/32769759/elasticsearch-update-existing-document-by-inserting-elements-to-its-array-field>

> [@Update existing values within a list or array in Elastic Search](https://discuss.elastic.co/t/update-existing-values-within-a-list-or-array-in-elastic-search/18279):
>
> I have a requirement where I need to update(not append) existing values within a list or array in Elastic Search. Is this feature supported in elastic search? For eg: I have a field called jobs as part of my document "jobs": [{ "status": "InProgress", "runId": 1, "start\_date": 2101112, "orderId": "undefined" }, { "status": "InProgress", "runId": 2, "start\_date": 2101112, "orderId": "undefined" },], and I am required to update the orderId for each job run to different values…

---

<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: [June 8, 2021, 9:20am UTC](https://discuss.elastic.co/t/elastic-search-update-api-updating-complex-data-including-array-objects-and-parts-of-document/272644/3 "2021-06-08T09:20:56Z")

</div>

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