1. 程式人生 > >Elasticsearch學習筆記(十)批量查詢mget、批量增刪改bulk

Elasticsearch學習筆記(十)批量查詢mget、批量增刪改bulk

出錯 color body 換行 nor test 增刪 doc document

一、批量查詢 mget


GET /_mget
{
"docs":[
{
"_index":"ecommerce",
"_type":"product",
"_id":1
},
{
"_index":"ecommerce",
"_type":"product",
"_id":2
}
]

}
如果docs內的document都是同一個index則可以簡略為:
GET /ecommerce/_mget
{
"docs":[
{
"_type":"product",
"_id":1
},
{
"_type":"product",
"_id":2
}
]

}
如果docs內的document都是同一個index同一個type則可以進一步簡略為:
GET /ecommerce/product/_mget
{
"ids":[1,2,3,4]
}

二、批量增刪改 bulk

1、bulk語法
POST /_bulk {"action":{"_index":"","_type":"","_id":""}} {"data"} action的值有: (1)delete:刪除一個文檔,只要1個json串就可以了
(2)create:PUT /index/type/id/_create,強制創建
(3)index:普通的put操作,可以是創建文檔,也可以是全量替換文檔
(4)update:執行的partial update操作
註意:
(1)action所在json和data所在的json要換行,當action為delete時沒有data
(2)action所在的json內不能有空格換行,否則出錯
(3)每個json串不能換行,只能放一行,同時一個json串和一個json串之間,必須有一個換行
(4)bulk操作中,任意一個操作失敗,是不會影響其他的操作的,但是在返回結果裏,會告訴你異常日誌
示例:
POST /_bulk
{"delete":{"_index":"test_index","_type":"test_type","_id":"3"}}
{"create":{"_index":"test_index","_type":"test_type","_id":"13"}}
{"test_field":"replaced test13"}
{"index":{"_index":"test_index","_type":"test_type","_id":"2"}}
{"test_field":"replaced test2"}
{"update":{"_index":"test_index","_type":"test_type","_id":"1","_retry_on_conflict":3}}
{"doc":{"test_field2":"bulk test 1"}}
2、bulk size最佳大小
bulk request會加載到內存裏,如果太大的話,性能反而會下降,因此需要反復嘗試一個最佳的bulk size。一般從1000~5000條數據開始,嘗試逐漸增加。另外,如果看大小的話,最好是在5~15MB之間。

Elasticsearch學習筆記(十)批量查詢mget、批量增刪改bulk