📖 Scenario: You are working with a small dataset of sales amounts in an Elasticsearch index. You want to calculate basic statistics like count, min, max, and average, and then extend it to include sum of squares, variance, and standard deviation.
🎯 Goal: Build Elasticsearch aggregation queries step-by-step to calculate stats and extended_stats on the sales field.
📋 What You'll Learn
Create an aggregation query with stats on the sales field
Add a variable field_name with value sales
Create an aggregation query with extended_stats on the sales field using the variable
Print the final JSON aggregation query
💡 Why This Matters
🌍 Real World
Calculating statistics on sales data helps businesses understand performance, detect trends, and make decisions.
💼 Career
Elasticsearch is widely used for search and analytics. Knowing how to write aggregation queries for stats is valuable for data engineers and backend developers.
Progress0 / 4 steps
1
Create a stats aggregation query
Create a JSON object called stats_query with an aggregation named sales_stats that uses the stats aggregation on the field sales.
Elasticsearch
Hint
Use the stats aggregation inside aggs with the field sales.
2
Add a field name variable
Create a variable called field_name and set it to the string "sales".
Elasticsearch
Hint
Just assign the string "sales" to the variable field_name.
3
Create an extended_stats aggregation using the variable
Create a JSON object called extended_stats_query with an aggregation named sales_extended_stats that uses the extended_stats aggregation on the field stored in the variable field_name.
Elasticsearch
Hint
Use the variable field_name as the value for the field key inside extended_stats.
4
Print the extended_stats query
Write a print statement to display the extended_stats_query JSON object.
Elasticsearch
Hint
Use print(extended_stats_query) to show the final JSON.
Practice
(1/5)
1. What does the stats aggregation in Elasticsearch provide by default?
easy
A. Count, min, max, avg, and sum
B. Variance and standard deviation
C. Sum of squares only
D. Median and mode
Solution
Step 1: Understand the purpose of stats aggregation
The stats aggregation provides basic statistical data about numeric fields.
Step 2: Identify the default metrics included
By default, it returns count, min, max, avg, and sum values.
Final Answer:
Count, min, max, avg, and sum -> Option A
Quick Check:
Stats aggregation = basic stats [OK]
Hint: Stats aggregation gives basic stats like count and sum [OK]
Common Mistakes:
Confusing stats with extended_stats
Thinking stats includes variance
Assuming median is included
2. Which of the following is the correct syntax to add an extended stats aggregation named my_stats on the field price?
B. The root key should be "aggregations" instead of "aggs"
C. extended_stats does not support the field "age"
D. The field key should be "field", not "field_name"
Solution
Step 1: Check the field key in extended_stats
The correct key to specify the field is field, not field_name.
Step 2: Verify other parts of the query
The aggregation name "my_stats" is valid, and "aggs" is the correct root key. The field "age" can be used if it exists.
Final Answer:
The field key should be "field", not "field_name" -> Option D
Quick Check:
Use "field" key for field name [OK]
Hint: Use "field" key, not "field_name" in aggregations [OK]
Common Mistakes:
Using "field_name" instead of "field"
Changing aggregation root key incorrectly
Assuming field name is invalid without checking
5. You want to find the average price and also understand the price spread using variance and standard deviation in one query. Which aggregation setup is best?
hard
A. Use stats aggregation on price
B. Use percentiles aggregation on price
C. Use extended_stats aggregation on price
D. Use terms aggregation on price
Solution
Step 1: Identify metrics needed
You want average, variance, and standard deviation, which describe spread and central tendency.
Step 2: Choose aggregation that provides all metrics
extended_stats provides avg, variance, std_deviation, sum_of_squares, and more in one aggregation.
Step 3: Compare other options
stats lacks variance and std deviation; terms groups data; percentiles gives percentile info, not variance.
Final Answer:
Use extended_stats aggregation on price -> Option C