Elasticsearch: update by query: how to handle version conflict?

Viewed 1108

I want to execute update by query on elastic, when other daemons are (maybe) also updating documents. The consequence of this is version conflict.

I want to get list of all(not only one bulk) ids of conflicted documents (for manual retry) or set up retry_on_conflict for update by query.

How can I do it?

Example of my query:

    "script": {
        "inline": "ctx._source['msp']=null",
        "lang": "painless"
    },
    "query": {
        "bool": {
            "must_not": {
                "terms": {
                    "msp-rev": [44]
                }
            },
            "must" : {
                "exists": {
                    "field": "msp"
                }
            }
        }
    }
1 Answers

Tldr;

Use update_by_query with conflicts=proceed. This will not abort your update. In the response you will have an array failures from which all the id that failed are.

POST /<index>/_update_by_query?conflicts=proceed
{
  "script": {
    "inline": "ctx._source['msp']=null",
    "lang": "painless"
  },
  "query": {
    "bool": {
        "must_not": {
            "terms": {
                "msp-rev": [44]
            }
        },
        "must" : {
            "exists": {
                "field": "msp"
            }
        }
    }
  }
}

Manually use the update query to update the documents one by one.

POST /<index>/_update/<_id>?retry_on_conflict=<some value>
{
  "doc": {
    "msp": null
  }
}
Related