【ElasticSearch(四)】PUT/POST更新數據、DELETE刪除數據、_bulk批量操作
先查詢下現在的情況GET http://localhost:9200/customer/external/1
{
"_index": "customer",
"_type": "external",
"_id": "1",
"_version": 3,
"_seq_no": 5,
"_primary_term": 1,
"found": true,
"_source": {
"name": 1
}
}
一、PUT / POST追加更新數據
要注意一點:
更新操作時,如果URL帶_update,更新前會比對新舊數據,如果新舊數據完全相同,將不會進行任何操作noop,不會影響序列號、版本號信息。
如果URI不帶_update,不會檢查原數據,都會顯示updated
1.POST訪問地址:(路徑帶_update)
POST customer/external/1/_update
Body數據:
路徑帶_update ,要在參數外套一層doc
{
"doc":{
"name":2
}
}
返回結果:更新成功
{
"_index": "customer",
"_type": "external",
"_id": "1",
"_version": 4,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 7,
"_primary_term": 2
}
當我們再次發送請求時(Body數據相同),會發現200請求成功,但是版本號沒有發生變化,result變成了noop(no operation)表示沒有發生變化。

現在修改Body數據
{
"doc":{
"name":3
}
}
再次發送請求,返回結果。updated

2.POST訪問地址:(路徑不帶_update)
POST customer/external/1
返回結果:盡管name參數和原來一樣,但還是會updated的。

3.PUT訪問地址:
(注意PUT訪問路徑是不能加_update的)
PUT customer/external/1
這個測試結果和 POST customer/external/1 一樣
二、PUT / POST增加屬性更新
增加屬性更新和上方規則相同,傳參直接添加屬性就可以保存。
下面僅以PUT為例:
PUT customer/external/1
Body請求數據:
{
"name": "xiaoming",
"age": 13
}
再次使用GET查詢返回結果:
{
"_index": "customer",
"_type": "external",
"_id": "1",
"_version": 8,
"_seq_no": 11,
"_primary_term": 2,
"found": true,
"_source": {
"name": "xiaoming",
"age": 13
}
}
三、DELETE刪除數據信息
DELETE http://localhost:9200/customer/external/1/
返回結果:result變為deleted
{
"_index": "customer",
"_type": "external",
"_id": "1",
"_version": 9,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 12,
"_primary_term": 2
}
再用GET方法查詢,返回404沒找到:found為false

四、DELETE刪除整個索引
DELETE http://localhost:9200/customer
返回200,結果:
{
"acknowledged": true
}
再用GET方法查詢,返回404錯誤:找不到索引
{
"error": {
"root_cause": [
{
"type": "index_not_found_exception",
"reason": "no such index [customer]",
"resource.type": "index_or_alias",
"resource.id": "customer",
"index_uuid": "_na_",
"index": "customer"
}
],
"type": "index_not_found_exception",
"reason": "no such index [customer]",
"resource.type": "index_or_alias",
"resource.id": "customer",
"index_uuid": "_na_",
"index": "customer"
},
"status": 404
}
注意:ES中並不能刪除索引中的類型,只能刪除整個索引和數據信息。
五、_bulk批量操作
批量操作中的每一個操作相互獨立,可以獨立成功或失敗,彼此沒有影響(不像mysql,批量操作中的一條執行失敗,后面就不會執行了)。
1.請求的語法格式
這里有兩條數據,數據之間可以沒有空行。
POST 索引/類型/_bulk
{action:{metadata}}\n
{request body}\n
{action:{metadata}}\n
{request body}\n
action:操作,可以是create(創建),"index"(保存),update(更新),delete(刪除)等
metadata:元數據,可以寫數據的"_id"等
request body:寫數據本身
2.測試工具
由於PostMan無法完成本功能,會出現400錯誤,出錯原因是由於PostMan自動優化了換行符號。
我們需要采用Kibana測試。
訪問http://localhost:5601,選擇Kibana,選擇側邊欄的Dev Tools

界面長這個樣子。點擊執行箭頭,就可以發送請求

3.測試案例
【案例一】
POST /customer/external/_bulk
{"index":{"_id":"1"}}
{"name": "JSON Doe"}
{"index":{"_id":"2"}}
{"name": "Jone Doe"}
返回結果:
took:完成請求花費的時間,毫秒
errors:是否出現了錯誤
items:返回的結果
#! [types removal] Specifying types in bulk requests is deprecated.
{
"took" : 40,
"errors" : false,
"items" : [
{
"index" : {
"_index" : "customer",
"_type" : "external",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1,
"status" : 201
}
},
{
"index" : {
"_index" : "customer",
"_type" : "external",
"_id" : "2",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 1,
"_primary_term" : 1,
"status" : 201
}
}
]
}
【案例二】對整個ES進行批量操作
POST /_bulk
{"delete": {"_index": "website", "_type": "blog", "_id": "123"}}
{"create": {"_index": "website", "_type": "blog", "_id": "123"}}
{"title": "My first blog post"}
{"index": {"_index": "website", "_type": "blog", "_id": "123"}}
{"title": "My second blog post"}
{"update": {"_index": "website", "_type": "blog", "_id": "123"}}
{"doc": {"title": "My updated blog post"}}
我這里不小心執行了兩次這個批量操作
返回結果:
#! [types removal] Specifying types in bulk requests is deprecated.
{
"took" : 6,
"errors" : false,
"items" : [
{
"delete" : {
"_index" : "website",
"_type" : "blog",
"_id" : "123",
"_version" : 5,
"result" : "deleted",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 4,
"_primary_term" : 1,
"status" : 200
}
},
{
"create" : {
"_index" : "website",
"_type" : "blog",
"_id" : "123",
"_version" : 6,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 5,
"_primary_term" : 1,
"status" : 201
}
},
{
"index" : {
"_index" : "website",
"_type" : "blog",
"_id" : "123",
"_version" : 7,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 6,
"_primary_term" : 1,
"status" : 200
}
},
{
"update" : {
"_index" : "website",
"_type" : "blog",
"_id" : "123",
"_version" : 8,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 7,
"_primary_term" : 1,
"status" : 200
}
}
