Update by query

Introduced 1.0

You can include a query and a script as part of your update request so OpenSearch can run the script to update all of the documents that match the query.

Path and HTTP methods

  1. POST <index1>, <index2>/_update_by_query

Path parameters

ParameterTypeDescription
<index>StringComma-separated list of indexes to update. To update all indexes, use * or omit this parameter.

Query parameters

All parameters are optional.

ParameterTypeDescription
allow_no_indicesBooleanWhether to ignore wildcards that don’t match any indexes. Default is true.
analyzerStringAnalyzer to use in the query string.
analyze_wildcardBooleanWhether the update operation should include wildcard and prefix queries in the analysis. Default is false.
conflictsStringIndicates to OpenSearch what should happen if the update by query operation runs into a version conflict. Valid options are abort and proceed. Default is abort.
default_operatorStringIndicates whether the default operator for a string query should be AND or OR. Default is OR.
dfStringThe default field if a field prefix is not provided in the query string.
expand_wildcardsStringSpecifies the type of index that wildcard expressions can match. Supports comma-separated values. Valid values are all (match any index), open (match open, non-hidden indexes), closed (match closed, non-hidden indexes), hidden (match hidden indexes), and none (deny wildcard expressions). Default is open.
fromIntegerThe starting index to search from. Default is 0.
ignore_unavailableBooleanWhether to exclude missing or closed indexes in the response and ignores unavailable shards during the search request. Default is false.
lenientBooleanSpecifies whether OpenSearch should accept requests if queries have format errors (for example, querying a text field for an integer). Default is false.
max_docsIntegerHow many documents the update by query operation should process at most. Default is all documents.
pipelineStringID of the pipeline to use to process documents.
preferenceStringSpecifies which shard or node OpenSearch should perform the update by query operation on.
qStringLucene query string’s query.
request_cacheBooleanSpecifies whether OpenSearch should use the request cache. Default is whether it’s enabled in the index’s settings.
refreshBooleanIf true, OpenSearch refreshes shards to make the update by query operation available to search results. Valid options are true and false. Default is false.
requests_per_secondIntegerSpecifies the request’s throttling in sub-requests per second. Default is -1, which means no throttling.
routingStringValue used to route the update by query operation to a specific shard.
scrollTimeHow long to keep the search context open.
scroll_sizeIntegerSize of the operation’s scroll request. Default is 1000.
search_typeStringWhether OpenSearch should use global term and document frequencies calculating relevance scores. Valid choices are query_then_fetch and dfs_query_then_fetch. query_then_fetch scores documents using local term and document frequencies for the shard. It’s usually faster but less accurate. dfs_query_then_fetch scores documents using global term and document frequencies across all shards. It’s usually slower but more accurate. Default is query_then_fetch.
search_timeoutTimeHow long to wait until OpenSearch deems the request timed out. Default is no timeout.
slicesString or integerThe number slices to split an operation into for faster processing, specified by integer. When set to auto OpenSearch it should decides how many the number of slices for the operation. Default is 1, which indicates an operation will not be split.
sortListA comma-separated list of <field> : <direction> pairs to sort by.
_sourceStringWhether to include the _source field in the response.
_source_excludesStringA comma-separated list of source fields to exclude from the response.
_source_includesStringA comma-separated list of source fields to include in the response.
statsStringValue to associate with the request for additional logging.
terminate_afterIntegerThe maximum number of documents OpenSearch should process before terminating the request.
timeoutTimeHow long the operation should wait from a response from active shards. Default is 1m.
versionBooleanWhether to include the document version as a match.
wait_for_active_shardsStringThe number of shards that must be active before OpenSearch executes the operation. Valid values are all or any integer up to the total number of shards in the index. Default is 1, which is the primary shard.
wait_for_completionbooleanWhen set to false, the response body includes a task ID and OpenSearch executes the operation asynchronously. The task ID can be used to check the status of the task or to cancel the task. Default is set to true.

Request body options

To update your indexes and documents by query, you must include a query and a script in the request body that OpenSearch can run to update your documents. If you don’t specify a query, then every document in the index gets updated.

  1. {
  2. "query": {
  3. "term": {
  4. "oldValue": 20
  5. }
  6. },
  7. "script" : {
  8. "source": "ctx._source.oldValue += params.newValue",
  9. "lang": "painless",
  10. "params" : {
  11. "newValue" : 10
  12. }
  13. }
  14. }

Example requests

  1. POST test-index1/_update_by_query
  2. {
  3. "query": {
  4. "term": {
  5. "oldValue": 10
  6. }
  7. },
  8. "script" : {
  9. "source": "ctx._source.oldValue += params.newValue",
  10. "lang": "painless",
  11. "params" : {
  12. "newValue" : 20
  13. }
  14. }
  15. }

copy

Example response

  1. {
  2. "took": 21,
  3. "timed_out": false,
  4. "total": 1,
  5. "updated": 1,
  6. "deleted": 0,
  7. "batches": 1,
  8. "version_conflicts": 0,
  9. "noops": 0,
  10. "retries": {
  11. "bulk": 0,
  12. "search": 0
  13. },
  14. "throttled_millis": 0,
  15. "requests_per_second": -1.0,
  16. "throttled_until_millis": 0,
  17. "failures": []
  18. }

Response body fields

FieldDescription
tookThe amount of time in milliseconds OpenSearch needed to complete the operation.
timed_outWhether any update requests during the operation timed out.
totalTotal number of documents processed.
updatedTotal number of documents updated.
batchesNumber of scroll responses the request processed.
version_conflictsNumber of conflicts the request ran into.
noopsHow many update requests OpenSearch ignored during the operation. This field always returns 0.
retriesThe number of bulk and search retry requests.
throttled_millisNumber of throttled milliseconds during the request.
requests_per_secondNumber of requests executed per second during the operation.
throttled_until_millisThe amount of time until OpenSearch executes the next throttled request. Always equal to 0 in an update by query request.
failuresAny failures that occur during the request.