1
- [[docs-update-by-query]]
2
- == Update By Query API
1
+ [[java- docs-update-by-query]]
2
+ === Update By Query API
3
3
4
4
The simplest usage of `updateByQuery` updates each
5
5
document 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.
8
7
9
- [source, java]
8
+ [" source"," java",subs="attributes,callouts,macros" ]
10
9
--------------------------------------------------
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]
16
11
--------------------------------------------------
17
12
18
13
Calls 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,
41
36
limiting the total number of documents to update, and updating documents
42
37
with a script:
43
38
44
- [source,java]
45
- --------------------------------------------------
46
- UpdateByQueryRequestBuilder updateByQuery = UpdateByQueryAction.INSTANCE.newRequestBuilder(client);
47
39
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]
54
43
--------------------------------------------------
55
44
56
45
`UpdateByQueryRequestBuilder` also enables direct access to the query used
57
46
to select the documents. You can use this access to change the default scroll size or
58
47
otherwise modify the request for matching documents.
59
48
60
- [source, java]
49
+ [" source"," java",subs="attributes,callouts,macros" ]
61
50
--------------------------------------------------
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]
68
52
--------------------------------------------------
69
53
70
54
You can also combine `size` with sorting to limit the documents updated:
71
55
72
- [source, java]
56
+ [" source"," java",subs="attributes,callouts,macros" ]
73
57
--------------------------------------------------
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]
80
59
--------------------------------------------------
81
60
82
61
In addition to changing the `_source` field for the document, you can use a
83
62
script to change the action, similar to the Update API:
84
63
85
- [source, java]
64
+ [" source"," java",subs="attributes,callouts,macros" ]
86
65
--------------------------------------------------
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]
99
67
--------------------------------------------------
100
68
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
102
70
operation that executes:
103
71
104
72
`noop`::
105
73
106
74
Set `ctx.op = "noop"` if your script doesn't make any
107
75
changes. 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.
110
77
111
78
`delete`::
112
79
113
80
Set `ctx.op = "delete"` if your script decides that the document must be
114
81
deleted. The deletion will be reported in the `deleted` counter in the
115
- <<docs-update-by-query- response-body, response body>> .
82
+ response body.
116
83
117
84
Setting `ctx.op` to any other value generates an error. Setting any
118
85
other field in `ctx` generates an error.
@@ -123,79 +90,55 @@ from its original location.
123
90
124
91
You can also perform these operations on multiple indices and types at once, similar to the search API:
125
92
126
- [source, java]
93
+ [" source"," java",subs="attributes,callouts,macros" ]
127
94
--------------------------------------------------
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]
133
96
--------------------------------------------------
134
97
135
98
If you provide a `routing` value then the process copies the routing value to the scroll query,
136
99
limiting the process to the shards that match that routing value:
137
100
138
- [source, java]
101
+ [" source"," java",subs="attributes,callouts,macros" ]
139
102
--------------------------------------------------
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]
145
104
--------------------------------------------------
146
105
147
- `updateByQuery` can also use the << ingest>> feature by
106
+ `updateByQuery` can also use the ingest node by
148
107
specifying a `pipeline` like this:
149
108
150
- [source, java]
109
+ [" source"," java",subs="attributes,callouts,macros" ]
151
110
--------------------------------------------------
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]
157
112
--------------------------------------------------
158
113
159
114
[float]
160
- [[docs-update-by-query-task-api]]
115
+ [[java- docs-update-by-query-task-api]]
161
116
=== Works with the Task API
162
117
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:
165
119
166
- [source, java]
120
+ [" source"," java",subs="attributes,callouts,macros" ]
167
121
--------------------------------------------------
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]
177
123
--------------------------------------------------
178
124
179
125
With the `TaskId` shown above you can look up the task directly:
180
126
181
127
// provide API Example
182
- [source, java]
128
+ [" source"," java",subs="attributes,callouts,macros" ]
183
129
--------------------------------------------------
184
- GetTaskResponse get = client.admin().cluster().prepareGetTask(taskId). get();
130
+ include-tagged::{ client-reindex-tests}/ReindexDocumentationIT.java[update-by-query- get-task]
185
131
--------------------------------------------------
186
132
187
133
[float]
188
- [[docs-update-by-query-cancel-task-api]]
134
+ [[java- docs-update-by-query-cancel-task-api]]
189
135
=== Works with the Cancel Task API
190
136
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:
192
138
193
- [source, java]
139
+ [" source"," java",subs="attributes,callouts,macros" ]
194
140
--------------------------------------------------
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]
199
142
--------------------------------------------------
200
143
201
144
Use 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
204
147
The task status API continues to list the task until the cancellation is complete.
205
148
206
149
[float]
207
- [[docs-update-by-query-rethrottle]]
150
+ [[java- docs-update-by-query-rethrottle]]
208
151
=== Rethrottling
209
152
210
153
Use the `_rethrottle` API to change the value of `requests_per_second` on a running update:
211
154
212
- [source, java]
155
+ [" source"," java",subs="attributes,callouts,macros" ]
213
156
--------------------------------------------------
214
- RethrottleAction.INSTANCE.newRequestBuilder( client).setTaskId(taskId).setRequestsPerSecond(2.0f).get();
157
+ include-tagged::{ client-reindex-tests}/ReindexDocumentationIT.java[update-by-query-rethrottle]
215
158
--------------------------------------------------
216
159
217
160
Use the `list tasks` API to find the value of `taskId`.
0 commit comments