ElasticSearch提供了文檔的批量操做機制Bulk API,能夠執行批量索引、批量刪除、批量更新等操做,也就是說Bulk API容許使用在單個步驟中進行屢次 create 、 index 、 update 或 delete 請求。node
操做流程如上圖所示:api
1.客戶端向 Node 1
發送 bulk
請求。node1計算請求中每一個文檔須要到哪一個主分區上面執行。測試
2.Node 1
爲每一個節點建立一個批量請求,並將這些請求並行轉發到每一個包含主分片的節點主機。this
3.主分片一個接一個按順序執行每一個操做。當每一個操做成功時,主分片並行轉發新文檔(或刪除)到副本分片,而後執行下一個操做。 一旦全部的副本分片報告全部操做成功,該節點將向協調節點報告成功,協調節點將這些響應收集整理並返回給客戶端。spa
bulk api與其餘請求體格式不一樣:code
{ action: { metadata }}\n { request body }\n { action: { metadata }}\n { request body }\n
它是由多個單獨的請求操做類型和請求內容共同組成的一個請求體。每一個操做類型後面緊跟的就是該操做的請求體內容。每一行都須要用\n結尾,在ElasticReach裏面就是經過\n做爲分割來解析bulk API的請求體的。索引
POST /_bulk {"delete":{"_index":"sfpay_log","_type":"waf","_id":"1"}} {"create":{"_index":"sfpay_log","_type":"waf","_id":"1"}} {"eventName":"枚舉類型爆破測試","title":"this is a bbtest","device":"0004"} {"index":{"_index":"sfpay_log","_type":"waf"}} {"eventName":"索引測試文檔","title":"this is a testccc"} {"update":{"_index":"sfpay_log","_type":"waf","_id":"2","_retry_on_conflict":3}} {"doc":{"eventName":"更新測試"}}
執行結果:返回的結果集和請求體中每個請求操做都有按順序對應的響應結果。文檔
#! Deprecation: Deprecated field [_retry_on_conflict] used, expected [retry_on_conflict] instead { "took": 49, "errors": false, "items": [ { "delete": { "_index": "sfpay_log", "_type": "waf", "_id": "1", "_version": 8, "result": "deleted", "_shards": { "total": 2, "successful": 2, "failed": 0 }, "_seq_no": 7, "_primary_term": 3, "status": 200 } }, { "create": { "_index": "sfpay_log", "_type": "waf", "_id": "1", "_version": 9, "result": "created", "_shards": { "total": 2, "successful": 2, "failed": 0 }, "_seq_no": 8, "_primary_term": 3, "status": 201 } }, { "index": { "_index": "sfpay_log", "_type": "waf", "_id": "ACrUWmQBt6c7eh0rttzY", "_version": 1, "result": "created", "_shards": { "total": 2, "successful": 2, "failed": 0 }, "_seq_no": 10, "_primary_term": 3, "status": 201 } }, { "update": { "_index": "sfpay_log", "_type": "waf", "_id": "2", "_version": 3, "result": "updated", "_shards": { "total": 2, "successful": 2, "failed": 0 }, "_seq_no": 2, "_primary_term": 3, "status": 200 } } ] }