0
0
Elasticsearchquery~30 mins

Updating documents in Elasticsearch - Mini Project: Build & Apply

Choose your learning style9 modes available
Updating documents in Elasticsearch
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use a print statement to show a success message after the update.