update-by-query.asciidoc 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. [[java-docs-update-by-query]]
  2. === Update By Query API
  3. The simplest usage of `updateByQuery` updates each
  4. document in an index without changing the source. This usage enables
  5. picking up a new property or another online mapping change.
  6. ["source","java",subs="attributes,callouts,macros"]
  7. --------------------------------------------------
  8. include-tagged::{client-reindex-tests}/ReindexDocumentationIT.java[update-by-query]
  9. --------------------------------------------------
  10. Calls to the `updateByQuery` API start by getting a snapshot of the index, indexing
  11. any documents found using the `internal` versioning.
  12. NOTE: Version conflicts happen when a document changes between the time of the
  13. snapshot and the time the index request processes.
  14. When the versions match, `updateByQuery` updates the document
  15. and increments the version number.
  16. All update and query failures cause `updateByQuery` to abort. These failures are
  17. available from the `BulkByScrollResponse#getIndexingFailures` method. Any
  18. successful updates remain and are not rolled back. While the first failure
  19. causes the abort, the response contains all of the failures generated by the
  20. failed bulk request.
  21. To prevent version conflicts from causing `updateByQuery` to abort, set
  22. `abortOnVersionConflict(false)`. The first example does this because it is
  23. trying to pick up an online mapping change and a version conflict means that
  24. the conflicting document was updated between the start of the `updateByQuery`
  25. and the time when it attempted to update the document. This is fine because
  26. that update will have picked up the online mapping update.
  27. The `UpdateByQueryRequestBuilder` API supports filtering the updated documents,
  28. limiting the total number of documents to update, and updating documents
  29. with a script:
  30. ["source","java",subs="attributes,callouts,macros"]
  31. --------------------------------------------------
  32. include-tagged::{client-reindex-tests}/ReindexDocumentationIT.java[update-by-query-filter]
  33. --------------------------------------------------
  34. `UpdateByQueryRequestBuilder` also enables direct access to the query used
  35. to select the documents. You can use this access to change the default scroll size or
  36. otherwise modify the request for matching documents.
  37. ["source","java",subs="attributes,callouts,macros"]
  38. --------------------------------------------------
  39. include-tagged::{client-reindex-tests}/ReindexDocumentationIT.java[update-by-query-size]
  40. --------------------------------------------------
  41. You can also combine `size` with sorting to limit the documents updated:
  42. ["source","java",subs="attributes,callouts,macros"]
  43. --------------------------------------------------
  44. include-tagged::{client-reindex-tests}/ReindexDocumentationIT.java[update-by-query-sort]
  45. --------------------------------------------------
  46. In addition to changing the `_source` field for the document, you can use a
  47. script to change the action, similar to the Update API:
  48. ["source","java",subs="attributes,callouts,macros"]
  49. --------------------------------------------------
  50. include-tagged::{client-reindex-tests}/ReindexDocumentationIT.java[update-by-query-script]
  51. --------------------------------------------------
  52. As in the <<java-docs-update,Update API>>, you can set the value of `ctx.op` to change the
  53. operation that executes:
  54. `noop`::
  55. Set `ctx.op = "noop"` if your script doesn't make any
  56. changes. The `updateByQuery` operaton then omits that document from the updates.
  57. This behavior increments the `noop` counter in the response body.
  58. `delete`::
  59. Set `ctx.op = "delete"` if your script decides that the document must be
  60. deleted. The deletion will be reported in the `deleted` counter in the
  61. response body.
  62. Setting `ctx.op` to any other value generates an error. Setting any
  63. other field in `ctx` generates an error.
  64. This API doesn't allow you to move the documents it touches, just modify their
  65. source. This is intentional! We've made no provisions for removing the document
  66. from its original location.
  67. You can also perform these operations on multiple indices and types at once, similar to the search API:
  68. ["source","java",subs="attributes,callouts,macros"]
  69. --------------------------------------------------
  70. include-tagged::{client-reindex-tests}/ReindexDocumentationIT.java[update-by-query-multi-index]
  71. --------------------------------------------------
  72. If you provide a `routing` value then the process copies the routing value to the scroll query,
  73. limiting the process to the shards that match that routing value:
  74. ["source","java",subs="attributes,callouts,macros"]
  75. --------------------------------------------------
  76. include-tagged::{client-reindex-tests}/ReindexDocumentationIT.java[update-by-query-routing]
  77. --------------------------------------------------
  78. `updateByQuery` can also use the ingest node by
  79. specifying a `pipeline` like this:
  80. ["source","java",subs="attributes,callouts,macros"]
  81. --------------------------------------------------
  82. include-tagged::{client-reindex-tests}/ReindexDocumentationIT.java[update-by-query-pipeline]
  83. --------------------------------------------------
  84. [float]
  85. [[java-docs-update-by-query-task-api]]
  86. === Works with the Task API
  87. You can fetch the status of all running update-by-query requests with the Task API:
  88. ["source","java",subs="attributes,callouts,macros"]
  89. --------------------------------------------------
  90. include-tagged::{client-reindex-tests}/ReindexDocumentationIT.java[update-by-query-list-tasks]
  91. --------------------------------------------------
  92. With the `TaskId` shown above you can look up the task directly:
  93. // provide API Example
  94. ["source","java",subs="attributes,callouts,macros"]
  95. --------------------------------------------------
  96. include-tagged::{client-reindex-tests}/ReindexDocumentationIT.java[update-by-query-get-task]
  97. --------------------------------------------------
  98. [float]
  99. [[java-docs-update-by-query-cancel-task-api]]
  100. === Works with the Cancel Task API
  101. Any Update By Query can be canceled using the Task Cancel API:
  102. ["source","java",subs="attributes,callouts,macros"]
  103. --------------------------------------------------
  104. include-tagged::{client-reindex-tests}/ReindexDocumentationIT.java[update-by-query-cancel-task]
  105. --------------------------------------------------
  106. Use the `list tasks` API to find the value of `taskId`.
  107. Cancelling a request is typically a very fast process but can take up to a few seconds.
  108. The task status API continues to list the task until the cancellation is complete.
  109. [float]
  110. [[java-docs-update-by-query-rethrottle]]
  111. === Rethrottling
  112. Use the `_rethrottle` API to change the value of `requests_per_second` on a running update:
  113. ["source","java",subs="attributes,callouts,macros"]
  114. --------------------------------------------------
  115. include-tagged::{client-reindex-tests}/ReindexDocumentationIT.java[update-by-query-rethrottle]
  116. --------------------------------------------------
  117. Use the `list tasks` API to find the value of `taskId`.
  118. As with the `updateByQuery` API, the value of `requests_per_second`
  119. can be any positive float value to set the level of the throttle, or `Float.POSITIVE_INFINITY` to disable throttling.
  120. A value of `requests_per_second` that speeds up the process takes
  121. effect immediately. `requests_per_second` values that slow the query take effect
  122. after completing the current batch in order to prevent scroll timeouts.