| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 | [[java-docs-update-by-query]]=== Update By Query APIThe simplest usage of `updateByQuery` updates eachdocument in an index without changing the source. This usage enablespicking up a new property or another online mapping change.["source","java",subs="attributes,callouts,macros"]--------------------------------------------------include-tagged::{client-reindex-tests}/ReindexDocumentationIT.java[update-by-query]--------------------------------------------------Calls to the `updateByQuery` API start by getting a snapshot of the index, indexingany documents found using the `internal` versioning.NOTE: Version conflicts happen when a document changes between the time of thesnapshot and the time the index request processes.When the versions match, `updateByQuery` updates the documentand increments the version number.All update and query failures cause `updateByQuery` to abort. These failures areavailable from the `BulkByScrollResponse#getIndexingFailures` method. Anysuccessful updates remain and are not rolled back. While the first failurecauses the abort, the response contains all of the failures generated by thefailed bulk request.To prevent version conflicts from causing `updateByQuery` to abort, set`abortOnVersionConflict(false)`. The first example does this because it istrying to pick up an online mapping change and a version conflict means thatthe conflicting document was updated between the start of the `updateByQuery`and the time when it attempted to update the document. This is fine becausethat update will have picked up the online mapping update.The `UpdateByQueryRequestBuilder` API supports filtering the updated documents,limiting the total number of documents to update, and updating documentswith a script:["source","java",subs="attributes,callouts,macros"]--------------------------------------------------include-tagged::{client-reindex-tests}/ReindexDocumentationIT.java[update-by-query-filter]--------------------------------------------------`UpdateByQueryRequestBuilder` also enables direct access to the query usedto select the documents. You can use this access to change the default scroll size orotherwise modify the request for matching documents.["source","java",subs="attributes,callouts,macros"]--------------------------------------------------include-tagged::{client-reindex-tests}/ReindexDocumentationIT.java[update-by-query-size]--------------------------------------------------You can also combine `size` with sorting to limit the documents updated:["source","java",subs="attributes,callouts,macros"]--------------------------------------------------include-tagged::{client-reindex-tests}/ReindexDocumentationIT.java[update-by-query-sort]--------------------------------------------------In addition to changing the `_source` field for the document, you can use ascript to change the action, similar to the Update API:["source","java",subs="attributes,callouts,macros"]--------------------------------------------------include-tagged::{client-reindex-tests}/ReindexDocumentationIT.java[update-by-query-script]--------------------------------------------------As in the <<java-docs-update,Update API>>, you can set the value of `ctx.op` to change theoperation that executes:`noop`::Set `ctx.op = "noop"` if your script doesn't make anychanges. The `updateByQuery` operaton then omits that document from the updates.This behavior increments the `noop` counter in the response body.`delete`::Set `ctx.op = "delete"` if your script decides that the document must bedeleted. The deletion will be reported in the `deleted` counter in theresponse body.Setting `ctx.op` to any other value generates an error. Setting anyother field in `ctx` generates an error.This API doesn't allow you to move the documents it touches, just modify theirsource. This is intentional! We've made no provisions for removing the documentfrom its original location.You can also perform these operations on multiple indices and types at once, similar to the search API:["source","java",subs="attributes,callouts,macros"]--------------------------------------------------include-tagged::{client-reindex-tests}/ReindexDocumentationIT.java[update-by-query-multi-index]--------------------------------------------------If you provide a `routing` value then the process copies the routing value to the scroll query,limiting the process to the shards that match that routing value:["source","java",subs="attributes,callouts,macros"]--------------------------------------------------include-tagged::{client-reindex-tests}/ReindexDocumentationIT.java[update-by-query-routing]--------------------------------------------------`updateByQuery` can also use the ingest node byspecifying a `pipeline` like this:["source","java",subs="attributes,callouts,macros"]--------------------------------------------------include-tagged::{client-reindex-tests}/ReindexDocumentationIT.java[update-by-query-pipeline]--------------------------------------------------[float][[java-docs-update-by-query-task-api]]=== Works with the Task APIYou can fetch the status of all running update-by-query requests with the Task API:["source","java",subs="attributes,callouts,macros"]--------------------------------------------------include-tagged::{client-reindex-tests}/ReindexDocumentationIT.java[update-by-query-list-tasks]--------------------------------------------------With the `TaskId` shown above you can look up the task directly:// provide API Example["source","java",subs="attributes,callouts,macros"]--------------------------------------------------include-tagged::{client-reindex-tests}/ReindexDocumentationIT.java[update-by-query-get-task]--------------------------------------------------[float][[java-docs-update-by-query-cancel-task-api]]=== Works with the Cancel Task APIAny Update By Query can be canceled using the Task Cancel API:["source","java",subs="attributes,callouts,macros"]--------------------------------------------------include-tagged::{client-reindex-tests}/ReindexDocumentationIT.java[update-by-query-cancel-task]--------------------------------------------------Use the `list tasks` API to find the value of `taskId`.Cancelling a request is typically a very fast process but can take up to a few seconds.The task status API continues to list the task until the cancellation is complete.[float][[java-docs-update-by-query-rethrottle]]=== RethrottlingUse the `_rethrottle` API to change the value of `requests_per_second` on a running update:["source","java",subs="attributes,callouts,macros"]--------------------------------------------------include-tagged::{client-reindex-tests}/ReindexDocumentationIT.java[update-by-query-rethrottle]--------------------------------------------------Use the `list tasks` API to find the value of `taskId`.As with the `updateByQuery` API, the value of `requests_per_second`can be any positive float value to set the level of the throttle, or `Float.POSITIVE_INFINITY` to disable throttling.A value of `requests_per_second` that speeds up the process takeseffect immediately. `requests_per_second` values that slow the query take effectafter completing the current batch in order to prevent scroll timeouts.
 |