内部版本控制:_version自增长,修改数据后,_version会自动加1
外部版本控制:为了保持_version与外部版本控制的数值一致
使用version_type=external
检查数据当前的version值是否小于请求中的version值
PUT /library/books/1
{
"title": "Elasticsearch: The Definitive Guide",
"name" : {
"first" : "Zachary",
"last" : "Tong"
},
"publish_date":"2015-02-06",
"price":"59.99"
}
查看数据:
GET /library/books/1
{
"_index": "library",
"_type": "books",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
"title": "Elasticsearch: The Definitive Guide",
"name": {
"first": "Zachary",
"last": "Tong"
},
"publish_date": "2015-02-06",
"price": "59.99"
}
}
修改数据:
POST /library/books/1/_update
{
"doc": {
"price" : 10
}
}
此时版本为2:
{
"_index": "library",
"_type": "books",
"_id": "1",
"_version": 2,
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
}
}
指定版本去更新:
POST /library/books/1/_update?version=3
{
"doc": {
"price" : 15
}
}
{
"error": {
"root_cause": [
{
"type": "version_conflict_engine_exception",
"reason": "[books][1]: version conflict, current [2], provided [3]",
"shard": "3",
"index": "library"
}
],
"type": "version_conflict_engine_exception",
"reason": "[books][1]: version conflict, current [2], provided [3]",
"shard": "3",
"index": "library"
},
"status": 409
}