0
0
Elasticsearchquery~5 mins

Partial updates in Elasticsearch

Choose your learning style9 modes available
Introduction

Partial updates let you change only some parts of a document without sending the whole document again. This saves time and data.

You want to update just one field in a large document.
You need to fix a typo in a user's profile without changing other info.
You want to add a new tag to a blog post without rewriting the whole post.
You want to increment a counter or update a timestamp quickly.
You want to update nested fields inside a document without replacing the entire nested object.
Syntax
Elasticsearch
POST /index_name/_update/document_id
{
  "doc": {
    "field_name": "new_value"
  }
}

The _update API is used for partial updates.

The doc key holds only the fields you want to change.

Examples
This updates the age field of the user with ID 1 to 30.
Elasticsearch
POST /users/_update/1
{
  "doc": {
    "age": 30
  }
}
This updates the price and stock fields of product 42.
Elasticsearch
POST /products/_update/42
{
  "doc": {
    "price": 19.99,
    "stock": 50
  }
}
This replaces the tags array with a new list.
Elasticsearch
POST /articles/_update/100
{
  "doc": {
    "tags": ["news", "update"]
  }
}
Sample Program

This example updates the document with ID 123 in the library index. It changes the author field to "Jane Doe" and sets available to true.

Elasticsearch
POST /library/_update/123
{
  "doc": {
    "author": "Jane Doe",
    "available": true
  }
}
OutputSuccess
Important Notes

If the document does not exist, the update will fail unless you use doc_as_upsert: true to create it.

You can also use scripts for more complex updates, like incrementing a number.

Summary

Partial updates change only some fields of a document.

Use the _update API with the doc key.

This saves bandwidth and time compared to replacing the whole document.