bulk.asciidoc 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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", "_type" : "type1", "_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":"type1","_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", "_type" : "type1", "_id" : "1" } }
  60. { "field1" : "value1" }
  61. { "delete" : { "_index" : "test", "_type" : "type1", "_id" : "2" } }
  62. { "create" : { "_index" : "test", "_type" : "type1", "_id" : "3" } }
  63. { "field1" : "value3" }
  64. { "update" : {"_id" : "1", "_type" : "type1", "_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": "type1",
  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": "type1",
  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": "type1",
  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": "type1",
  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
  167. individual results of each action that was performed. The failure of a
  168. single action does not affect the remaining actions.
  169. There is no "correct" number of actions to perform in a single bulk
  170. call. You should experiment with different settings to find the optimum
  171. size for your particular workload.
  172. If using the HTTP API, make sure that the client does not send HTTP
  173. chunks, as this will slow things down.
  174. [float]
  175. [[bulk-versioning]]
  176. === Versioning
  177. Each bulk item can include the version value using the
  178. `_version`/`version` field. It automatically follows the behavior of the
  179. index / delete operation based on the `_version` mapping. It also
  180. support the `version_type`/`_version_type` (see <<index-versioning, versioning>>)
  181. [float]
  182. [[bulk-routing]]
  183. === Routing
  184. Each bulk item can include the routing value using the
  185. `_routing`/`routing` field. It automatically follows the behavior of the
  186. index / delete operation based on the `_routing` mapping.
  187. [float]
  188. [[bulk-wait-for-active-shards]]
  189. === Wait For Active Shards
  190. When making bulk calls, you can set the `wait_for_active_shards`
  191. parameter to require a minimum number of shard copies to be active
  192. before starting to process the bulk request. See
  193. <<index-wait-for-active-shards,here>> for further details and a usage
  194. example.
  195. [float]
  196. [[bulk-refresh]]
  197. === Refresh
  198. Control when the changes made by this request are visible to search. See
  199. <<docs-refresh,refresh>>.
  200. [float]
  201. [[bulk-update]]
  202. === Update
  203. When using `update` action `_retry_on_conflict` can be used as field in
  204. the action itself (not in the extra payload line), to specify how many
  205. times an update should be retried in the case of a version conflict.
  206. The `update` action payload, supports the following options: `doc`
  207. (partial document), `upsert`, `doc_as_upsert`, `script`, `params` (for
  208. script), `lang` (for script) and `_source`. See update documentation for details on
  209. the options. Example with update actions:
  210. [source,js]
  211. --------------------------------------------------
  212. POST _bulk
  213. { "update" : {"_id" : "1", "_type" : "type1", "_index" : "index1", "_retry_on_conflict" : 3} }
  214. { "doc" : {"field" : "value"} }
  215. { "update" : { "_id" : "0", "_type" : "type1", "_index" : "index1", "_retry_on_conflict" : 3} }
  216. { "script" : { "source": "ctx._source.counter += params.param1", "lang" : "painless", "params" : {"param1" : 1}}, "upsert" : {"counter" : 1}}
  217. { "update" : {"_id" : "2", "_type" : "type1", "_index" : "index1", "_retry_on_conflict" : 3} }
  218. { "doc" : {"field" : "value"}, "doc_as_upsert" : true }
  219. { "update" : {"_id" : "3", "_type" : "type1", "_index" : "index1", "_source" : true} }
  220. { "doc" : {"field" : "value"} }
  221. { "update" : {"_id" : "4", "_type" : "type1", "_index" : "index1"} }
  222. { "doc" : {"field" : "value"}, "_source": true}
  223. --------------------------------------------------
  224. // CONSOLE
  225. // TEST[continued]
  226. [float]
  227. [[bulk-security]]
  228. === Security
  229. See <<url-access-control>>