0
0
Elasticsearchquery~30 mins

Deleting documents in Elasticsearch - Mini Project: Build & Apply

Choose your learning style9 modes available
Deleting documents
📖 Scenario: You manage a small online store's product catalog stored in Elasticsearch. Sometimes, products are discontinued and need to be removed from the catalog.
🎯 Goal: You will learn how to delete documents from an Elasticsearch index using the document ID and a query.
📋 What You'll Learn
Create an index with sample product documents
Set a variable for the product ID to delete
Write the command to delete a document by ID
Write the command to delete documents by a query
Print the deletion responses
💡 Why This Matters
🌍 Real World
Deleting outdated or discontinued products from an online store's Elasticsearch catalog keeps the data clean and relevant.
💼 Career
Knowing how to delete documents by ID or query is essential for managing data lifecycle in Elasticsearch, a common skill for backend developers and data engineers.
Progress0 / 4 steps
1
Create the product index with sample documents
Create an index called products with three documents having these exact IDs and fields:
1: {"name": "Laptop", "price": 1200},
2: {"name": "Smartphone", "price": 800},
3: {"name": "Tablet", "price": 400}.
Use the Elasticsearch index API calls as shown.
Elasticsearch
Need a hint?

Use the POST /products/_doc/{id} endpoint to add each product document.

2
Set the product ID to delete
Create a variable called product_id and set it to the string "2" to represent the product to delete.
Elasticsearch
Need a hint?

In your client or script, assign the string "2" to a variable named product_id.

3
Delete the product document by ID
Write the Elasticsearch DELETE API call to delete the document with ID stored in product_id from the products index.
Elasticsearch
Need a hint?

Use DELETE /products/_doc/{product_id} to delete the document by ID.

4
Delete products by query and print results
Write the Elasticsearch POST API call to delete all products with price less than 1000 using _delete_by_query on the products index.
Then print the response showing how many documents were deleted.
Elasticsearch
Need a hint?

Use POST /products/_delete_by_query with a range query on price less than 1000.