0
0
Elasticsearchquery~10 mins

Stats and extended stats in Elasticsearch - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Stats and extended stats
Start Query
Select Field
Apply Stats Aggregation
Calculate Basic Stats
Calculate Extended Stats
Return Results
The query starts by selecting a field, then applies stats aggregation to calculate basic and extended statistics, and finally returns the results.
Execution Sample
Elasticsearch
{
  "aggs": {
    "stats_example": {
      "extended_stats": { "field": "price" }
    }
  }
}
This Elasticsearch query calculates extended statistics on the 'price' field.
Execution Table
StepActionFieldCalculationResult
1Start aggregationpriceN/AN/A
2Calculate countpriceCount all documents with price5
3Calculate minpriceFind minimum price10
4Calculate maxpriceFind maximum price50
5Calculate avgpriceCalculate average price30
6Calculate sumpriceSum all price values150
7Calculate sum_of_squarespriceSum of each price squared5500
8Calculate variancepriceVariance of price values250
9Calculate std_deviationpriceStandard deviation of price15.81
10Return all statspriceAll calculated stats{"count":5,"min":10,"max":50,"avg":30,"sum":150,"sum_of_squares":5500,"variance":250,"std_deviation":15.81}
💡 All extended stats calculated and returned for the 'price' field.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5After Step 6After Step 7After Step 8After Step 9Final
count0555555555
min1010101010101010
max-∞-∞-∞50505050505050
avgN/AN/AN/AN/A303030303030
sum00000150150150150150
sum_of_squares0000005500550055005500
varianceN/AN/AN/AN/AN/AN/AN/A250250250
std_deviationN/AN/AN/AN/AN/AN/AN/AN/A15.8115.81
Key Moments - 3 Insights
Why do we calculate sum_of_squares before variance?
Sum_of_squares is needed to compute variance; as shown in steps 7 and 8 of the execution_table, variance calculation depends on sum_of_squares.
What does 'count' represent in the stats aggregation?
Count is the number of documents with the field present, as shown in step 2 of the execution_table where count becomes 5.
Why is standard deviation calculated after variance?
Standard deviation is the square root of variance, so variance must be calculated first (step 8), then std_deviation is computed (step 9).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'max' after step 4?
A50
B10
C30
D5
💡 Hint
Check the 'max' variable value in the execution_table row for step 4.
At which step is the average ('avg') calculated?
AStep 3
BStep 5
CStep 7
DStep 9
💡 Hint
Look at the 'avg' calculation in the execution_table rows.
If the count was 10 instead of 5, which variable would change in the variable_tracker?
Amin
Bstd_deviation
Ccount
Dmax
💡 Hint
Refer to the 'count' variable changes in the variable_tracker.
Concept Snapshot
Stats and extended stats aggregation in Elasticsearch:
- Use 'stats' or 'extended_stats' aggregation on a numeric field.
- Calculates count, min, max, avg, sum (basic stats).
- Extended stats add sum_of_squares, variance, std_deviation.
- Useful for quick numeric summaries in search results.
- Syntax: {"aggs": {"name": {"extended_stats": {"field": "fieldname"}}}}
Full Transcript
This visual execution trace shows how Elasticsearch calculates stats and extended stats on a numeric field. The process starts by selecting the field, then counts documents with that field, finds minimum and maximum values, calculates average and sum, then computes sum_of_squares, variance, and standard deviation. Each step updates variables tracked in the variable_tracker. Key moments clarify why sum_of_squares is needed before variance and why standard deviation follows variance. The visual quiz tests understanding of variable values at specific steps and how changes affect results. The concept snapshot summarizes how to use stats and extended_stats aggregations in Elasticsearch queries.