Update document

Introduced 1.0

If you need to update a document’s fields in your index, you can use the update document API operation. You can do so by specifying the new data you want to be in your index or by including a script in your request body, which OpenSearch runs to update the document. By default, the update operation only updates a document that exists in the index. If a document does not exist, the API returns an error. To upsert a document (update the document that exists or index a new one), use the upsert operation.

Example

  1. POST /sample-index1/_update/1
  2. {
  3. "doc": {
  4. "first_name" : "Bruce",
  5. "last_name" : "Wayne"
  6. }
  7. }

copy

Script example

  1. POST /test-index1/_update/1
  2. {
  3. "script" : {
  4. "source": "ctx._source.secret_identity = \"Batman\""
  5. }
  6. }

copy

Path and HTTP methods

  1. POST /<index>/_update/<_id>

URL parameters

ParameterTypeDescriptionRequired
<index>StringName of the index.Yes
<_id>StringThe ID of the document to update.Yes
if_seq_noIntegerOnly perform the update operation if the document has the specified sequence number.No
if_primary_termIntegerPerform the update operation if the document has the specified primary term.No
langStringLanguage of the script. Default is painless.No
require_aliasBooleanSpecifies whether the destination must be an index alias. Default is false.No
refreshEnumIf true, OpenSearch refreshes shards to make the operation visible to searching. Valid options are true, false, and wait_for, which tells OpenSearch to wait for a refresh before executing the operation. Default is false.No
retry_on_conflictIntegerThe amount of times OpenSearch should retry the operation if there’s a document conflict. Default is 0.No
routingStringValue to route the update operation to a specific shard.No
_sourceBoolean or ListWhether or not to include the _source field in the response body. Default is false. This parameter also supports a comma-separated list of source fields for including multiple source fields in the query response.No
_source_excludesListA comma-separated list of source fields to exclude in the query response.No
_source_includesListA comma-separated list of source fields to include in the query response.No
timeoutTimeHow long to wait for a response from the cluster.No
wait_for_active_shardsStringThe number of active shards that must be available before OpenSearch processes the update request. Default is 1 (only the primary shard). Set to all or a positive integer. Values greater than 1 require replicas. For example, if you specify a value of 3, the index must have two replicas distributed across two additional nodes for the operation to succeed.No

Request body

Your request body must contain the information with which you want to update your document. If you only want to replace certain fields in your document, your request body must include a doc object containing the fields that you want to update:

  1. {
  2. "doc": {
  3. "first_name": "Thomas",
  4. "last_name": "Wayne"
  5. }
  6. }

You can also use a script to tell OpenSearch how to update your document:

  1. {
  2. "script" : {
  3. "source": "ctx._source.oldValue += params.newValue",
  4. "lang": "painless",
  5. "params" : {
  6. "newValue" : 10
  7. }
  8. }
  9. }

Upsert

Upsert is an operation that conditionally either updates an existing document or inserts a new one based on information in the object.

In the following example, the upsert operation updates the first_name and last_name fields if a document already exists. If a document does not exist, a new one is indexed using content in the upsert object.

  1. POST /sample-index1/_update/1
  2. {
  3. "doc": {
  4. "first_name": "Martha",
  5. "last_name": "Rivera"
  6. },
  7. "upsert": {
  8. "last_name": "Oliveira",
  9. "age": "31"
  10. }
  11. }

Consider an index that contains the following document:

  1. {
  2. "_index": "sample-index1",
  3. "_id": "1",
  4. "_score": 1,
  5. "_source": {
  6. "first_name": "Bruce",
  7. "last_name": "Wayne"
  8. }
  9. }

After the upsert operation, the document’s first_name and last_name fields are updated:

  1. {
  2. "_index": "sample-index1",
  3. "_id": "1",
  4. "_score": 1,
  5. "_source": {
  6. "first_name": "Martha",
  7. "last_name": "Rivera"
  8. }
  9. }

If the document does not exist in the index, a new document is indexed with the fields specified in the upsert object:

  1. {
  2. "_index": "sample-index1",
  3. "_id": "1",
  4. "_score": 1,
  5. "_source": {
  6. "last_name": "Oliveira",
  7. "age": "31"
  8. }
  9. }

You can also add doc_as_upsert to the request and set it to true to use the information in the doc field for performing the upsert operation:

  1. POST /sample-index1/_update/1
  2. {
  3. "doc": {
  4. "first_name": "Martha",
  5. "last_name": "Oliveira",
  6. "age": "31"
  7. },
  8. "doc_as_upsert": true
  9. }

Consider an index that contains the following document:

  1. {
  2. "_index": "sample-index1",
  3. "_id": "1",
  4. "_score": 1,
  5. "_source": {
  6. "first_name": "Bruce",
  7. "last_name": "Wayne"
  8. }
  9. }

After the upsert operation, the document’s first_name and last_name fields are updated and an age field is added. If the document does not exist in the index, a new document is indexed with the fields specified in the upsert object. In both cases, the document is as follows:

  1. {
  2. "_index": "sample-index1",
  3. "_id": "1",
  4. "_score": 1,
  5. "_source": {
  6. "first_name": "Martha",
  7. "last_name": "Oliveira",
  8. "age": "31"
  9. }
  10. }

Response

  1. {
  2. "_index": "sample-index1",
  3. "_id": "1",
  4. "_version": 3,
  5. "result": "updated",
  6. "_shards": {
  7. "total": 2,
  8. "successful": 2,
  9. "failed": 0
  10. },
  11. "_seq_no": 4,
  12. "_primary_term": 17
  13. }

Response body fields

FieldDescription
_indexThe name of the index.
_idThe document’s ID.
_versionThe document’s version.
resultThe result of the update operation.
_shardsDetailed information about the cluster’s shards.
totalThe total number of shards.
successfulThe number of shards OpenSearch successfully updated the document in.
failedThe number of shards OpenSearch failed to update the document in.
_seq_noThe sequence number assigned when the document was indexed.
_primary_termThe primary term assigned when the document was indexed.