How exactly elasticsearch update an document when use update?

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

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

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

Same here. Anyone can help ?

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

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

2 Likes

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