1- [[docs-update-by-query]]
2- == Update By Query API
1+ [[java- docs-update-by-query]]
2+ === Update By Query API
33
44The simplest usage of `updateByQuery` updates each
55document in an index without changing the source. This usage enables
6- <<picking-up-a-new-property,picking up a new property>> or another online
7- mapping change.
6+ picking up a new property or another online mapping change.
87
9- [source, java]
8+ [" source"," java",subs="attributes,callouts,macros" ]
109--------------------------------------------------
11- UpdateByQueryRequestBuilder updateByQuery = UpdateByQueryAction.INSTANCE.newRequestBuilder(client);
12-
13- updateByQuery.source("source_index").abortOnVersionConflict(false);
14-
15- BulkByScrollResponse response = updateByQuery.get();
10+ include-tagged::{client-reindex-tests}/ReindexDocumentationIT.java[update-by-query]
1611--------------------------------------------------
1712
1813Calls to the `updateByQuery` API start by getting a snapshot of the index, indexing
@@ -41,78 +36,50 @@ The `UpdateByQueryRequestBuilder` API supports filtering the updated documents,
4136limiting the total number of documents to update, and updating documents
4237with a script:
4338
44- [source,java]
45- --------------------------------------------------
46- UpdateByQueryRequestBuilder updateByQuery = UpdateByQueryAction.INSTANCE.newRequestBuilder(client);
4739
48- updateByQuery.source("source_index")
49- .filter(termQuery("level", "awesome"))
50- .size(1000)
51- .script(new Script("ctx._source.awesome = 'absolutely'", ScriptType.INLINE, "painless", emptyMap()));
52-
53- BulkByScrollResponse response = updateByQuery.get();
40+ ["source","java",subs="attributes,callouts,macros"]
41+ --------------------------------------------------
42+ include-tagged::{client-reindex-tests}/ReindexDocumentationIT.java[update-by-query-filter]
5443--------------------------------------------------
5544
5645`UpdateByQueryRequestBuilder` also enables direct access to the query used
5746to select the documents. You can use this access to change the default scroll size or
5847otherwise modify the request for matching documents.
5948
60- [source, java]
49+ [" source"," java",subs="attributes,callouts,macros" ]
6150--------------------------------------------------
62- UpdateByQueryRequestBuilder updateByQuery = UpdateByQueryAction.INSTANCE.newRequestBuilder(client);
63-
64- updateByQuery.source("source_index")
65- .source().setSize(500);
66-
67- BulkByScrollResponse response = updateByQuery.get();
51+ include-tagged::{client-reindex-tests}/ReindexDocumentationIT.java[update-by-query-size]
6852--------------------------------------------------
6953
7054You can also combine `size` with sorting to limit the documents updated:
7155
72- [source, java]
56+ [" source"," java",subs="attributes,callouts,macros" ]
7357--------------------------------------------------
74- UpdateByQueryRequestBuilder updateByQuery = UpdateByQueryAction.INSTANCE.newRequestBuilder(client);
75-
76- updateByQuery.source("source_index").size(100)
77- .source().addSort("cat", SortOrder.DESC);
78-
79- BulkByScrollResponse response = updateByQuery.get();
58+ include-tagged::{client-reindex-tests}/ReindexDocumentationIT.java[update-by-query-sort]
8059--------------------------------------------------
8160
8261In addition to changing the `_source` field for the document, you can use a
8362script to change the action, similar to the Update API:
8463
85- [source, java]
64+ [" source"," java",subs="attributes,callouts,macros" ]
8665--------------------------------------------------
87- UpdateByQueryRequestBuilder updateByQuery = UpdateByQueryAction.INSTANCE.newRequestBuilder(client);
88-
89- updateByQuery.source("source_index")
90- .script(new Script(
91- "if (ctx._source.awesome == 'absolutely) {"
92- + " ctx.op='noop'
93- + "} else if (ctx._source.awesome == 'lame') {"
94- + " ctx.op='delete'"
95- + "} else {"
96- + "ctx._source.awesome = 'absolutely'}", ScriptType.INLINE, "painless", emptyMap()));
97-
98- BulkByScrollResponse response = updateByQuery.get();
66+ include-tagged::{client-reindex-tests}/ReindexDocumentationIT.java[update-by-query-script]
9967--------------------------------------------------
10068
101- As in the <<docs-update,Update API>>, you can set the value of `ctx.op` to change the
69+ As in the <<java- docs-update,Update API>>, you can set the value of `ctx.op` to change the
10270operation that executes:
10371
10472`noop`::
10573
10674Set `ctx.op = "noop"` if your script doesn't make any
10775changes. The `updateByQuery` operaton then omits that document from the updates.
108- This behavior increments the `noop` counter in the
109- <<docs-update-by-query-response-body, response body>>.
76+ This behavior increments the `noop` counter in the response body.
11077
11178`delete`::
11279
11380Set `ctx.op = "delete"` if your script decides that the document must be
11481deleted. The deletion will be reported in the `deleted` counter in the
115- <<docs-update-by-query- response-body, response body>> .
82+ response body.
11683
11784Setting `ctx.op` to any other value generates an error. Setting any
11885other field in `ctx` generates an error.
@@ -123,79 +90,55 @@ from its original location.
12390
12491You can also perform these operations on multiple indices and types at once, similar to the search API:
12592
126- [source, java]
93+ [" source"," java",subs="attributes,callouts,macros" ]
12794--------------------------------------------------
128- UpdateByQueryRequestBuilder updateByQuery = UpdateByQueryAction.INSTANCE.newRequestBuilder(client);
129-
130- updateByQuery.source("foo", "bar").source().setTypes("a", "b");
131-
132- BulkByScrollResponse response = updateByQuery.get();
95+ include-tagged::{client-reindex-tests}/ReindexDocumentationIT.java[update-by-query-multi-index]
13396--------------------------------------------------
13497
13598If you provide a `routing` value then the process copies the routing value to the scroll query,
13699limiting the process to the shards that match that routing value:
137100
138- [source, java]
101+ [" source"," java",subs="attributes,callouts,macros" ]
139102--------------------------------------------------
140- UpdateByQueryRequestBuilder updateByQuery = UpdateByQueryAction.INSTANCE.newRequestBuilder(client);
141-
142- updateByQuery.source().setRouting("cat");
143-
144- BulkByScrollResponse response = updateByQuery.get();
103+ include-tagged::{client-reindex-tests}/ReindexDocumentationIT.java[update-by-query-routing]
145104--------------------------------------------------
146105
147- `updateByQuery` can also use the << ingest>> feature by
106+ `updateByQuery` can also use the ingest node by
148107specifying a `pipeline` like this:
149108
150- [source, java]
109+ [" source"," java",subs="attributes,callouts,macros" ]
151110--------------------------------------------------
152- UpdateByQueryRequestBuilder updateByQuery = UpdateByQueryAction.INSTANCE.newRequestBuilder(client);
153-
154- updateByQuery.setPipeline("hurray");
155-
156- BulkByScrollResponse response = updateByQuery.get();
111+ include-tagged::{client-reindex-tests}/ReindexDocumentationIT.java[update-by-query-pipeline]
157112--------------------------------------------------
158113
159114[float]
160- [[docs-update-by-query-task-api]]
115+ [[java- docs-update-by-query-task-api]]
161116=== Works with the Task API
162117
163- You can fetch the status of all running update-by-query requests with the
164- <<tasks,Task API>>:
118+ You can fetch the status of all running update-by-query requests with the Task API:
165119
166- [source, java]
120+ [" source"," java",subs="attributes,callouts,macros" ]
167121--------------------------------------------------
168- ListTasksResponse tasksList = client.admin().cluster().prepareListTasks()
169- .setActions(UpdateByQueryAction.NAME).setDetailed(true).get();
170-
171- for (TaskInfo info: tasksList.getTasks()) {
172- TaskId taskId = info.getTaskId();
173- BulkByScrollTask.Status status = (BulkByScrollTask.Status) info.getStatus();
174- // do stuff
175- }
176-
122+ include-tagged::{client-reindex-tests}/ReindexDocumentationIT.java[update-by-query-list-tasks]
177123--------------------------------------------------
178124
179125With the `TaskId` shown above you can look up the task directly:
180126
181127// provide API Example
182- [source, java]
128+ [" source"," java",subs="attributes,callouts,macros" ]
183129--------------------------------------------------
184- GetTaskResponse get = client.admin().cluster().prepareGetTask(taskId). get();
130+ include-tagged::{ client-reindex-tests}/ReindexDocumentationIT.java[update-by-query- get-task]
185131--------------------------------------------------
186132
187133[float]
188- [[docs-update-by-query-cancel-task-api]]
134+ [[java- docs-update-by-query-cancel-task-api]]
189135=== Works with the Cancel Task API
190136
191- Any Update By Query can be canceled using the <<tasks, Task Cancel API>> :
137+ Any Update By Query can be canceled using the Task Cancel API:
192138
193- [source, java]
139+ [" source"," java",subs="attributes,callouts,macros" ]
194140--------------------------------------------------
195- // Cancel all update-by-query requests
196- client.admin().cluster().prepareCancelTasks().setActions(UpdateByQueryAction.NAME).get().getTasks()
197- // Cancel a specific update-by-query request
198- client.admin().cluster().prepareCancelTasks().setTaskId(taskId).get().getTasks()
141+ include-tagged::{client-reindex-tests}/ReindexDocumentationIT.java[update-by-query-cancel-task]
199142--------------------------------------------------
200143
201144Use the `list tasks` API to find the value of `taskId`.
@@ -204,14 +147,14 @@ Cancelling a request is typically a very fast process but can take up to a few s
204147The task status API continues to list the task until the cancellation is complete.
205148
206149[float]
207- [[docs-update-by-query-rethrottle]]
150+ [[java- docs-update-by-query-rethrottle]]
208151=== Rethrottling
209152
210153Use the `_rethrottle` API to change the value of `requests_per_second` on a running update:
211154
212- [source, java]
155+ [" source"," java",subs="attributes,callouts,macros" ]
213156--------------------------------------------------
214- RethrottleAction.INSTANCE.newRequestBuilder( client).setTaskId(taskId).setRequestsPerSecond(2.0f).get();
157+ include-tagged::{ client-reindex-tests}/ReindexDocumentationIT.java[update-by-query-rethrottle]
215158--------------------------------------------------
216159
217160Use the `list tasks` API to find the value of `taskId`.
0 commit comments