Update by query API examples
Stack
This page provides examples of how to use the Update by query API.
You can learn how to:
- Run basic update-by-query operations
- Modify documents using scripts or ingest pipelines
- Throttle update operations
- Parallelize updates using manual slicing
- Automate slicing for better performance
- Apply mapping changes to existing documents
The simplest usage of _update_by_query
just performs an update on every document in the data stream or index without changing the source. This is useful to pick up a new property or some other online mapping change.
To update selected documents, specify a query in the request body:
POST my-index-000001/_update_by_query?conflicts=proceed
{ "query": { "term": { "user.id": "kimchy" } } }
- The query must be passed as a value to the
query
key, in the same way as the Search API. You can also use theq
parameter in the same way as the search API.
Update documents in multiple data streams or indices:
POST my-index-000001,my-index-000002/_update_by_query
Limit the update by query operation to shards that a particular routing value:
POST my-index-000001/_update_by_query?routing=1
By default update by query uses scroll batches of 1000. You can change the batch size with the scroll_size
parameter:
POST my-index-000001/_update_by_query?scroll_size=100
Update a document using a unique attribute:
POST my-index-000001/_update_by_query
{ "query": { "term": { "user.id": "kimchy" } }, "max_docs": 1 }
Update by query supports scripts to update the document source. For example, the following request increments the count
field for all documents with a user.id
of kimchy
in my-index-000001
:
POST my-index-000001/_update_by_query
{ "script": { "source": "ctx._source.count++", "lang": "painless" }, "query": { "term": { "user.id": "kimchy" } } }
Note that conflicts=proceed
is not specified in this example. In this case, a version conflict should halt the process so you can handle the failure.
As with the Update API, you can set ctx.op
to change the operation that is performed:
noop
- Set
ctx.op = "noop"
if your script decides that it doesn't have to make any changes. The update by query operation skips updating the document and increments thenoop
counter. delete
- Set
ctx.op = "delete"
if your script decides that the document should be deleted. The update by query operation deletes the document and increments thedeleted
counter.
Update by query only supports index
, noop
, and delete
. Setting ctx.op
to anything else is an error. Setting any other field in ctx
is an error. This API only enables you to modify the source of matching documents, you cannot move them.
Update by query can use the ingest pipelines feature by specifying a pipeline
:
PUT _ingest/pipeline/set-foo
{ "description" : "sets foo", "processors" : [ { "set" : { "field": "foo", "value": "bar" } } ] } POST my-index-000001/_update_by_query?pipeline=set-foo
You can fetch the status of all running update by query requests with the Task API:
GET _tasks?detailed=true&actions=*byquery
The responses looks like:
{ "nodes" : { "r1A2WoRbTwKZ516z6NEs5A" : { "name" : "r1A2WoR", "transport_address" : "127.0.0.1:9300", "host" : "127.0.0.1", "ip" : "127.0.0.1:9300", "attributes" : { "testattr" : "test", "portsfile" : "true" }, "tasks" : { "r1A2WoRbTwKZ516z6NEs5A:36619" : { "node" : "r1A2WoRbTwKZ516z6NEs5A", "id" : 36619, "type" : "transport", "action" : "indices:data/write/update/byquery", "status" : { "total" : 6154, "updated" : 3500, "created" : 0, "deleted" : 0, "batches" : 4, "version_conflicts" : 0, "noops" : 0, "retries": { "bulk": 0, "search": 0 }, "throttled_millis": 0 }, "description" : "" } } } } }
- This object contains the actual status. It is just like the response JSON with the important addition of the
total
field.total
is the total number of operations that the reindex expects to perform. You can estimate the progress by adding theupdated
,created
, anddeleted
fields. The request will finish when their sum is equal to thetotal
field.
With the task id you can look up the task directly. The following example retrieves information about task r1A2WoRbTwKZ516z6NEs5A:36619
:
GET /_tasks/r1A2WoRbTwKZ516z6NEs5A:36619
The advantage of this API is that it integrates with wait_for_completion=false
to transparently return the status of completed tasks. If the task is completed and wait_for_completion=false
was set on it, then it'll come back with a results
or an error
field. The cost of this feature is the document that wait_for_completion=false
creates at .tasks/task/${taskId}
. It is up to you to delete that document.
Any update by query can be cancelled using the Cancel API:
POST _tasks/r1A2WoRbTwKZ516z6NEs5A:36619/_cancel
The task ID can be found using the Task API.
Cancellation should happen quickly but might take a few seconds. The task status API above will continue to list the update by query task until this task checks that it has been cancelled and terminates itself.
The value of requests_per_second
can be changed on a running update by query using the _rethrottle
API:
POST _update_by_query/r1A2WoRbTwKZ516z6NEs5A:36619/_rethrottle?requests_per_second=-1
The task ID can be found using the Task API.
Just like when setting it on the _update_by_query
API, requests_per_second
can be either -1
to disable throttling or any decimal number like 1.7
or 12
to throttle to that level. Rethrottling that speeds up the query takes effect immediately, but rethrotting that slows down the query will take effect after completing the current batch. This prevents scroll timeouts.
Slice an update by query manually by providing a slice id and total number of slices to each request:
POST my-index-000001/_update_by_query
{ "slice": { "id": 0, "max": 2 }, "script": { "source": "ctx._source['extra'] = 'test'" } } POST my-index-000001/_update_by_query { "slice": { "id": 1, "max": 2 }, "script": { "source": "ctx._source['extra'] = 'test'" } }
Which you can verify works with:
GET _refresh
POST my-index-000001/_search?size=0&q=extra:test&filter_path=hits.total
Which results in a sensible total
like this one:
{ "hits": { "total": { "value": 120, "relation": "eq" } } }
You can also let update by query automatically parallelize using slice-scroll to slice on _id
. Use slices
to specify the number of slices to use:
POST my-index-000001/_update_by_query?refresh&slices=5
{ "script": { "source": "ctx._source['extra'] = 'test'" } }
Which you also can verify works with:
POST my-index-000001/_search?size=0&q=extra:test&filter_path=hits.total
Which results in a sensible total
like this one:
{ "hits": { "total": { "value": 120, "relation": "eq" } } }
Setting slices
to auto
will let Elasticsearch choose the number of slices to use. This setting will use one slice per shard, up to a certain limit. If there are multiple source data streams or indices, it will choose the number of slices based on the index or backing index with the smallest number of shards.
Adding slices
to _update_by_query
just automates the manual process used in the section above, creating sub-requests which means it has some quirks:
- You can see these requests in the Tasks APIs. These sub-requests are "child" tasks of the task for the request with
slices
. - Fetching the status of the task for the request with
slices
only contains the status of completed slices. - These sub-requests are individually addressable for things like cancellation and rethrottling.
- Rethrottling the request with
slices
will rethrottle the unfinished sub-request proportionally. - Canceling the request with
slices
will cancel each sub-request. - Due to the nature of
slices
each sub-request won't get a perfectly even portion of the documents. All documents will be addressed, but some slices may be larger than others. Expect larger slices to have a more even distribution. - Parameters like
requests_per_second
andmax_docs
on a request withslices
are distributed proportionally to each sub-request. Combine that with the point above about distribution being uneven and you should conclude that usingmax_docs
withslices
might not result in exactlymax_docs
documents being updated. - Each sub-request gets a slightly different snapshot of the source data stream or index though these are all taken at approximately the same time.
Say you created an index without dynamic mapping, filled it with data, and then added a mapping value to pick up more fields from the data:
PUT test
{ "mappings": { "dynamic": false, "properties": { "text": {"type": "text"} } } } POST test/_doc?refresh { "text": "words words", "flag": "bar" } POST test/_doc?refresh { "text": "words words", "flag": "foo" } PUT test/_mapping { "properties": { "text": {"type": "text"}, "flag": {"type": "text", "analyzer": "keyword"} } }
This means that new fields won't be indexed, just stored in
_source
.This updates the mapping to add the new
flag
field. To pick up the new field you have to reindex all documents with it.
Searching for the data won't find anything:
POST test/_search?filter_path=hits.total
{ "query": { "match": { "flag": "foo" } } }
{ "hits" : { "total": { "value": 0, "relation": "eq" } } }
But you can issue an _update_by_query
request to pick up the new mapping:
POST test/_update_by_query?refresh&conflicts=proceed
POST test/_search?filter_path=hits.total { "query": { "match": { "flag": "foo" } } }
{ "hits" : { "total": { "value": 1, "relation": "eq" } } }
You can do the exact same thing when adding a field to a multifield.