Partial updates let you change only some parts of a document without sending the whole document again. This saves time and data.
Partial updates in Elasticsearch
Introduction
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
age field of the user with ID 1 to 30.Elasticsearch
POST /users/_update/1 { "doc": { "age": 30 } }
price and stock fields of product 42.Elasticsearch
POST /products/_update/42 { "doc": { "price": 19.99, "stock": 50 } }
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 } }
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.
Practice
1. What is the main purpose of using partial updates in Elasticsearch?
easy
Solution
Step 1: Understand partial update concept
Partial updates allow changing only some fields of a document instead of replacing the entire document.Step 2: Identify the main purpose
This saves bandwidth and time by sending only the changed fields, not the whole document.Final Answer:
To update only specific fields of a document without replacing the whole document -> Option CQuick Check:
Partial update = update some fields only [OK]
Hint: Partial updates change only some fields, not whole document [OK]
Common Mistakes:
- Confusing partial update with document deletion
- Thinking partial update creates new documents
- Believing partial update retrieves documents
2. Which of the following is the correct syntax to perform a partial update on a document with ID '1' in index 'products'?
easy
Solution
Step 1: Identify correct HTTP method and endpoint
Partial updates use POST method on the _update API with the document ID.Step 2: Check request body format
The update body must include a "doc" key with the fields to update.Final Answer:
POST /products/_update/1 { "doc": { "price": 20 } } -> Option BQuick Check:
Use POST + _update + doc key [OK]
Hint: Use POST with _update and doc key for partial updates [OK]
Common Mistakes:
- Using GET instead of POST for update
- Omitting the 'doc' key in request body
- Using PUT on _update endpoint incorrectly
- Updating with _doc endpoint instead of _update
3. Given this partial update request on document ID '2' in index 'users':
If the original document was {"name": "Alice", "age": 25}, what will be the document after the update?
POST /users/_update/2
{
"doc": {
"age": 30
}
}If the original document was {"name": "Alice", "age": 25}, what will be the document after the update?
medium
Solution
Step 1: Understand partial update effect
The partial update changes only the "age" field to 30, keeping other fields unchanged.Step 2: Apply update to original document
Original document {"name": "Alice", "age": 25} becomes {"name": "Alice", "age": 30} after update.Final Answer:
{"name": "Alice", "age": 30} -> Option AQuick Check:
Partial update changes only specified fields [OK]
Hint: Partial update changes only given fields, others stay same [OK]
Common Mistakes:
- Thinking update replaces whole document
- Assuming fields not in doc are removed
- Adding extra fields not in update request
4. You try this partial update:
But you get an error. Which is the most likely cause?
POST /orders/_update/5
{
"doc": {
"status": "shipped"
}
}But you get an error. Which is the most likely cause?
medium
Solution
Step 1: Check common update errors
Partial update errors often happen if the document ID does not exist.Step 2: Analyze given request and error cause
The syntax is correct, so the likely cause is missing document with ID '5'.Final Answer:
The document with ID '5' does not exist in 'orders' index -> Option AQuick Check:
Missing document ID causes update error [OK]
Hint: Check if document ID exists before updating [OK]
Common Mistakes:
- Confusing HTTP method errors with missing document
- Assuming all fields are immutable
- Omitting 'doc' key causing syntax errors
5. You want to increase the 'stock' field by 10 in a product document with ID '10' using a partial update. Which request body correctly achieves this?
hard
Solution
Step 1: Understand increment operation in partial update
To increase a numeric field, use a script that adds the value to the existing field.Step 2: Identify correct syntax for scripted update
{ "script": { "source": "ctx._source.stock += params.count", "params": { "count": 10 } } } uses the 'script' key with source code to add 10 to 'stock'.Final Answer:
{ "script": { "source": "ctx._source.stock += params.count", "params": { "count": 10 } } } -> Option DQuick Check:
Use script to increment numeric fields [OK]
Hint: Use script with ctx._source to increment fields [OK]
Common Mistakes:
- Trying to update with doc key using expression string
- Using 'update' key instead of 'doc' or 'script'
- Assigning value directly instead of incrementing
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
