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).
Path parameters
-
The name of the target index. By default, the index is created automatically if it doesn't exist.
-
A unique identifier for the document to be updated.
Query parameters
-
if_primary_term number
Only perform the operation if the document has this primary term.
-
if_seq_no number
Only perform the operation if the document has this sequence number.
-
lang string
The script language.
-
refresh string
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 are
true
,false
, orwait_for
. -
require_alias boolean
If
true
, the destination must be an index alias. -
retry_on_conflict number
The number of times the operation should be retried when a conflict occurs.
-
routing string
A custom value used to route operations to a specific shard.
-
timeout string
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_shards number | string
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 of1
means it waits for each primary shard to be active. -
_source boolean | string | array[string]
If
false
, source retrieval is turned off. You can also specify a comma-separated list of the fields you want to retrieve. -
_source_excludes string | array[string]
The source fields you want to exclude.
-
_source_includes string | array[string]
The source fields you want to retrieve.
Body Required
-
detect_noop boolean
If
true
, theresult
in the response is set tonoop
(no operation) when there are no changes to the document. -
doc object
A partial update to an existing document. If both
doc
andscript
are specified,doc
is ignored.Additional properties are allowed.
-
doc_as_upsert boolean
If
true
, use the contents of 'doc' as the value of 'upsert'. NOTE: Using ingest pipelines withdoc_as_upsert
is not supported. -
script object
Additional properties are allowed.
-
scripted_upsert boolean
If
true
, run the script whether or not the document exists. _source boolean | object
Defines how to fetch a source. Fetching can be disabled entirely, or the source can be filtered.
-
upsert object
If the document does not already exist, the contents of 'upsert' are inserted as a new document. If the document exists, the 'script' is run.
Additional properties are allowed.
curl \
-X POST http://api.example.com/{index}/_update/{id} \
-H "Content-Type: application/json" \
-d '{"detect_noop":true,"doc":{},"doc_as_upsert":true,"script":{"source":"string","id":"string","params":{"additionalProperty1":{},"additionalProperty2":{}},"":"painless","options":{"additionalProperty1":"string","additionalProperty2":"string"}},"scripted_upsert":true,"":true,"upsert":{}}'
{
"detect_noop": true,
"doc": {},
"doc_as_upsert": true,
"script": {
"source": "string",
"id": "string",
"params": {
"additionalProperty1": {},
"additionalProperty2": {}
},
"": "painless",
"options": {
"additionalProperty1": "string",
"additionalProperty2": "string"
}
},
"scripted_upsert": true,
"": true,
"upsert": {}
}
{
"_id": "string",
"_index": "string",
"_primary_term": 42.0,
"result": "created",
"_seq_no": 42.0,
"_shards": {
"failed": 42.0,
"successful": 42.0,
"total": 42.0,
"failures": [
{
"index": "string",
"node": "string",
"reason": {
"type": "string",
"reason": "string",
"stack_trace": "string",
"caused_by": {},
"root_cause": [
{}
],
"suppressed": [
{}
]
},
"shard": 42.0,
"status": "string"
}
],
"skipped": 42.0
},
"_version": 42.0,
"forced_refresh": true,
"get": {
"fields": {
"additionalProperty1": {},
"additionalProperty2": {}
},
"found": true,
"_seq_no": 42.0,
"_primary_term": 42.0,
"_routing": "string",
"_source": {}
}
}