普通關係型數據庫使用的是(悲觀併發控制(PCC))數據庫
當咱們在修改一個數據前先鎖定這一行,而後確保只有讀取到數據的這個線程能夠修改這一行數據併發
ES使用的是(樂觀併發控制(OCC)) curl
ES不會阻止某一數據的訪問,然而,若是基礎數據在咱們讀取和寫入的間隔中發生了變化,更新就會失敗,這時候就由程序來決定如何處理這個衝突。它能夠從新讀取新數據來進行更新,又或者將這一狀況直接反饋給用戶。elasticsearch
ES如何實現版本控制(使用es內部版本號)ide
1)首先獲得想要修改的文檔,獲取版本version號oop
curl -XGET http://master:9200/zhouls/user/2
[hadoop@master elasticsearch-2.4.0]$ curl -XGET http://master:9200/zhouls/user/2 {"_index":"zhouls","_type":"user","_id":"2","_version":1,"found":true,"_source":{"name" : "john" , "age" : 28}}[hadoop@master elasticsearch-2.4.0]$ [hadoop@master elasticsearch-2.4.0]$ [hadoop@master elasticsearch-2.4.0]$
2)在執行更新操做的時候把版本號傳過去url
curl -XPUT http://master:9200/zhouls/user/2?version=1 -d '{"name" : "john1" , "age" : 29}'
[hadoop@master elasticsearch-2.4.0]$ curl -XPUT http://master:9200/zhouls/user/2?version=1 -d '{"name" : "john1" , "age" : 29}' {"_index":"zhouls","_type":"user","_id":"2","_version":2,"_shards":{"total":2,"successful":2,"failed":0},"created":false}[hadoop@master elasticsearch-2.4.0]$
curl -XPOST http://master:9200/zhouls/user/2/_update?version=2 -d '{"doc" :{"age" : 30}}'
[hadoop@master elasticsearch-2.4.0]$ curl -XPOST http://master:9200/zhouls/user/2/_update?version=2 -d '{"doc" :{"age" : 30}}' {"_index":"zhouls","_type":"user","_id":"2","_version":3,"_shards":{"total":2,"successful":2,"failed":0}}[hadoop@master elasticsearch-2.4.0]$ [hadoop@master elasticsearch-2.4.0]$
3)若是傳遞的版本號和待更新的文檔的版本號不一致,則會更新失敗。spa
如今,待更新文檔的版本號是3。我分別傳1和穿4進入,都會報更新失敗。以下.net
[hadoop@master elasticsearch-2.4.0]$ curl -XPOST http://master:9200/zhouls/user/2/_update?version=2 -d '{"doc" :{"age" : 30}}' {"_index":"zhouls","_type":"user","_id":"2","_version":3,"_shards":{"total":2,"successful":2,"failed":0}}[hadoop@master elasticsearch-2.4.0]$ [hadoop@master elasticsearch-2.4.0]$ [hadoop@master elasticsearch-2.4.0]$ curl -XPOST http://master:9200/zhouls/user/2/_update?version=1 -d '{"doc" :{"age" : 30}}' {"error":{"root_cause":[{"type":"version_conflict_engine_exception","reason":"[user][2]: version conflict, current [3], provided [1]","index":"zhouls","shard":"2"}],"type":"version_conflict_engine_exception","reason":"[user][2]: version conflict, current [3], provided [1]","index":"zhouls","shard":"2"},"status":409}[hadoop@master elasticsearch-2.4.0]$ [hadoop@master elasticsearch-2.4.0]$ [hadoop@master elasticsearch-2.4.0]$ curl -XPOST http://master:9200/zhouls/user/2/_update?version=4 -d '{"doc" :{"age" : 30}}' {"error":{"root_cause":[{"type":"version_conflict_engine_exception","reason":"[user][2]: version conflict, current [3], provided [4]","index":"zhouls","shard":"2"}],"type":"version_conflict_engine_exception","reason":"[user][2]: version conflict, current [3], provided [4]","index":"zhouls","shard":"2"},"status":409}[hadoop@master elasticsearch-2.4.0]$ [hadoop@master elasticsearch-2.4.0]$ [hadoop@master elasticsearch-2.4.0]$
更多,請見插件