📖 Scenario: You are managing a collection of books in an Elasticsearch index. Sometimes, you need to update only certain fields of a book without replacing the entire document.
🎯 Goal: Learn how to perform partial updates on documents in Elasticsearch using the _update API.
📋 What You'll Learn
Create an index with a sample book document
Prepare the _update API call for document ID 1
Write a partial update script to change specific fields
Print the update response to verify success
💡 Why This Matters
🌍 Real World
Partial updates let you change only some parts of a document without sending the whole data again. This saves time and bandwidth.
💼 Career
Many jobs working with search engines or databases require updating records efficiently. Knowing partial updates in Elasticsearch is a valuable skill.
Progress0 / 4 steps
1
Create the initial book document
Create an index called library and add a document with ID 1 containing these fields: title as "The Great Gatsby", author as "F. Scott Fitzgerald", and year as 1925.
Elasticsearch
Hint
Use the PUT method with the index name library and document ID 1.
2
Prepare the _update API call
Use the _update API endpoint for document ID 1 with an empty doc object.
Elasticsearch
Hint
Use the _update API with the document ID 1.
3
Write the partial update to change the year
Use the _update API on the library index and document ID 1 to change the year field to 1926 using the doc parameter.
Elasticsearch
Hint
Inside doc, specify the field year with the new value 1926.
4
Print the update response
Print the response from the update request to confirm the partial update was successful.
Elasticsearch
Hint
When you run the update command, Elasticsearch returns a JSON response showing "result": "updated" if successful.
Practice
(1/5)
1. What is the main purpose of using partial updates in Elasticsearch?
easy
A. To delete a document completely from the index
B. To create a new document with all fields specified
C. To update only specific fields of a document without replacing the whole document
D. To retrieve documents matching a query
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 C
Quick 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
A. POST /products/_doc/1 { "price": 20 }
B. POST /products/_update/1 { "doc": { "price": 20 } }
C. PUT /products/_update/1 { "price": 20 }
D. GET /products/_update/1 { "doc": { "price": 20 } }
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 B
Quick 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':
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
A. {"name": "Alice", "age": 30}
B. {"age": 30}
C. {"name": "Alice"}
D. {"name": "Alice", "age": 25, "updated": true}
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 A
Quick 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:
POST /orders/_update/5
{
"doc": {
"status": "shipped"
}
}
But you get an error. Which is the most likely cause?
medium
A. The document with ID '5' does not exist in 'orders' index
B. You used GET instead of POST
C. You forgot to include the 'doc' key in the body
D. The 'status' field cannot be updated
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 A
Quick 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?