📖 Scenario: You work for an online store that tracks sales data in Elasticsearch. You want to analyze sales performance by calculating average, total, minimum, and maximum sales amounts.
🎯 Goal: Build an Elasticsearch query that uses metric aggregations to find the average, sum, minimum, and maximum of the sales_amount field.
📋 What You'll Learn
Create an aggregation named average_sales that calculates the average of sales_amount.
Create an aggregation named total_sales that calculates the sum of sales_amount.
Create an aggregation named min_sales that finds the minimum sales_amount.
Create an aggregation named max_sales that finds the maximum sales_amount.
💡 Why This Matters
🌍 Real World
Companies use Elasticsearch metric aggregations to quickly analyze large datasets for business insights like sales performance, customer behavior, and system metrics.
💼 Career
Knowing how to write metric aggregation queries is essential for roles like data analyst, backend developer, and search engineer working with Elasticsearch.
Progress0 / 4 steps
1
Setup the base query structure
Create a JSON object called query with an empty aggs field to hold aggregations.
Elasticsearch
Hint
Start with an empty aggs object inside your query JSON.
2
Add average and sum aggregations
Inside the aggs field, add two aggregations: average_sales using avg on sales_amount, and total_sales using sum on sales_amount.
Elasticsearch
Hint
Use avg and sum aggregation types with the field set to sales_amount.
3
Add minimum and maximum aggregations
Add two more aggregations inside aggs: min_sales using min on sales_amount, and max_sales using max on sales_amount.
Elasticsearch
Hint
Use min and max aggregation types with the field set to sales_amount.
4
Complete the Elasticsearch query
Wrap the aggs object inside a full Elasticsearch query JSON with a size of 0 to only return aggregation results.
Elasticsearch
Hint
Set size to 0 to avoid returning documents and focus on aggregation results.
Practice
(1/5)
1. What does the avg metric aggregation calculate in an Elasticsearch query?
easy
A. The average value of a numeric field across documents
B. The total count of documents matching the query
C. The maximum value of a numeric field
D. The minimum value of a numeric field
Solution
Step 1: Understand the purpose of avg aggregation
The avg aggregation calculates the average (mean) of values in a numeric field.
Step 2: Compare with other metric aggregations
Other aggregations like sum, min, and max calculate total, minimum, and maximum respectively, not average.
Final Answer:
The average value of a numeric field across documents -> Option A
Quick Check:
avg = average value [OK]
Hint: avg means average value, not count or min/max [OK]
Common Mistakes:
Confusing avg with sum or count
Thinking avg returns min or max values
Mixing avg with document count
2. Which of the following is the correct syntax to calculate the sum of the field price in an Elasticsearch aggregation?
Assuming the documents have prices: 10, 25, 7, 30, 15.
medium
A. 25
B. 30
C. 15
D. 10
Solution
Step 1: Understand max aggregation
The max aggregation returns the highest value of the specified field.
Step 2: Identify the maximum price from the list
From the prices 10, 25, 7, 30, 15, the maximum is 30.
Final Answer:
30 -> Option B
Quick Check:
max price = 30 [OK]
Hint: max returns the highest number in the field values [OK]
Common Mistakes:
Choosing the second highest value instead of max
Confusing max with avg or sum
Ignoring the actual field values
4. Identify the error in this Elasticsearch aggregation query that tries to calculate the minimum value of the field age:
{
"aggs": {
"min_age": {
"min": "age"
}
}
}
medium
A. The query is missing a "query" section
B. The aggregation name "min_age" is invalid
C. The aggregation type should be "max" instead of "min"
D. The field name should be inside a nested object with key "field"
Solution
Step 1: Check the syntax for min aggregation
The min aggregation requires the field name inside an object with key "field".
Step 2: Identify the error in the given query
The query uses "min": "age" instead of "min": { "field": "age" } which is incorrect syntax.
Final Answer:
The field name should be inside a nested object with key "field" -> Option D
Quick Check:
min aggregation needs "field": "age" [OK]
Hint: Field must be inside { "field": "name" } for metric aggs [OK]
Common Mistakes:
Passing field name as string directly without object
Confusing aggregation type names
Assuming query section is mandatory for aggregation
5. You want to find the average, sum, minimum, and maximum values of the field score using the avg, sum, min, and max aggregations in a single Elasticsearch query. Which aggregation structure correctly achieves this?