# How exactly elasticsearch update an document when use update?

**URL:** https://discuss.elastic.co/t/how-exactly-elasticsearch-update-an-document-when-use-update/318178
**Category:** Elasticsearch
**Tags:** painless
**Created:** [November 4, 2022, 10:19am UTC](https://discuss.elastic.co/t/how-exactly-elasticsearch-update-an-document-when-use-update/318178 "2022-11-04T10:19:36Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![robocon20x](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/robocon20x/32/99145_2.png) [@robocon20x](https://discuss.elastic.co/u/robocon20x)
#### Post date: [November 4, 2022, 10:19am UTC](https://discuss.elastic.co/t/how-exactly-elasticsearch-update-an-document-when-use-update/318178/1 "2022-11-04T10:19:36Z")

</div>

hi everyone,  
as i read elasticsearch doc that, everytime I update an document, es will create new doc then merge old doc with new doc with new data. but what if my udpate API not change any filed of it,

will it still create new doc then merge to that empty doc?

i use bulk update by script, my script compare my new data and es data, if it have the same, it will not update \_ctx.source data. But it's version and segment still change.

will my script help me to reduce time to write to ES?

Here is my script

```auto
POST _scripts/cdp_upsert_test
{
  "script": {
    "lang": "painless",
    "source": """
if (params.data != null) {
    def valid = true;
    if (params.data.size() == 1) {
        for (entry in params.data.entrySet()) {
            if (entry.getKey() == 'last_updated') {
                valid = false
            }
        }
    }

    if (valid) {
        for (entry in params.data.entrySet()) {
            if (entry.getValue() != null) {
                ctx._source[entry.getKey()] = entry.getValue();
            } else {
                ctx._source.remove(entry.getKey());
            }
        }
    }
}

if (ctx._source.parent_child == null && ctx._source.parent_id == null) {
    ctx._source.parent_child = 'parent';
}
"""
}}

```

and here is my bulk request

```auto
POST _bulk
{"update":{"_id":"33167:0388280194","_index":"v3_customers_33167", "retry_on_conflict":3}} 
{"script":{"id":"cdp_upsert_test","params":{}},"scripted_upsert":true,"upsert":{}}

```

as you see, even when I set my params to null, it still update version

Thanks and best regards

---

<div class="post-metadata">

### Author: ![duyhq](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/duyhq/32/99596_2.png) [@duyhq](https://discuss.elastic.co/u/duyhq)
#### Post date: [November 4, 2022, 10:49am UTC](https://discuss.elastic.co/t/how-exactly-elasticsearch-update-an-document-when-use-update/318178/2 "2022-11-04T10:49:38Z")

</div>

Same here. Anyone can help ?

---

<div class="post-metadata">

### Author: ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)
#### Post date: [November 4, 2022, 10:53am UTC](https://discuss.elastic.co/t/how-exactly-elasticsearch-update-an-document-when-use-update/318178/3 "2022-11-04T10:53:58Z")

</div>

> [@robocon20x](#):
>
> but what if my udpate API not change any filed of it, will it still create new doc then merge to that empty doc?

No. The operation is ignored as there is no change. The response will contain the result as **noop**.

> [@robocon20x](#):
>
> i use bulk update by script, my script compare my new data and es data, if it have the same, it will not update \_ctx.source data. But it's version and segment still change

If the data is really the same and the version changes your script may be wrong. I would leave this checking to elastic.

---

<div class="post-metadata">

### Author: ![robocon20x](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/robocon20x/32/99145_2.png) [@robocon20x](https://discuss.elastic.co/u/robocon20x)
#### Post date: [November 4, 2022, 11:25am UTC](https://discuss.elastic.co/t/how-exactly-elasticsearch-update-an-document-when-use-update/318178/4 "2022-11-04T11:25:52Z")

</div>

thank you @RabBit_BR i have updated my script and bulk query, would you mind check it for me?

---

<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: [December 2, 2022, 11:26am UTC](https://discuss.elastic.co/t/how-exactly-elasticsearch-update-an-document-when-use-update/318178/5 "2022-12-02T11:26:27Z")

</div>

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