文檔是不可改變的,不能修改他們。
如果想要更新現(xiàn)在有的文檔,需要重建索引或者進(jìn)行替換
不能訪問舊版本的文檔,但它不是立即刪除的
PUT /website/blog/123
{
"title": "My first blog entry",
"text": "I am starting to get the hang of this...",
"date": "2014/01/02"
}
返回的結(jié)果:_version自增,created,表示不是create
{
"_index" : "website",
"_type" : "blog",
"_id" : "123",
"_version" : 2,
"created": false
}
可以利用_version來保證安全
put請求,(1)是指_version在1的時候才能更新
PUT /website/blog/1?version=1 (1)
{
"title": "My first blog entry",
"text": "Starting to get the hang of this..."
}
更新成功
{
"_index": "website",
"_type": "blog",
"_id": "1",
"_version": 2
"created": false
}
再次相同請求,則```490 Conflict
{
"error": {
"root_cause": [
{
"type": "version_conflict_engine_exception",
"reason": "[blog][1]: version conflict, current [2], provided [1]",
"index": "website",
"shard": "3"
}
],
"type": "version_conflict_engine_exception",
"reason": "[blog][1]: version conflict, current [2], provided [1]",
"index": "website",
"shard": "3"
},
"status": 409
}
外部系統(tǒng)保證安全
指定版本號需要大于ES當(dāng)前版本號
創(chuàng)建外部版本號為5的博客
PUT /website/blog/2?version=5&version_type=external
{
"title": "My first external blog entry",
"text": "Starting to get the hang of this..."
}
成功響應(yīng):
{
"_index": "website",
"_type": "blog",
"_id": "2",
"_version": 5,
"created": true
}
更新這個文檔,指定新版本號
PUT /website/blog/2?version=10&version_type=external
{
"title": "My first external blog entry",
"text": "This is a piece of cake..."
}
成功,失敗則返回之前的沖突錯誤
{
"_index": "website",
"_type": "blog",
"_id": "2",
"_version": 10,
"created": false
}