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 } } ] }