delete-by-query.asciidoc 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. [[java-rest-high-document-delete-by-query]]
  2. === Delete By Query API
  3. [[java-rest-high-document-delete-by-query-request]]
  4. ==== Delete By Query Request
  5. A `DeleteByQueryRequest` can be used to delete documents from an index. It requires an existing index (or a set of indices)
  6. on which deletion is to be performed.
  7. The simplest form of a `DeleteByQueryRequest` looks like:
  8. ["source","java",subs="attributes,callouts,macros"]
  9. --------------------------------------------------
  10. include-tagged::{doc-tests}/CRUDDocumentationIT.java[delete-by-query-request]
  11. --------------------------------------------------
  12. <1> Creates the `DeleteByQueryRequest` on a set of indices.
  13. By default version conflicts abort the `DeleteByQueryRequest` process but you can just count them by settings it to
  14. `proceed` in the request body
  15. ["source","java",subs="attributes,callouts,macros"]
  16. --------------------------------------------------
  17. include-tagged::{doc-tests}/CRUDDocumentationIT.java[delete-by-query-request-conflicts]
  18. --------------------------------------------------
  19. <1> Set `proceed` on version conflict
  20. You can limit the documents by adding a query.
  21. ["source","java",subs="attributes,callouts,macros"]
  22. --------------------------------------------------
  23. include-tagged::{doc-tests}/CRUDDocumentationIT.java[delete-by-query-request-query]
  24. --------------------------------------------------
  25. <1> Only copy documents which have field `user` set to `kimchy`
  26. It’s also possible to limit the number of processed documents by setting size.
  27. ["source","java",subs="attributes,callouts,macros"]
  28. --------------------------------------------------
  29. include-tagged::{doc-tests}/CRUDDocumentationIT.java[delete-by-query-request-size]
  30. --------------------------------------------------
  31. <1> Only copy 10 documents
  32. By default `DeleteByQueryRequest` uses batches of 1000. You can change the batch size with `setBatchSize`.
  33. ["source","java",subs="attributes,callouts,macros"]
  34. --------------------------------------------------
  35. include-tagged::{doc-tests}/CRUDDocumentationIT.java[delete-by-query-request-scrollSize]
  36. --------------------------------------------------
  37. <1> Use batches of 100 documents
  38. `DeleteByQueryRequest` also helps in automatically parallelizing using `sliced-scroll` to
  39. slice on `_uid`. Use `setSlices` to specify the number of slices to use.
  40. ["source","java",subs="attributes,callouts,macros"]
  41. --------------------------------------------------
  42. include-tagged::{doc-tests}/CRUDDocumentationIT.java[delete-by-query-request-slices]
  43. --------------------------------------------------
  44. <1> set number of slices to use
  45. `DeleteByQueryRequest` uses the `scroll` parameter to control how long it keeps the "search context" alive.
  46. ["source","java",subs="attributes,callouts,macros"]
  47. --------------------------------------------------
  48. include-tagged::{doc-tests}/CRUDDocumentationIT.java[delete-by-query-request-scroll]
  49. --------------------------------------------------
  50. <1> set scroll time
  51. If you provide routing then the routing is copied to the scroll query, limiting the process to the shards that match
  52. that routing value.
  53. ["source","java",subs="attributes,callouts,macros"]
  54. --------------------------------------------------
  55. include-tagged::{doc-tests}/CRUDDocumentationIT.java[delete-by-query-request-routing]
  56. --------------------------------------------------
  57. <1> set routing
  58. ==== Optional arguments
  59. In addition to the options above the following arguments can optionally be also provided:
  60. ["source","java",subs="attributes,callouts,macros"]
  61. --------------------------------------------------
  62. include-tagged::{doc-tests}/CRUDDocumentationIT.java[delete-by-query-request-timeout]
  63. --------------------------------------------------
  64. <1> Timeout to wait for the delete by query request to be performed as a `TimeValue`
  65. ["source","java",subs="attributes,callouts,macros"]
  66. --------------------------------------------------
  67. include-tagged::{doc-tests}/CRUDDocumentationIT.java[delete-by-query-request-refresh]
  68. --------------------------------------------------
  69. <1> Refresh index after calling delete by query
  70. ["source","java",subs="attributes,callouts,macros"]
  71. --------------------------------------------------
  72. include-tagged::{doc-tests}/CRUDDocumentationIT.java[delete-by-query-request-indicesOptions]
  73. --------------------------------------------------
  74. <1> Set indices options
  75. [[java-rest-high-document-delete-by-query-sync]]
  76. ==== Synchronous Execution
  77. ["source","java",subs="attributes,callouts,macros"]
  78. --------------------------------------------------
  79. include-tagged::{doc-tests}/CRUDDocumentationIT.java[delete-by-query-execute]
  80. --------------------------------------------------
  81. [[java-rest-high-document-delete-by-query-async]]
  82. ==== Asynchronous Execution
  83. The asynchronous execution of an delete by query request requires both the `DeleteByQueryRequest`
  84. instance and an `ActionListener` instance to be passed to the asynchronous
  85. method:
  86. ["source","java",subs="attributes,callouts,macros"]
  87. --------------------------------------------------
  88. include-tagged::{doc-tests}/CRUDDocumentationIT.java[delete-by-query-execute-async]
  89. --------------------------------------------------
  90. <1> The `DeleteByQueryRequest` to execute and the `ActionListener` to use when
  91. the execution completes
  92. The asynchronous method does not block and returns immediately. Once it is
  93. completed the `ActionListener` is called back using the `onResponse` method
  94. if the execution successfully completed or using the `onFailure` method if
  95. it failed.
  96. A typical listener for `BulkByScrollResponse` looks like:
  97. ["source","java",subs="attributes,callouts,macros"]
  98. --------------------------------------------------
  99. include-tagged::{doc-tests}/CRUDDocumentationIT.java[delete-by-query-execute-listener]
  100. --------------------------------------------------
  101. <1> Called when the execution is successfully completed. The response is
  102. provided as an argument and contains a list of individual results for each
  103. operation that was executed. Note that one or more operations might have
  104. failed while the others have been successfully executed.
  105. <2> Called when the whole `DeleteByQueryRequest` fails. In this case the raised
  106. exception is provided as an argument and no operation has been executed.
  107. [[java-rest-high-document-delete-by-query-execute-listener-response]]
  108. ==== Delete By Query Response
  109. The returned `BulkByScrollResponse` contains information about the executed operations and
  110. allows to iterate over each result as follows:
  111. ["source","java",subs="attributes,callouts,macros"]
  112. --------------------------------------------------
  113. include-tagged::{doc-tests}/CRUDDocumentationIT.java[delete-by-query-response]
  114. --------------------------------------------------
  115. <1> Get total time taken
  116. <2> Check if the request timed out
  117. <3> Get total number of docs processed
  118. <4> Number of docs that were deleted
  119. <5> Number of batches that were executed
  120. <6> Number of skipped docs
  121. <7> Number of version conflicts
  122. <8> Number of times request had to retry bulk index operations
  123. <9> Number of times request had to retry search operations
  124. <10> The total time this request has throttled itself not including the current throttle time if it is currently sleeping
  125. <11> Remaining delay of any current throttle sleep or 0 if not sleeping
  126. <12> Failures during search phase
  127. <13> Failures during bulk index operation