Metric aggregations help you quickly find numbers like averages, totals, smallest, or biggest values in your data.
Metric aggregations (avg, sum, min, max) in Elasticsearch
Introduction
Syntax
Elasticsearch
{
"aggs": {
"aggregation_name": {
"avg" : {
"field": "field_name"
}
}
}
}Replace aggregation_name with any name you choose to identify the result.
Choose one metric type: avg, sum, min, or max.
Examples
price field.Elasticsearch
{
"aggs": {
"average_price": {
"avg": {
"field": "price"
}
}
}
}sales field.Elasticsearch
{
"aggs": {
"total_sales": {
"sum": {
"field": "sales"
}
}
}
}age field.Elasticsearch
{
"aggs": {
"min_age": {
"min": {
"field": "age"
}
}
}
}score field.Elasticsearch
{
"aggs": {
"max_score": {
"max": {
"field": "score"
}
}
}
}Sample Program
This query finds the average price, total sales, minimum age, and maximum score from all documents.
Elasticsearch
{
"query": {
"match_all": {}
},
"aggs": {
"average_price": {
"avg": {
"field": "price"
}
},
"total_sales": {
"sum": {
"field": "sales"
}
},
"min_age": {
"min": {
"field": "age"
}
},
"max_score": {
"max": {
"field": "score"
}
}
}
}Important Notes
If a field has no values, the aggregation result will be null.
You can combine multiple metric aggregations in one query to get many results at once.
Summary
Metric aggregations calculate numbers like average, sum, min, and max from your data.
Use them to quickly understand key statistics in your Elasticsearch documents.
You write them inside the aggs part of your query with the field name.
Practice
1. What does the
avg metric aggregation calculate in an Elasticsearch query?easy
Solution
Step 1: Understand the purpose of avg aggregation
Theavgaggregation calculates the average (mean) of values in a numeric field.Step 2: Compare with other metric aggregations
Other aggregations likesum,min, andmaxcalculate total, minimum, and maximum respectively, not average.Final Answer:
The average value of a numeric field across documents -> Option AQuick 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?easy
Solution
Step 1: Identify the aggregation type for sum
The aggregation type to calculate sum issum.Step 2: Check the syntax structure
The correct syntax uses"sum": { "field": "price" }insideaggs.Final Answer:
"aggs": { "total_price": { "sum": { "field": "price" } } } -> Option AQuick Check:
sum aggregation syntax = "aggs": { "total_price": { "sum": { "field": "price" } } } [OK]
Hint: sum uses "sum": { "field": "fieldname" } syntax [OK]
Common Mistakes:
- Using avg or max instead of sum
- Missing the field name inside sum
- Wrong nesting of aggregation keys
3. Given this Elasticsearch aggregation query snippet, what will be the output for the
max_price aggregation?
{
"aggs": {
"max_price": {
"max": { "field": "price" }
}
}
}
Assuming the documents have prices: 10, 25, 7, 30, 15.medium
Solution
Step 1: Understand max aggregation
Themaxaggregation 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 BQuick 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
Solution
Step 1: Check the syntax for min aggregation
Theminaggregation 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 DQuick 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?hard
Solution
Step 1: Understand how to combine multiple metric aggregations
Each metric aggregation must be a separate named aggregation insideaggs.Step 2: Analyze each option's structure
{ "aggs": { "avg_score": { "avg": { "field": "score" } }, "sum_score": { "sum": { "field": "score" } }, "min_score": { "min": { "field": "score" } }, "max_score": { "max": { "field": "score" } } } } correctly defines four separate aggregations for avg, sum, min, and max on the fieldscore. { "aggs": { "score_stats": { "stats": { "field": "score" } } } } usesstatswhich returns all metrics but is not separate aggregations. { "aggs": { "score_metrics": { "metric": [ { "avg": { "field": "score" } }, { "sum": { "field": "score" } } ] } } } and D have invalid syntax or unsupported structures.Final Answer:
Separate named aggregations for avg, sum, min, and max inside aggs -> Option CQuick Check:
Multiple metric aggs need separate keys inside aggs [OK]
Hint: Use separate named aggs for each metric under aggs [OK]
Common Mistakes:
- Trying to combine metrics inside one aggregation object
- Using unsupported array or metric keys
- Confusing stats aggregation with separate metric aggs
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
