0
0
Elasticsearchquery~30 mins

Runtime fields in Elasticsearch - Mini Project: Build & Apply

Choose your learning style9 modes available
Using Runtime Fields in Elasticsearch
📖 Scenario: You have a collection of products indexed in Elasticsearch. Each product has a price and quantity field. You want to calculate the total_value of each product (price multiplied by quantity) on the fly without changing the original data.
🎯 Goal: Create a runtime field called total_value that calculates the product of price and quantity for each document. Then, query the index to show the name and total_value for each product.
📋 What You'll Learn
Create an index mapping with fields name (keyword), price (double), and quantity (integer).
Add a runtime field total_value that multiplies price and quantity.
Index at least 3 product documents with exact values.
Query the index to return name and total_value fields.
💡 Why This Matters
🌍 Real World
Runtime fields help you add new calculated data to your Elasticsearch queries without reindexing your data. This is useful when you want quick insights or temporary calculations.
💼 Career
Many jobs working with Elasticsearch require knowledge of runtime fields to optimize queries and add dynamic data without downtime or heavy reindexing.
Progress0 / 4 steps
1
Create the index with product data
Create an index called products with the following documents exactly: {"name": "Pen", "price": 1.5, "quantity": 10}, {"name": "Notebook", "price": 3.0, "quantity": 5}, and {"name": "Eraser", "price": 0.5, "quantity": 20}. Use the _bulk API to add these documents.
Elasticsearch
Need a hint?

Use the _bulk API with { "index": {} } lines before each document.

2
Add a runtime field for total value
Update the products index mapping to add a runtime field called total_value of type double. This field should calculate doc['price'].value * doc['quantity'].value.
Elasticsearch
Need a hint?

Use the PUT /products/_mapping API with a runtime field named total_value and a script that multiplies price and quantity.

3
Query products with runtime field
Write a search query on the products index that returns the name and the runtime field total_value for each product. Use _source to include name and runtime_mappings to include total_value.
Elasticsearch
Need a hint?

Use _source to get name, add runtime_mappings for total_value, and request fields to show total_value.

4
Display the product names and total values
Print the name and total_value for each product from the search results. Format each line as: Product: <name>, Total Value: <total_value>.
Elasticsearch
Need a hint?

Print each product's name and total_value exactly as shown.