0
0
Elasticsearchquery~10 mins

Metric aggregations (avg, sum, min, max) in Elasticsearch - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Metric aggregations (avg, sum, min, max)
Start Query
Select Metric Aggregation
Elasticsearch Processes Data
Calculate Metric (avg, sum, min, max)
Return Aggregation Result
Display Result
The flow shows how a metric aggregation query is sent to Elasticsearch, which processes the data and returns the calculated metric result.
Execution Sample
Elasticsearch
{
  "aggs": {
    "average_price": { "avg": { "field": "price" } }
  }
}
This query calculates the average value of the 'price' field in the documents.
Execution Table
StepActionFieldAggregation TypeIntermediate ResultFinal Result
1Start query----
2Select aggregationpriceavg--
3Elasticsearch scans documentspriceavgCollect values: [10, 20, 30, 40]-
4Calculate aggregationpriceavg-Average = (10+20+30+40)/4 = 25
5Return resultpriceavg-25
💡 All documents processed, average calculated and returned.
Variable Tracker
VariableStartAfter Step 3After Step 4Final
values_collected[][10, 20, 30, 40][10, 20, 30, 40][10, 20, 30, 40]
aggregation_resultnullnull2525
Key Moments - 3 Insights
Why does Elasticsearch collect all values before calculating the average?
Because the average needs all values to sum and count them, as shown in execution_table step 3 and 4.
What happens if the field has missing values in some documents?
Elasticsearch ignores missing values in the aggregation calculation, so only existing values are used, as implied in step 3.
Can we use the same flow for sum, min, and max aggregations?
Yes, the flow is similar; only the calculation step changes to sum, min, or max instead of average.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the intermediate collected values at step 3?
A[25]
B[10, 20, 30, 40]
C[]
D[5, 15, 25]
💡 Hint
Check the 'Intermediate Result' column at step 3 in the execution_table.
At which step is the final average calculated?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look for the step where 'Average = (10+20+30+40)/4 = 25' appears in the execution_table.
If the field had missing values, how would the 'values_collected' change after step 3?
AIt would only include existing values, ignoring missing ones
BIt would include nulls for missing values
CIt would be empty
DIt would double the values
💡 Hint
Refer to key_moments explanation about missing values and step 3 in execution_table.
Concept Snapshot
Metric aggregations in Elasticsearch calculate summary values like avg, sum, min, max.
Syntax: { "aggs": { "name": { "avg": { "field": "fieldname" } } } }
Elasticsearch collects field values, then computes the metric.
Missing values are ignored in calculations.
Same flow applies for sum, min, max with different calculation steps.
Full Transcript
This visual execution trace shows how Elasticsearch performs metric aggregations such as average, sum, min, and max. The process starts with a query selecting the aggregation type and field. Elasticsearch scans documents, collects the field values, and then calculates the metric. For example, the average is computed by summing all collected values and dividing by their count. Missing values in documents are ignored during aggregation. The final result is returned and displayed. This flow is similar for sum, min, and max aggregations, differing only in the calculation step.