📖 Scenario: You are managing a small online store's product catalog stored in Elasticsearch. Sometimes, product details like price or stock need to be updated.
🎯 Goal: Learn how to update existing documents in Elasticsearch using the _update API with a script.
📋 What You'll Learn
Create an index with sample product documents
Set a variable for the product ID to update
Write an update request to change the product's price and stock
Print a success message to confirm the update
💡 Why This Matters
🌍 Real World
Updating product details like price and stock is common in online stores to keep information accurate.
💼 Career
Knowing how to update documents in Elasticsearch is important for roles involving search engines, data indexing, and real-time data updates.
Progress0 / 4 steps
1
Create the products index with sample documents
Create an index called products with two documents having IDs 1 and 2. Document 1 should have name as "Laptop", price as 1200, and stock as 30. Document 2 should have name as "Smartphone", price as 800, and stock as 50.
Elasticsearch
Hint
Use the _bulk API to add multiple documents at once with their IDs.
2
Set the product ID to update
Create a variable called product_id and set it to the string "1" to specify the product to update.
Elasticsearch
Hint
Just assign the string "1" to the variable product_id.
3
Write the update request to change price and stock
Write an Elasticsearch _update request for the document with ID 1. Use a script to set price to 1100 and stock to 25.
Elasticsearch
Hint
Use the _update API with a script that sets ctx._source.price and ctx._source.stock to new values.
4
Print the update response
Print a success message to confirm the document was updated.
Elasticsearch
Hint
Use a print statement to show a success message after the update.
Practice
(1/5)
1. What does the _update API in Elasticsearch do?
easy
A. It retrieves a document by its ID.
B. It modifies specific fields of an existing document without replacing the whole document.
C. It creates a new index with updated settings.
D. It deletes the entire document from the index.
Solution
Step 1: Understand the purpose of _update API
The _update API is designed to change parts of a document without replacing the entire document.
Step 2: Compare options with the API function
Only It modifies specific fields of an existing document without replacing the whole document. correctly describes modifying specific fields without full replacement, while others describe unrelated actions.
Final Answer:
It modifies specific fields of an existing document without replacing the whole document. -> Option B
Quick Check:
_update API modifies parts of a document [OK]
Hint: Remember _update changes parts, not whole documents [OK]
Common Mistakes:
Thinking _update deletes documents
Confusing _update with document retrieval
Assuming _update creates new indexes
2. Which of the following is the correct syntax to update the field status to "active" in a document with ID 1 in index users?
easy
A. PUT /users/_update/1 { "status": "active" }
B. GET /users/_update/1 { "doc": { "status": "active" } }
C. POST /users/_update/1 { "doc": { "status": "active" } }
D. POST /users/_doc/1 { "status": "active" }
Solution
Step 1: Identify the correct HTTP method and endpoint
The update API uses POST method with the path /index/_update/document_id.
Step 2: Check the request body format
The fields to update must be inside the doc object. POST /users/_update/1 { "doc": { "status": "active" } } matches this format exactly.
Final Answer:
POST /users/_update/1 { "doc": { "status": "active" } } -> Option C
Quick Check:
Use POST with _update and doc object [OK]
Hint: Use POST with _update and put fields inside doc [OK]
Common Mistakes:
Using GET instead of POST for update
Omitting the doc wrapper in the body
Using _doc endpoint instead of _update
3. Given this update request:
POST /products/_update/42
{
"doc": {
"price": 19.99
}
}
What happens to the other fields of the document with ID 42?
medium
A. They remain unchanged; only the price field is updated.
B. They are deleted and replaced by only the price field.
C. The update request will fail due to missing fields.
D. All fields are reset to default values.
Solution
Step 1: Understand partial update behavior
The _update API updates only the fields inside the doc object, leaving other fields intact.
Step 2: Analyze the given request
Only the price field is specified, so other fields stay as they were.
Final Answer:
They remain unchanged; only the price field is updated. -> Option A
Quick Check:
_update changes specified fields only [OK]
Hint: Update changes only fields inside doc, others stay same [OK]
Common Mistakes:
Assuming update replaces whole document
Expecting update to fail without all fields
Thinking update resets other fields
4. You try to update a document but get an error. Which of these is the most likely cause?
POST /orders/_update/100
{
"status": "shipped"
}
medium
A. Using POST instead of PUT method.
B. The index name is incorrect.
C. Document ID 100 does not exist.
D. Missing the doc wrapper around the fields to update.
Solution
Step 1: Check the update request format
The update body must have a doc object wrapping the fields to update.
Step 2: Identify the error in the request
The request body lacks the doc wrapper, causing a syntax error.
Final Answer:
Missing the doc wrapper around the fields to update. -> Option D