bulk.asciidoc 11 KB

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