bulk.asciidoc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. ["source","java",subs="attributes,callouts,macros"]
  61. --------------------------------------------------
  62. include-tagged::{doc-tests}/CRUDDocumentationIT.java[bulk-execute-async]
  63. --------------------------------------------------
  64. <1> Called when the execution is successfully completed. The response is
  65. provided as an argument and contains a list of individual results for each
  66. operation that was executed. Note that one or more operations might have
  67. failed while the others have been successfully executed.
  68. <2> Called when the whole `BulkRequest` fails. In this case the raised
  69. exception is provided as an argument and no operation has been executed.
  70. [[java-rest-high-document-bulk-response]]
  71. ==== Bulk Response
  72. The returned `BulkResponse` contains information about the executed operations and
  73. allows to iterate over each result as follows:
  74. ["source","java",subs="attributes,callouts,macros"]
  75. --------------------------------------------------
  76. include-tagged::{doc-tests}/CRUDDocumentationIT.java[bulk-response]
  77. --------------------------------------------------
  78. <1> Iterate over the results of all operations
  79. <2> Retrieve the response of the operation (successful or not), can be `IndexResponse`,
  80. `UpdateResponse` or `DeleteResponse` which can all be seen as `DocWriteResponse` instances
  81. <3> Handle the response of an index operation
  82. <4> Handle the response of a update operation
  83. <5> Handle the response of a delete operation
  84. The Bulk response provides a method to quickly check if one or more operation has failed:
  85. ["source","java",subs="attributes,callouts,macros"]
  86. --------------------------------------------------
  87. include-tagged::{doc-tests}/CRUDDocumentationIT.java[bulk-has-failures]
  88. --------------------------------------------------
  89. <1> This method returns `true` if at least one operation failed
  90. In such situation it is necessary to iterate over all operation results in order to check
  91. if the operation failed, and if so, retrieve the corresponding failure:
  92. ["source","java",subs="attributes,callouts,macros"]
  93. --------------------------------------------------
  94. include-tagged::{doc-tests}/CRUDDocumentationIT.java[bulk-errors]
  95. --------------------------------------------------
  96. <1> Indicate if a given operation failed
  97. <2> Retrieve the failure of the failed operation
  98. [[java-rest-high-document-bulk-processor]]
  99. ==== Bulk Processor
  100. The `BulkProcessor` simplifies the usage of the Bulk API by providing
  101. a utility class that allows index/update/delete operations to be
  102. transparently executed as they are added to the processor.
  103. In order to execute the requests, the `BulkProcessor` requires 3 components:
  104. `RestHighLevelClient`:: This client is used to execute the `BulkRequest`
  105. and to retrieve the `BulkResponse`
  106. `BulkProcessor.Listener`:: This listener is called before and after
  107. every `BulkRequest` execution or when a `BulkRequest` failed
  108. `ThreadPool`:: The `BulkRequest` executions are done using threads from this
  109. pool, allowing the `BulkProcessor` to work in a non-blocking manner and to
  110. accept new index/update/delete requests while bulk requests are executing.
  111. Then the `BulkProcessor.Builder` class can be used to build a new `BulkProcessor`:
  112. ["source","java",subs="attributes,callouts,macros"]
  113. --------------------------------------------------
  114. include-tagged::{doc-tests}/CRUDDocumentationIT.java[bulk-processor-init]
  115. --------------------------------------------------
  116. <1> Create the `ThreadPool` using the given `Settings`
  117. <2> Create the `BulkProcessor.Listener`
  118. <3> This method is called before each execution of a `BulkRequest`
  119. <4> This method is called after each execution of a `BulkRequest`
  120. <5> This method is called when a `BulkRequest` failed
  121. <6> Create the `BulkProcessor` by calling the `build()` method from
  122. the `BulkProcessor.Builder`. The `RestHighLevelClient.bulkAsync()`
  123. method will be used to execute the `BulkRequest` under the hood.
  124. The `BulkProcessor.Builder` provides methods to configure how the `BulkProcessor`
  125. should handle requests execution:
  126. ["source","java",subs="attributes,callouts,macros"]
  127. --------------------------------------------------
  128. include-tagged::{doc-tests}/CRUDDocumentationIT.java[bulk-processor-options]
  129. --------------------------------------------------
  130. <1> Set when to flush a new bulk request based on the number of
  131. actions currently added (defaults to 1000, use -1 to disable it)
  132. <2> Set when to flush a new bulk request based on the size of
  133. actions currently added (defaults to 5Mb, use -1 to disable it)
  134. <3> Set the number of concurrent requests allowed to be executed
  135. (default to 1, use 0 to only allow the execution of a single request)
  136. <4> Set a flush interval flushing any `BulkRequest` pending if the
  137. interval passes (defaults to not set)
  138. <5> Set a constant back off policy that initially waits for 1 second
  139. and retries up to 3 times. See `BackoffPolicy.noBackoff()`,
  140. `BackoffPolicy.constantBackoff()` and `BackoffPolicy.exponentialBackoff()`
  141. for more options.
  142. Once the `BulkProcessor` is created requests can be added to it:
  143. ["source","java",subs="attributes,callouts,macros"]
  144. --------------------------------------------------
  145. include-tagged::{doc-tests}/CRUDDocumentationIT.java[bulk-processor-add]
  146. --------------------------------------------------
  147. The requests will be executed by the `BulkProcessor`, which takes care of
  148. calling the `BulkProcessor.Listener` for every bulk request.
  149. The listener provides methods to access to the `BulkRequest` and the `BulkResponse`:
  150. ["source","java",subs="attributes,callouts,macros"]
  151. --------------------------------------------------
  152. include-tagged::{doc-tests}/CRUDDocumentationIT.java[bulk-processor-listener]
  153. --------------------------------------------------
  154. <1> Called before each execution of a `BulkRequest`, this method allows
  155. to know the number of operations that are going to be executed within the `BulkRequest`
  156. <2> Called after each execution of a `BulkRequest`, this method allows
  157. to know if the `BulkResponse` contains errors
  158. <3> Called if the `BulkRequest` failed, this method allows to know
  159. the failure
  160. Once all requests have been added to the `BulkProcessor`, its instance needs to
  161. be closed closed using one of the two available closing methods.
  162. The `awaitClose()` method can be used to wait until all requests have been processed
  163. or the specified waiting time elapses:
  164. ["source","java",subs="attributes,callouts,macros"]
  165. --------------------------------------------------
  166. include-tagged::{doc-tests}/CRUDDocumentationIT.java[bulk-processor-await]
  167. --------------------------------------------------
  168. <1> The method returns `true` if all bulk requests completed and `false` if the
  169. waiting time elapsed before all the bulk requests completed
  170. The `close()` method can be used to immediately close the `BulkProcessor`:
  171. ["source","java",subs="attributes,callouts,macros"]
  172. --------------------------------------------------
  173. include-tagged::{doc-tests}/CRUDDocumentationIT.java[bulk-processor-close]
  174. --------------------------------------------------
  175. Both methods flush the requests added to the processor before closing the processor
  176. and also forbid any new request to be added to it.