Sure, basically you provide what you think the version is on each request (you can get it beforehand). Then, on the indexing request you specify what you think the current version. If the version in fact matches, Elasticsearch will execute the update. If you the version does not match, Elasticsearch will reject the request. If you have two racing requests, only one of them will win and the other will be rejected. When this happens, if the request should be retried (because it was the most recent update that was rejected), you have to get the version again and send a new request. You have to manage client side whether the loser in a race was a previous versus the most recent update.
$ curl -H "Content-Type: application/json" -XPOST localhost:9200/i/_bulk?pretty=true -d '
{ "create": { "_index": "i", "_type": "t", "_id": "1" } }
{ "f": "v" }
'
{
"took" : 53,
"errors" : false,
"items" : [
{
"create" : {
"_index" : "i",
"_type" : "t",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"created" : true,
"status" : 201
}
}
]
}
$ curl -H "Content-Type: application/json" -XPOST localhost:9200/i/_bulk?pretty=true -d '
{ "index": { "_index": "i", "_type": "t", "_id": "1", "_version": 1 } }
{ "f": "v" }
'
{
"took" : 2,
"errors" : false,
"items" : [
{
"index" : {
"_index" : "i",
"_type" : "t",
"_id" : "1",
"_version" : 2,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"created" : false,
"status" : 200
}
}
]
}
$ curl -H "Content-Type: application/json" -XPOST localhost:9200/i/_bulk?pretty=true -d '
{ "index": { "_index": "i", "_type": "t", "_id": "1", "_version": 1 } }
{ "f": "v" }
'
{
"took" : 0,
"errors" : true,
"items" : [
{
"index" : {
"_index" : "i",
"_type" : "t",
"_id" : "1",
"status" : 409,
"error" : {
"type" : "version_conflict_engine_exception",
"reason" : "[t][1]: version conflict, current version [2] is different than the one provided [1]",
"index_uuid" : "wVU_5D_ZQ3a9hZxrtBoeVA",
"shard" : "3",
"index" : "i"
}
}
}
]
}