delete-by-query.asciidoc 7.3 KB

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