bulk.asciidoc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. [[docs-bulk]]
  2. == Bulk API
  3. The bulk API makes it possible to perform many index/delete operations
  4. in a single API call. This can greatly increase the indexing speed.
  5. .Client support for bulk requests
  6. *********************************************
  7. Some of the officially supported clients provide helpers to assist with
  8. bulk requests and reindexing of documents from one index to another:
  9. Perl::
  10. See https://metacpan.org/pod/Search::Elasticsearch::Bulk[Search::Elasticsearch::Bulk]
  11. and https://metacpan.org/pod/Search::Elasticsearch::Scroll[Search::Elasticsearch::Scroll]
  12. Python::
  13. See http://elasticsearch-py.readthedocs.org/en/master/helpers.html[elasticsearch.helpers.*]
  14. *********************************************
  15. The REST API endpoint is `/_bulk`, and it expects the following JSON
  16. structure:
  17. [source,js]
  18. --------------------------------------------------
  19. action_and_meta_data\n
  20. optional_source\n
  21. action_and_meta_data\n
  22. optional_source\n
  23. ....
  24. action_and_meta_data\n
  25. optional_source\n
  26. --------------------------------------------------
  27. *NOTE*: the final line of data must end with a newline character `\n`.
  28. The possible actions are `index`, `create`, `delete` and `update`.
  29. `index` and `create` expect a source on the next
  30. line, and have the same semantics as the `op_type` parameter to the
  31. standard index API (i.e. create will fail if a document with the same
  32. index and type exists already, whereas index will add or replace a
  33. document as necessary). `delete` does not expect a source on the
  34. following line, and has the same semantics as the standard delete API.
  35. `update` expects that the partial doc, upsert and script and its options
  36. are specified on the next line.
  37. If you're providing text file input to `curl`, you *must* use the
  38. `--data-binary` flag instead of plain `-d`. The latter doesn't preserve
  39. newlines. Example:
  40. [source,js]
  41. --------------------------------------------------
  42. $ cat requests
  43. { "index" : { "_index" : "test", "_type" : "type1", "_id" : "1" } }
  44. { "field1" : "value1" }
  45. $ curl -s -XPOST localhost:9200/_bulk --data-binary "@requests"; echo
  46. {"took":7,"items":[{"create":{"_index":"test","_type":"type1","_id":"1","_version":1}}]}
  47. --------------------------------------------------
  48. Because this format uses literal `\n`'s as delimiters, please be sure
  49. that the JSON actions and sources are not pretty printed. Here is an
  50. example of a correct sequence of bulk commands:
  51. [source,js]
  52. --------------------------------------------------
  53. { "index" : { "_index" : "test", "_type" : "type1", "_id" : "1" } }
  54. { "field1" : "value1" }
  55. { "delete" : { "_index" : "test", "_type" : "type1", "_id" : "2" } }
  56. { "create" : { "_index" : "test", "_type" : "type1", "_id" : "3" } }
  57. { "field1" : "value3" }
  58. { "update" : {"_id" : "1", "_type" : "type1", "_index" : "index1"} }
  59. { "doc" : {"field2" : "value2"} }
  60. --------------------------------------------------
  61. In the above example `doc` for the `update` action is a partial
  62. document, that will be merged with the already stored document.
  63. The endpoints are `/_bulk`, `/{index}/_bulk`, and `{index}/{type}/_bulk`.
  64. When the index or the index/type are provided, they will be used by
  65. default on bulk items that don't provide them explicitly.
  66. A note on the format. The idea here is to make processing of this as
  67. fast as possible. As some of the actions will be redirected to other
  68. shards on other nodes, only `action_meta_data` is parsed on the
  69. receiving node side.
  70. Client libraries using this protocol should try and strive to do
  71. something similar on the client side, and reduce buffering as much as
  72. possible.
  73. The response to a bulk action is a large JSON structure with the
  74. individual results of each action that was performed. The failure of a
  75. single action does not affect the remaining actions.
  76. There is no "correct" number of actions to perform in a single bulk
  77. call. You should experiment with different settings to find the optimum
  78. size for your particular workload.
  79. If using the HTTP API, make sure that the client does not send HTTP
  80. chunks, as this will slow things down.
  81. [float]
  82. [[bulk-versioning]]
  83. === Versioning
  84. Each bulk item can include the version value using the
  85. `_version`/`version` field. It automatically follows the behavior of the
  86. index / delete operation based on the `_version` mapping. It also
  87. support the `version_type`/`_version_type` (see <<index-versioning, versioning>>)
  88. [float]
  89. [[bulk-routing]]
  90. === Routing
  91. Each bulk item can include the routing value using the
  92. `_routing`/`routing` field. It automatically follows the behavior of the
  93. index / delete operation based on the `_routing` mapping.
  94. [float]
  95. [[bulk-parent]]
  96. === Parent
  97. Each bulk item can include the parent value using the `_parent`/`parent`
  98. field. It automatically follows the behavior of the index / delete
  99. operation based on the `_parent` / `_routing` mapping.
  100. [float]
  101. [[bulk-timestamp]]
  102. === Timestamp
  103. Each bulk item can include the timestamp value using the
  104. `_timestamp`/`timestamp` field. It automatically follows the behavior of
  105. the index operation based on the `_timestamp` mapping.
  106. [float]
  107. [[bulk-ttl]]
  108. === TTL
  109. Each bulk item can include the ttl value using the `_ttl`/`ttl` field.
  110. It automatically follows the behavior of the index operation based on
  111. the `_ttl` mapping.
  112. [float]
  113. [[bulk-consistency]]
  114. === Write Consistency
  115. When making bulk calls, you can require a minimum number of active
  116. shards in the partition through the `consistency` parameter. The values
  117. allowed are `one`, `quorum`, and `all`. It defaults to the node level
  118. setting of `action.write_consistency`, which in turn defaults to
  119. `quorum`.
  120. For example, in a N shards with 2 replicas index, there will have to be
  121. at least 2 active shards within the relevant partition (`quorum`) for
  122. the operation to succeed. In a N shards with 1 replica scenario, there
  123. will need to be a single shard active (in this case, `one` and `quorum`
  124. is the same).
  125. [float]
  126. [[bulk-refresh]]
  127. === Refresh
  128. The `refresh` parameter can be set to `true` in order to refresh the relevant
  129. primary and replica shards immediately after the bulk operation has occurred
  130. and make it searchable, instead of waiting for the normal refresh interval to
  131. expire. Setting it to `true` can trigger additional load, and may slow down
  132. indexing. Due to its costly nature, the `refresh` parameter is set on the bulk request level
  133. and is not supported on each individual bulk item.
  134. [float]
  135. [[bulk-update]]
  136. === Update
  137. When using `update` action `_retry_on_conflict` can be used as field in
  138. the action itself (not in the extra payload line), to specify how many
  139. times an update should be retried in the case of a version conflict.
  140. The `update` action payload, supports the following options: `doc`
  141. (partial document), `upsert`, `doc_as_upsert`, `script`, `params` (for
  142. script), `lang` (for script) and `fields`. See update documentation for details on
  143. the options. Curl example with update actions:
  144. [source,js]
  145. --------------------------------------------------
  146. { "update" : {"_id" : "1", "_type" : "type1", "_index" : "index1", "_retry_on_conflict" : 3} }
  147. { "doc" : {"field" : "value"} }
  148. { "update" : { "_id" : "0", "_type" : "type1", "_index" : "index1", "_retry_on_conflict" : 3} }
  149. { "script" : { "inline": "ctx._source.counter += param1", "lang" : "js", "params" : {"param1" : 1}}, "upsert" : {"counter" : 1}}
  150. { "update" : {"_id" : "2", "_type" : "type1", "_index" : "index1", "_retry_on_conflict" : 3} }
  151. { "doc" : {"field" : "value"}, "doc_as_upsert" : true }
  152. { "update" : {"_id" : "3", "_type" : "type1", "_index" : "index1", "fields" : ["_source"]} }
  153. { "doc" : {"field" : "value"} }
  154. { "update" : {"_id" : "4", "_type" : "type1", "_index" : "index1"} }
  155. { "doc" : {"field" : "value"}, "fields": ["_source"]}
  156. --------------------------------------------------
  157. [float]
  158. [[bulk-security]]
  159. === Security
  160. See <<url-access-control>>