(十三)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返回時,它將爲每一個操做提供一個狀態(按照發送的順序),以便您能夠檢查特定操做是否失敗。
相關文章
相關標籤/搜索