bulk.asciidoc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. [[java-rest-high-document-bulk]]
  2. === Bulk API
  3. [[java-rest-high-document-bulk-request]]
  4. ==== Bulk Request
  5. A `BulkRequest` can be used to execute multiple index, update and/or delete
  6. operations using a single request.
  7. It requires at least one operation to be added to the Bulk request:
  8. ["source","java",subs="attributes,callouts,macros"]
  9. --------------------------------------------------
  10. include-tagged::{doc-tests}/CRUDDocumentationIT.java[bulk-request]
  11. --------------------------------------------------
  12. <1> Creates the `BulkRequest`
  13. <2> Adds a first `IndexRequest` to the Bulk request. See <<java-rest-high-document-index>>
  14. for more information on how to build `IndexRequest`.
  15. <3> Adds a second `IndexRequest`
  16. <4> Adds a third `IndexRequest`
  17. WARNING: The Bulk API supports only documents encoded in JSON or SMILE. Providing documents
  18. in any other format will result in an error.
  19. And different operation types can be added to the same `BulkRequest`:
  20. ["source","java",subs="attributes,callouts,macros"]
  21. --------------------------------------------------
  22. include-tagged::{doc-tests}/CRUDDocumentationIT.java[bulk-request-with-mixed-operations]
  23. --------------------------------------------------
  24. <1> Adds a `DeleteRequest` to the `BulkRequest`. See <<java-rest-high-document-delete>>
  25. for more information on how to build `DeleteRequest`.
  26. <2> Adds an `UpdateRequest` to the `BulkRequest`. See <<java-rest-high-document-update>>
  27. for more information on how to build `UpdateRequest`.
  28. <3> Adds an `IndexRequest` using the SMILE format
  29. ==== Optional arguments
  30. The following arguments can optionally be provided:
  31. ["source","java",subs="attributes,callouts,macros"]
  32. --------------------------------------------------
  33. include-tagged::{doc-tests}/CRUDDocumentationIT.java[bulk-request-timeout]
  34. --------------------------------------------------
  35. <1> Timeout to wait for the bulk request to be performed as a `TimeValue`
  36. <2> Timeout to wait for the bulk request to be performed as a `String`
  37. ["source","java",subs="attributes,callouts,macros"]
  38. --------------------------------------------------
  39. include-tagged::{doc-tests}/CRUDDocumentationIT.java[bulk-request-refresh]
  40. --------------------------------------------------
  41. <1> Refresh policy as a `WriteRequest.RefreshPolicy` instance
  42. <2> Refresh policy as a `String`
  43. ["source","java",subs="attributes,callouts,macros"]
  44. --------------------------------------------------
  45. include-tagged::{doc-tests}/CRUDDocumentationIT.java[bulk-request-active-shards]
  46. --------------------------------------------------
  47. <1> Sets the number of shard copies that must be active before proceeding with
  48. the index/update/delete operations.
  49. <2> Number of shard copies provided as a `ActiveShardCount`: can be `ActiveShardCount.ALL`,
  50. `ActiveShardCount.ONE` or `ActiveShardCount.DEFAULT` (default)
  51. [[java-rest-high-document-bulk-sync]]
  52. ==== Synchronous Execution
  53. ["source","java",subs="attributes,callouts,macros"]
  54. --------------------------------------------------
  55. include-tagged::{doc-tests}/CRUDDocumentationIT.java[bulk-execute]
  56. --------------------------------------------------
  57. [[java-rest-high-document-bulk-async]]
  58. ==== Asynchronous Execution
  59. ["source","java",subs="attributes,callouts,macros"]
  60. --------------------------------------------------
  61. include-tagged::{doc-tests}/CRUDDocumentationIT.java[bulk-execute-async]
  62. --------------------------------------------------
  63. <1> Called when the execution is successfully completed. The response is
  64. provided as an argument and contains a list of individual results for each
  65. operation that was executed. Note that one or more operations might have
  66. failed while the others have been successfully executed.
  67. <2> Called when the whole `BulkRequest` fails. In this case the raised
  68. exception is provided as an argument and no operation has been executed.
  69. [[java-rest-high-document-bulk-response]]
  70. ==== Bulk Response
  71. The returned `BulkResponse` contains information about the executed operations and
  72. allows to iterate over each result as follows:
  73. ["source","java",subs="attributes,callouts,macros"]
  74. --------------------------------------------------
  75. include-tagged::{doc-tests}/CRUDDocumentationIT.java[bulk-response]
  76. --------------------------------------------------
  77. <1> Iterate over the results of all operations
  78. <2> Retrieve the response of the operation (successful or not), can be `IndexResponse`,
  79. `UpdateResponse` or `DeleteResponse` which can all be seen as `DocWriteResponse` instances
  80. <3> Handle the response of an index operation
  81. <4> Handle the response of a update operation
  82. <5> Handle the response of a delete operation
  83. The Bulk response provides a method to quickly check if one or more operation has failed:
  84. ["source","java",subs="attributes,callouts,macros"]
  85. --------------------------------------------------
  86. include-tagged::{doc-tests}/CRUDDocumentationIT.java[bulk-has-failures]
  87. --------------------------------------------------
  88. <1> This method returns `true` if at least one operation failed
  89. In such situation it is necessary to iterate over all operation results in order to check
  90. if the operation failed, and if so, retrieve the corresponding failure:
  91. ["source","java",subs="attributes,callouts,macros"]
  92. --------------------------------------------------
  93. include-tagged::{doc-tests}/CRUDDocumentationIT.java[bulk-errors]
  94. --------------------------------------------------
  95. <1> Indicate if a given operation failed
  96. <2> Retrieve the failure of the failed operation