第八篇 elasticsearch的批量增刪改查
阿新 • • 發佈:2019-02-10
1. 批量查詢
1.1 mget查詢的語法
GET /_mget
{
"docs" : [
{
"_index" : "test_index",
"_type" : "test_type",
"_id" : 1
},
{
"_index" : "test_index",
"_type" : "test_type",
"_id" : 2
}
]
}
1.2 查詢的document是一個index下的不同type
GET /test_index/_mget
{
"docs" : [
{
"_type" : "test_type",
"_id" : 1
},
{
"_type" : "test_type",
"_id" : 2
}
]
}
1.3 如果查詢的資料都在同一個index下的同一個type下
GET /test_index/test_type/_mget
{
"ids": [1, 2]
}
- mget的重要性
mget是很重要的,一般來說,在進行查詢的時候,如果一次性要查詢多條資料的話,那麼一定要用batch批量操作的api,儘可能減少網路開銷次數,可能可以將效能提升數倍,甚至數十倍,非常非常之重要。
2. bulk批量增刪改
- bulk語法
POST /_bulk
//刪除
{ "delete": { "_index": "test_index", "_type": "test_type", "_id": "3" }}
//強制建立
{ "create": { "_index": "test_index", "_type": "test_type", "_id": "12" }}
{"test_field1": "test1", "test_field2": "test2"}
//一般建立
{ "index": { "_index": "test_index", "_type": "test_type" , "_id": "2" }}
{"test_field1": "test1", "test_field2": "test2"}
//partial update操作
{ "update": { "_index": "test_index", "_type": "test_type", "_id": "1", "_retry_on_conflict" : 3} }
{ "doc" : {"test_field2" : "bulk test1"} }
- bulk size最佳大小
bulk request會載入到記憶體裡,如果太大的話,效能反而會下降,因此需要反覆嘗試一個最佳的bulk size。一般從1000~5000條資料開始,嘗試逐漸增加。另外,如果看大小的話,最好是在5~15MB之間。