bulk.asciidoc 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. --
  2. :api: bulk
  3. :request: BulkRequest
  4. :response: BulkResponse
  5. --
  6. [id="{upid}-{api}"]
  7. === Bulk API
  8. NOTE: The Java High Level REST Client provides the
  9. <<{upid}-{api}-processor>> to assist with bulk requests.
  10. [id="{upid}-{api}-request"]
  11. ==== Bulk Request
  12. A +{request}+ can be used to execute multiple index, update and/or delete
  13. operations using a single request.
  14. It requires at least one operation to be added to the Bulk request:
  15. ["source","java",subs="attributes,callouts,macros"]
  16. --------------------------------------------------
  17. include-tagged::{doc-tests-file}[{api}-request]
  18. --------------------------------------------------
  19. <1> Creates the +{request}+
  20. <2> Adds a first `IndexRequest` to the Bulk request. See <<{upid}-index>> for
  21. more information on how to build `IndexRequest`.
  22. <3> Adds a second `IndexRequest`
  23. <4> Adds a third `IndexRequest`
  24. WARNING: The Bulk API supports only documents encoded in JSON or SMILE.
  25. Providing documents in any other format will result in an error.
  26. And different operation types can be added to the same +{request}+:
  27. ["source","java",subs="attributes,callouts,macros"]
  28. --------------------------------------------------
  29. include-tagged::{doc-tests-file}[{api}-request-with-mixed-operations]
  30. --------------------------------------------------
  31. <1> Adds a `DeleteRequest` to the +{request}+. See <<{upid}-delete>>
  32. for more information on how to build `DeleteRequest`.
  33. <2> Adds an `UpdateRequest` to the +{request}+. See <<{upid}-update>>
  34. for more information on how to build `UpdateRequest`.
  35. <3> Adds an `IndexRequest` using the SMILE format
  36. ==== Optional arguments
  37. The following arguments can optionally be provided:
  38. ["source","java",subs="attributes,callouts,macros"]
  39. --------------------------------------------------
  40. include-tagged::{doc-tests-file}[{api}-request-timeout]
  41. --------------------------------------------------
  42. <1> Timeout to wait for the bulk request to be performed as a `TimeValue`
  43. <2> Timeout to wait for the bulk request to be performed as a `String`
  44. ["source","java",subs="attributes,callouts,macros"]
  45. --------------------------------------------------
  46. include-tagged::{doc-tests-file}[{api}-request-refresh]
  47. --------------------------------------------------
  48. <1> Refresh policy as a `WriteRequest.RefreshPolicy` instance
  49. <2> Refresh policy as a `String`
  50. ["source","java",subs="attributes,callouts,macros"]
  51. --------------------------------------------------
  52. include-tagged::{doc-tests-file}[{api}-request-active-shards]
  53. --------------------------------------------------
  54. <1> Sets the number of shard copies that must be active before proceeding with
  55. the index/update/delete operations.
  56. <2> Number of shard copies provided as a `ActiveShardCount`: can be
  57. `ActiveShardCount.ALL`, `ActiveShardCount.ONE` or
  58. `ActiveShardCount.DEFAULT` (default)
  59. ["source","java",subs="attributes,callouts,macros"]
  60. --------------------------------------------------
  61. include-tagged::{doc-tests-file}[{api}-request-pipeline]
  62. --------------------------------------------------
  63. <1> Global pipelineId used on all sub requests, unless overridden on a sub request
  64. ["source","java",subs="attributes,callouts,macros"]
  65. --------------------------------------------------
  66. include-tagged::{doc-tests-file}[{api}-request-routing]
  67. --------------------------------------------------
  68. <1> Global routingId used on all sub requests, unless overridden on a sub request
  69. ["source","java",subs="attributes,callouts,macros"]
  70. --------------------------------------------------
  71. include-tagged::{doc-tests-file}[{api}-request-index-type]
  72. --------------------------------------------------
  73. <1> A bulk request with global index and type used on all sub requests, unless overridden on a sub request.
  74. Both parameters are @Nullable and can only be set during +{request}+ creation.
  75. include::../execution.asciidoc[]
  76. [id="{upid}-{api}-response"]
  77. ==== Bulk Response
  78. The returned +{response}+ contains information about the executed operations and
  79. allows to iterate over each result as follows:
  80. ["source","java",subs="attributes,callouts,macros"]
  81. --------------------------------------------------
  82. include-tagged::{doc-tests-file}[{api}-response]
  83. --------------------------------------------------
  84. <1> Iterate over the results of all operations
  85. <2> Retrieve the response of the operation (successful or not), can be
  86. `IndexResponse`, `UpdateResponse` or `DeleteResponse` which can all be seen as
  87. `DocWriteResponse` instances
  88. <3> Handle the response of an index operation
  89. <4> Handle the response of a update operation
  90. <5> Handle the response of a delete operation
  91. The Bulk response provides a method to quickly check if one or more operation
  92. has failed:
  93. ["source","java",subs="attributes,callouts,macros"]
  94. --------------------------------------------------
  95. include-tagged::{doc-tests-file}[{api}-has-failures]
  96. --------------------------------------------------
  97. <1> This method returns `true` if at least one operation failed
  98. In such situation it is necessary to iterate over all operation results in order
  99. to check if the operation failed, and if so, retrieve the corresponding failure:
  100. ["source","java",subs="attributes,callouts,macros"]
  101. --------------------------------------------------
  102. include-tagged::{doc-tests-file}[{api}-errors]
  103. --------------------------------------------------
  104. <1> Indicate if a given operation failed
  105. <2> Retrieve the failure of the failed operation
  106. [id="{upid}-{api}-processor"]
  107. ==== Bulk Processor
  108. The `BulkProcessor` simplifies the usage of the Bulk API by providing
  109. a utility class that allows index/update/delete operations to be
  110. transparently executed as they are added to the processor.
  111. In order to execute the requests, the `BulkProcessor` requires the following
  112. components:
  113. `RestHighLevelClient`:: This client is used to execute the +{request}+
  114. and to retrieve the `BulkResponse`
  115. `BulkProcessor.Listener`:: This listener is called before and after
  116. every +{request}+ execution or when a +{request}+ failed
  117. Then the `BulkProcessor.builder` method can be used to build a new
  118. `BulkProcessor`:
  119. ["source","java",subs="attributes,callouts,macros"]
  120. --------------------------------------------------
  121. include-tagged::{doc-tests-file}[{api}-processor-init]
  122. --------------------------------------------------
  123. <1> Create the `BulkProcessor.Listener`
  124. <2> This method is called before each execution of a +{request}+
  125. <3> This method is called after each execution of a +{request}+
  126. <4> This method is called when a +{request}+ failed
  127. <5> Create the `BulkProcessor` by calling the `build()` method from
  128. the `BulkProcessor.Builder`. The `RestHighLevelClient.bulkAsync()`
  129. method will be used to execute the +{request}+ under the hood.
  130. The `BulkProcessor.Builder` provides methods to configure how the
  131. `BulkProcessor` should handle requests execution:
  132. ["source","java",subs="attributes,callouts,macros"]
  133. --------------------------------------------------
  134. include-tagged::{doc-tests-file}[{api}-processor-options]
  135. --------------------------------------------------
  136. <1> Set when to flush a new bulk request based on the number of
  137. actions currently added (defaults to 1000, use -1 to disable it)
  138. <2> Set when to flush a new bulk request based on the size of
  139. actions currently added (defaults to 5Mb, use -1 to disable it)
  140. <3> Set the number of concurrent requests allowed to be executed
  141. (default to 1, use 0 to only allow the execution of a single request)
  142. <4> Set a flush interval flushing any +{request}+ pending if the
  143. interval passes (defaults to not set)
  144. <5> Set a constant back off policy that initially waits for 1 second
  145. and retries up to 3 times. See `BackoffPolicy.noBackoff()`,
  146. `BackoffPolicy.constantBackoff()` and `BackoffPolicy.exponentialBackoff()`
  147. for more options.
  148. Once the `BulkProcessor` is created requests can be added to it:
  149. ["source","java",subs="attributes,callouts,macros"]
  150. --------------------------------------------------
  151. include-tagged::{doc-tests-file}[{api}-processor-add]
  152. --------------------------------------------------
  153. The requests will be executed by the `BulkProcessor`, which takes care of
  154. calling the `BulkProcessor.Listener` for every bulk request.
  155. The listener provides methods to access to the +{request}+ and the +{response}+:
  156. ["source","java",subs="attributes,callouts,macros"]
  157. --------------------------------------------------
  158. include-tagged::{doc-tests-file}[{api}-processor-listener]
  159. --------------------------------------------------
  160. <1> Called before each execution of a +{request}+, this method allows to know
  161. the number of operations that are going to be executed within the +{request}+
  162. <2> Called after each execution of a +{request}+, this method allows to know if
  163. the +{response}+ contains errors
  164. <3> Called if the +{request}+ failed, this method allows to know
  165. the failure
  166. Once all requests have been added to the `BulkProcessor`, its instance needs to
  167. be closed using one of the two available closing methods.
  168. The `awaitClose()` method can be used to wait until all requests have been
  169. processed or the specified waiting time elapses:
  170. ["source","java",subs="attributes,callouts,macros"]
  171. --------------------------------------------------
  172. include-tagged::{doc-tests-file}[{api}-processor-await]
  173. --------------------------------------------------
  174. <1> The method returns `true` if all bulk requests completed and `false` if the
  175. waiting time elapsed before all the bulk requests completed
  176. The `close()` method can be used to immediately close the `BulkProcessor`:
  177. ["source","java",subs="attributes,callouts,macros"]
  178. --------------------------------------------------
  179. include-tagged::{doc-tests-file}[{api}-processor-close]
  180. --------------------------------------------------
  181. Both methods flush the requests added to the processor before closing the
  182. processor and also forbid any new request to be added to it.