bulk.asciidoc 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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::Client::5_0::Bulk[Search::Elasticsearch::Client::5_0::Bulk]
  11. and https://metacpan.org/pod/Search::Elasticsearch::Client::5_0::Scroll[Search::Elasticsearch::Client::5_0::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 newline delimited JSON
  16. (NDJSON) 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. // NOTCONSOLE
  28. *NOTE*: the final line of data must end with a newline character `\n`. Each newline character
  29. may be preceded by a carriage return `\r`. When sending requests to this endpoint the
  30. `Content-Type` header should be set to `application/x-ndjson`.
  31. The possible actions are `index`, `create`, `delete` and `update`.
  32. `index` and `create` expect a source on the next
  33. line, and have the same semantics as the `op_type` parameter to the
  34. standard index API (i.e. create will fail if a document with the same
  35. index and type exists already, whereas index will add or replace a
  36. document as necessary). `delete` does not expect a source on the
  37. following line, and has the same semantics as the standard delete API.
  38. `update` expects that the partial doc, upsert and script and its options
  39. are specified on the next line.
  40. If you're providing text file input to `curl`, you *must* use the
  41. `--data-binary` flag instead of plain `-d`. The latter doesn't preserve
  42. newlines. Example:
  43. [source,js]
  44. --------------------------------------------------
  45. $ cat requests
  46. { "index" : { "_index" : "test", "_id" : "1" } }
  47. { "field1" : "value1" }
  48. $ curl -s -H "Content-Type: application/x-ndjson" -XPOST localhost:9200/_bulk --data-binary "@requests"; echo
  49. {"took":7, "errors": false, "items":[{"index":{"_index":"test","_type":"_doc","_id":"1","_version":1,"result":"created","forced_refresh":false}}]}
  50. --------------------------------------------------
  51. // NOTCONSOLE
  52. // Not converting to console because this shows how curl works
  53. Because this format uses literal `\n`'s as delimiters, please be sure
  54. that the JSON actions and sources are not pretty printed. Here is an
  55. example of a correct sequence of bulk commands:
  56. [source,js]
  57. --------------------------------------------------
  58. POST _bulk
  59. { "index" : { "_index" : "test", "_id" : "1" } }
  60. { "field1" : "value1" }
  61. { "delete" : { "_index" : "test", "_id" : "2" } }
  62. { "create" : { "_index" : "test", "_id" : "3" } }
  63. { "field1" : "value3" }
  64. { "update" : {"_id" : "1", "_index" : "test"} }
  65. { "doc" : {"field2" : "value2"} }
  66. --------------------------------------------------
  67. // CONSOLE
  68. The result of this bulk operation is:
  69. [source,js]
  70. --------------------------------------------------
  71. {
  72. "took": 30,
  73. "errors": false,
  74. "items": [
  75. {
  76. "index": {
  77. "_index": "test",
  78. "_type": "_doc",
  79. "_id": "1",
  80. "_version": 1,
  81. "result": "created",
  82. "_shards": {
  83. "total": 2,
  84. "successful": 1,
  85. "failed": 0
  86. },
  87. "status": 201,
  88. "_seq_no" : 0,
  89. "_primary_term": 1
  90. }
  91. },
  92. {
  93. "delete": {
  94. "_index": "test",
  95. "_type": "_doc",
  96. "_id": "2",
  97. "_version": 1,
  98. "result": "not_found",
  99. "_shards": {
  100. "total": 2,
  101. "successful": 1,
  102. "failed": 0
  103. },
  104. "status": 404,
  105. "_seq_no" : 1,
  106. "_primary_term" : 2
  107. }
  108. },
  109. {
  110. "create": {
  111. "_index": "test",
  112. "_type": "_doc",
  113. "_id": "3",
  114. "_version": 1,
  115. "result": "created",
  116. "_shards": {
  117. "total": 2,
  118. "successful": 1,
  119. "failed": 0
  120. },
  121. "status": 201,
  122. "_seq_no" : 2,
  123. "_primary_term" : 3
  124. }
  125. },
  126. {
  127. "update": {
  128. "_index": "test",
  129. "_type": "_doc",
  130. "_id": "1",
  131. "_version": 2,
  132. "result": "updated",
  133. "_shards": {
  134. "total": 2,
  135. "successful": 1,
  136. "failed": 0
  137. },
  138. "status": 200,
  139. "_seq_no" : 3,
  140. "_primary_term" : 4
  141. }
  142. }
  143. ]
  144. }
  145. --------------------------------------------------
  146. // TESTRESPONSE[s/"took": 30/"took": $body.took/]
  147. // TESTRESPONSE[s/"index_uuid": .../"index_uuid": $body.items.3.update.error.index_uuid/]
  148. // TESTRESPONSE[s/"_seq_no" : 0/"_seq_no" : $body.items.0.index._seq_no/]
  149. // TESTRESPONSE[s/"_primary_term" : 1/"_primary_term" : $body.items.0.index._primary_term/]
  150. // TESTRESPONSE[s/"_seq_no" : 1/"_seq_no" : $body.items.1.delete._seq_no/]
  151. // TESTRESPONSE[s/"_primary_term" : 2/"_primary_term" : $body.items.1.delete._primary_term/]
  152. // TESTRESPONSE[s/"_seq_no" : 2/"_seq_no" : $body.items.2.create._seq_no/]
  153. // TESTRESPONSE[s/"_primary_term" : 3/"_primary_term" : $body.items.2.create._primary_term/]
  154. // TESTRESPONSE[s/"_seq_no" : 3/"_seq_no" : $body.items.3.update._seq_no/]
  155. // TESTRESPONSE[s/"_primary_term" : 4/"_primary_term" : $body.items.3.update._primary_term/]
  156. The endpoints are `/_bulk`, `/{index}/_bulk`, and `{index}/{type}/_bulk`.
  157. When the index or the index/type are provided, they will be used by
  158. default on bulk items that don't provide them explicitly.
  159. A note on the format. The idea here is to make processing of this as
  160. fast as possible. As some of the actions will be redirected to other
  161. shards on other nodes, only `action_meta_data` is parsed on the
  162. receiving node side.
  163. Client libraries using this protocol should try and strive to do
  164. something similar on the client side, and reduce buffering as much as
  165. possible.
  166. The response to a bulk action is a large JSON structure with the individual
  167. results of each action that was performed in the same order as the actions that
  168. appeared in the request. The failure of a single action does not affect the
  169. remaining actions.
  170. There is no "correct" number of actions to perform in a single bulk
  171. call. You should experiment with different settings to find the optimum
  172. size for your particular workload.
  173. If using the HTTP API, make sure that the client does not send HTTP
  174. chunks, as this will slow things down.
  175. [float]
  176. [[bulk-optimistic-concurrency-control]]
  177. === Optimistic Concurrency Control
  178. Each `index` and `delete` action within a bulk API call may include the
  179. `if_seq_no` and `if_primary_term` parameters in their respective action
  180. and meta data lines. The `if_seq_no` and `if_primary_term` parameters control
  181. how operations are executed, based on the last modification to existing
  182. documents. See <<optimistic-concurrency-control>> for more details.
  183. [float]
  184. [[bulk-versioning]]
  185. === Versioning
  186. Each bulk item can include the version value using the
  187. `version` field. It automatically follows the behavior of the
  188. index / delete operation based on the `_version` mapping. It also
  189. support the `version_type` (see <<index-versioning, versioning>>)
  190. [float]
  191. [[bulk-routing]]
  192. === Routing
  193. Each bulk item can include the routing value using the
  194. `routing` field. It automatically follows the behavior of the
  195. index / delete operation based on the `_routing` mapping.
  196. [float]
  197. [[bulk-wait-for-active-shards]]
  198. === Wait For Active Shards
  199. When making bulk calls, you can set the `wait_for_active_shards`
  200. parameter to require a minimum number of shard copies to be active
  201. before starting to process the bulk request. See
  202. <<index-wait-for-active-shards,here>> for further details and a usage
  203. example.
  204. [float]
  205. [[bulk-refresh]]
  206. === Refresh
  207. Control when the changes made by this request are visible to search. See
  208. <<docs-refresh,refresh>>.
  209. NOTE: Only the shards that receive the bulk request will be affected by
  210. `refresh`. Imagine a `_bulk?refresh=wait_for` request with three
  211. documents in it that happen to be routed to different shards in an index
  212. with five shards. The request will only wait for those three shards to
  213. refresh. The other two shards of that make up the index do not
  214. participate in the `_bulk` request at all.
  215. [float]
  216. [[bulk-update]]
  217. === Update
  218. When using `update` action `retry_on_conflict` can be used as field in
  219. the action itself (not in the extra payload line), to specify how many
  220. times an update should be retried in the case of a version conflict.
  221. The `update` action payload, supports the following options: `doc`
  222. (partial document), `upsert`, `doc_as_upsert`, `script`, `params` (for
  223. script), `lang` (for script) and `_source`. See update documentation for details on
  224. the options. Example with update actions:
  225. [source,js]
  226. --------------------------------------------------
  227. POST _bulk
  228. { "update" : {"_id" : "1", "_index" : "index1", "retry_on_conflict" : 3} }
  229. { "doc" : {"field" : "value"} }
  230. { "update" : { "_id" : "0", "_index" : "index1", "retry_on_conflict" : 3} }
  231. { "script" : { "source": "ctx._source.counter += params.param1", "lang" : "painless", "params" : {"param1" : 1}}, "upsert" : {"counter" : 1}}
  232. { "update" : {"_id" : "2", "_index" : "index1", "retry_on_conflict" : 3} }
  233. { "doc" : {"field" : "value"}, "doc_as_upsert" : true }
  234. { "update" : {"_id" : "3", "_index" : "index1", "_source" : true} }
  235. { "doc" : {"field" : "value"} }
  236. { "update" : {"_id" : "4", "_index" : "index1"} }
  237. { "doc" : {"field" : "value"}, "_source": true}
  238. --------------------------------------------------
  239. // CONSOLE
  240. // TEST[continued]
  241. [float]
  242. [[bulk-security]]
  243. === Security
  244. See <<url-access-control>>