'Elasticsearch version conflict

I have updated document in the elastic search. after update using I am fetching the same document by using their ID. It is giving me following response:

 {
"_index": "b123456",
"_type": "documents",
"_id": "bltde56dd11ba998bab",
"_version": 3,
"found": true,
"_source": {
    "title": "index.json",
    "url": "/index1",
    "tags": [],
    "created_at": "2018-06-19T05:02:38.174Z",
    "updated_at": "2018-06-19T05:07:57.155Z",
    "version": 1,
    "fields": [{
            "uid": "fname",
            "value": "john"
        },
        {
            "uid": "lname",
            "value": "test"
        }
    ],
    "class": "first"
}


}

After I am using update_by_query to update document I am sending following request to update_by_query:

{
    "script": {
        "source": "ctx._source.title = params.title;ctx._source.url = params.url;ctx._source.created_at = params.created_at;ctx._source.updated_at = params.updated_at;ctx._source.version = params.version;ctx._source.fields = params.fields",
        "params": {
            "title": "Demo title",
            "url": "/demo",
            "created_at": "2018-06-19T05:02:38.174Z",
            "updated_at": "2018-06-19T05:07:57.155Z",
            "version": 2,
            "fields": [{
                    "uid": "fname",
                    "value": "vicky"
                },
                {
                    "uid": "lname",
                    "value": "test"
                }
            ]
        }
    },
    "query": {
        "bool": {
            "must": [{
                    "term": {
                        "_id": "bltde56dd11ba998bab"
                    }
                },
                {
                    "range": {
                        "version": {
                            "lt": 2
                        }
                    }
                }
            ]
        }
    }
}

But it is giving me status code:409 and following error:

[documents][bltde56dd11ba998bab]: version conflict, current version [3] is different than the one provided [2]

My document also contain custom version key. Can anyone help me into this



Solution 1:[1]

For the sake of posterity, I'll submit an answer to this old question. The issue is occurring because ElasticSearch's internal version value in the _version field is actually 3 in your initial response, not 1.

You are then trying to update the document to using external version value 2, Elastic sees this as a conflict, as internally it thinks version 3 is the most up-to-date version, not version 1. Effectively, something as caused your external version scheme and Elastic's internal version scheme to become out-of-sync.

Also note, the following parameter should be included in your update calls to indicate that the operation should follow the rules for external versioning as opposed to Elastic's internal versioning scheme.

"version_type":external

There is a subtle but important distinction that needs to be made by specifying this parameter.

With version_type set to external, Elasticsearch will store the version number as given and will not increment it. Also, instead of checking for an exact match, Elasticsearch will only return a version collision error if the version currently stored is greater or equal to the one in the indexing command.

More information can be on Elastic's version can be found in their blog post

Solution 2:[2]

for me, it was document id. I am using node js elastic-search client, when I create a document I need to pass a document Id,

I was getting version conflict because I was trying to create multiple documents with the same id.

    await elasticWrapper.client.create({
          index: ElasticIndexs.Payments,
          id: data.id, // <-- id should be unique 
          body: {
            ...data,
          },
        });
``

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 burtmacklin16
Solution 2 Rafiq