(十三)Batch Processing

In addition to being able to index, update, and delete individual documents, Elasticsearch also provides the ability to perform any of the above operations in batches using the _bulk API. This functionality is important in that it provides a very efficient mechanism to do multiple operations as fast as possible with as few network roundtrips as possible.html

批处理过程 除了可以索引,更新和删除单个文档以外,Elasticsearch还提供了使用_bulk API批量执行上述任何操做的功能。此功能很是重要,由于它提供了一种很是有效的机制,能够尽量快地执行多个操做,并尽量少地进行网络往返。
 
 As a quick example, the following call indexes two documents (ID 1 - John Doe and ID 2 - Jane Doe) in one bulk operation:
做为一个简单示例,如下调用在一个批量操做中索引两个文档(ID 1 - John Doe和ID 2 - Jane Doe):
curl -X POST "localhost:9200/customer/_doc/_bulk?pretty" -H 'Content-Type: application/json' -d'
{"index":{"_id":"1"}}
{"name": "John Doe" }
{"index":{"_id":"2"}}
{"name": "Jane Doe" }
'
使用postMan调用时
{"index":{"_id":"1"}}
{"name": "John Doe" }
//此处必须换行 {"index":{"_id":"2"}} {"name": "Jane Doe" }
//此处必须换行

This example updates the first document (ID of 1) and then deletes the second document (ID of 2) in one bulk operation:json

此示例更新第一个文档(ID为1),而后在一个批量操做中删除第二个文档(ID为2):
curl -X POST "localhost:9200/customer/_doc/_bulk?pretty" -H 'Content-Type: application/json' -d'
{"update":{"_id":"1"}}
{"doc": { "name": "John Doe becomes Jane Doe" } }
{"delete":{"_id":"2"}}
'
使用postMan调用时
与上面同样,须要经过换行分隔

Note above that for the delete action, there is no corresponding source document after it since deletes only require the ID of the document to be deleted.网络

请注意,对于删除操做,以后没有相应的源文档,由于删除只须要删除文档的ID。
 
 The Bulk API does not fail due to failures in one of the actions. If a single action fails for whatever reason, it will continue to process the remainder of the actions after it. When the bulk API returns, it will provide a status for each action (in the same order it was sent in) so that you can check if a specific action failed or not.
  Bulk API不会因其中一个操做失败而失败。若是单个操做因任何缘由失败,它将继续处理其后的其他操做。批量API返回时,它将为每一个操做提供一个状态(按照发送的顺序),以便您能够检查特定操做是否失败。
相关文章
相关标签/搜索