1、批量查詢的好處
就是一條一條的查詢,比如說要查詢100條數據,那么就要發送100次網絡請求,這個開銷還是很大的。如果進行批量查詢的話,查詢100條數據,就只要發送1次網絡請求,網絡請求的性能開銷縮減100倍
2、批量查詢 mget
可以說mget是很重要的,一般來說,在進行查詢的時候,如果一次性要查詢多條數據的話,那么一定要用batch批量操作的api,盡可能減少網絡開銷次數,可能可以將性能提升數倍,甚至數十倍,非常非常之重要。
先放點數據:
put /ecommerce/product/1 { "a":"1", "b":"2" } put /ecommerce/product/2 { "a":"3", "b":"4" }
批量查詢:
GET /_mget { "docs":[ { "_index":"ecommerce", "_type":"product", "_id":1 }, { "_index":"ecommerce", "_type":"product", "_id":2 } ] }
結果:
{ "docs" : [ { "_index" : "ecommerce", "_type" : "product", "_id" : "1", "_version" : 1, "_seq_no" : 0, "_primary_term" : 1, "found" : true, "_source" : { "a" : "1", "b" : "2" } }, { "_index" : "ecommerce", "_type" : "product", "_id" : "2", "_version" : 1, "_seq_no" : 0, "_primary_term" : 1, "found" : true, "_source" : { "a" : "3", "b" : "4" } } ] }
get ecommerce/_mget { "docs":[ { "_type":"product", "_id":1 }, { "_type":"product", "_id":2 } ] }
GET /ecommerce/product/_mget { "ids":[1,2,3,4] }
3、批量增刪改 bulk
(2)create:PUT /index/type/id/_create,強制創建
(3)index:普通的put操作,可以是創建文檔,也可以是全量替換文檔
(4)update:執行的partial update操作
注意:
(1)action所在json和data所在的json要換行,當action為delete時沒有data
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"}}
bulk request會加載到內存里,如果太大的話,性能反而會下降,因此需要反復嘗試一個最佳的bulk size。一般從1000~5000條數據開始,嘗試逐漸增加。另外,如果看大小的話,最好是在5~15MB之間。
1、document的全量替換
(1)語法與創建文檔是一樣的,如果document id不存在,那么就是創建;如果document id已經存在,那么就是全量替換操作,替換document的json串內容
(2)document是不可變的,如果要修改document的內容,第一種方式就是全量替換,直接對document重新建立索引,替換里面所有的內容
(3)es會將老的document標記為deleted,然后新增我們給定的一個document,當我們創建越來越多的document的時候,es會在適當的時機在后台自動刪除標記為deleted的document
2、document的強制創建
(1)創建文檔與全量替換的語法是一樣的,有時我們只是想新建文檔,不想替換文檔,如果強制進行創建呢?
(2)PUT /index/type/id?op_type=create 或者 PUT /index/type/id/_create
3、document的刪除
(1)DELETE /index/type/id
(2)不會理解物理刪除,只會將其標記為deleted,當數據越來越多的時候,在后台自動刪除