Bulk是elasticsearch提供的適用於批量操作的API,可以實現批量的添加、修改、刪除,上一篇中記錄的Multi Get只能實現批量的獲取。
Bulk會把將要處理的數據載入內存中,所以數據量是有限制的,最佳的數據量不是一個確定的數值,它取決於硬件,文檔大小,復雜性,索引、以及搜索的負載。一般建議是1000-5000個文檔,大小建議是5-15M,默認不能超過100M,可以在elasticsearch的配置文件elasticsearch.yml中配置。
Bulk格式包含請求行為action和請求數據requestbody,這兩個是一條命令,但是要換行,如下:
{action:{metatata}}\n
{requestbody}\n
action是操作行為包含以下幾種:
create:文檔不存在時創建
update:更新文檔
index:創建新文檔或替換已有文檔
delete:刪除一個文檔
metatata:包含_index,_type,_id 即要執行的索引、類型、文檔id
create和index的區別:如果數據存在,使用create操作失敗,會提示文檔已經存在,使用index則可以成功執行。
具體示例如下:
1、批量添加
向索引列表lib中的books里添加id是1,3,4的文檔。(直接添加文檔會自動創建索引)
POST /lib/books/_bulk {"index":{"_id":1}} {"title":"Html5","price":45} {"index":{"_id":3}} {"title":"Php","price":35} {"index":{"_id":4}} {"title":"Python","price":50}
用上一節記錄的命令查看一下是否成功
GET /lib/books/_mget { "ids":["1","3","4"] }
2、批量修改
修改文檔id是1、3、4的文檔,把age分別修改為51、53、54
POST /lib/books/_bulk {"update":{"_index":"lib2","_type":"books","_id":1}} {"doc":{"price":51}} {"update":{"_index":"lib2","_type":"books","_id":3}} {"doc":{"price":53}} {"update":{"_index":"lib2","_type":"books","_id":4}} {"doc":{"price":54}}
3、批量刪除
POST /lib/books/_bulk {"delete":{"_index":"lib","_type":"books","_id":1}} {"delete":{"_index":"lib","_type":"books","_id":3}} {"delete":{"_index":"lib","_type":"books","_id":4}}
4、使用POST混合操作
如下,添加文檔id是1、3、4的文檔,把1的age修改為53,4的age修改為54,然后刪除掉id是3、4的文檔,此時只剩下id為1的文檔,age是53
POST /lib/books/_bulk {"index":{"_id":1}} {"title":"Html5","price":45} {"index":{"_id":3}} {"title":"Php","price":35} {"index":{"_id":4}} {"title":"Python","price":50} {"update":{"_index":"lib","_type":"books","_id":1}} {"doc":{"price":53}} {"update":{"_index":"lib","_type":"books","_id":4}} {"doc":{"price":54}} {"delete":{"_index":"lib","_type":"books","_id":3}} {"delete":{"_index":"lib","_type":"books","_id":4}}
查詢一下執行結果,與預期一致
GET /lib/books/_search { "query":{ "match_all":{} } }
{ "took": 3, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 1, "max_score": 1, "hits": [ { "_index": "lib", "_type": "books", "_id": "1", "_score": 1, "_source": { "title": "Html5", "price": 53 } } ] } }