Update a document
Update a document by running a script or passing a partial document.
If the Elasticsearch security features are enabled, you must have the index or write index privilege for the target index or index alias.
The script can update, delete, or skip modifying the document. The API also supports passing a partial document, which is merged into the existing document. To fully replace an existing document, use the index API. This operation:
- Gets the document (collocated with the shard) from the index.
- Runs the specified script.
- Indexes the result.
The document must still be reindexed, but using this API removes some network roundtrips and reduces chances of version conflicts between the GET and the index operation.
The _source field must be enabled to use this API.
In addition to _source, you can access the following variables through the ctx map: _index, _type, _id, _version, _routing, and _now (the current timestamp).
Required authorization
- Index privileges write
Query parameters
-
include_source_on_errorboolean · default: true -
True or false if to include the document source in the error message in case of parsing errors.
-
refresh· default: false -
If 'true', Elasticsearch refreshes the affected shards to make this operation visible to search. If 'wait_for', it waits for a refresh to make this operation visible to search. If 'false', it does nothing with refreshes.
-
Values:
truefalsewait_for -
retry_on_conflictnumber · default: 0 -
The number of times the operation should be retried when a conflict occurs.
-
timeoutstring · default: 1m -
The period to wait for the following operations: dynamic mapping updates and waiting for active shards. Elasticsearch waits for at least the timeout period before failing. The actual wait time could be longer, particularly when multiple waits occur.
-
wait_for_active_shardsunion · default: 1 -
The number of copies of each shard that must be active before proceeding with the operation. Set to 'all' or any positive integer up to the total number of shards in the index (
number_of_replicas+1). The default value of1means it waits for each primary shard to be active. -
Values:
allindex-setting
Request body
-
detect_noopboolean · default: true -
If
true, theresultin the response is set tonoop(no operation) when there are no changes to the document. -
docobject -
A partial update to an existing document. If both
docandscriptare specified,docis ignored. -
doc_as_upsertboolean · default: false -
If
true, use the contents of 'doc' as the value of 'upsert'. NOTE: Using ingest pipelines withdoc_as_upsertis not supported. -
scripted_upsertboolean · default: false -
If
true, run the script whether or not the document exists.
Response
-
resultrequired -
The result of the indexing operation:
createdorupdated. -
Values:
createdupdateddeletednot_foundnoop -
_seq_nonumber -
The sequence number assigned to the document for the indexing operation. Sequence numbers are used to ensure an older version of a document doesn't overwrite a newer version.
-
_versionnumberrequired -
The document version, which is incremented each time the document is updated.