Bird
Raised Fist0
Elasticsearchquery~20 mins

Metric aggregations (avg, sum, min, max) in Elasticsearch - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Metric Aggregations Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this average aggregation?
Given the following Elasticsearch aggregation query, what is the average value returned for the field price?
Elasticsearch
{
  "size": 0,
  "aggs": {
    "average_price": {
      "avg": {
        "field": "price"
      }
    }
  }
}
A{"aggregations":{"average_price":{"value":25.5}}}
B{"aggregations":{"average_price":{"value":50}}}
C{"aggregations":{"average_price":{"value":0}}}
D{"aggregations":{"average_price":{"value":null}}}
Attempts:
2 left
💡 Hint
The average aggregation calculates the mean of all values in the price field.
❓ Predict Output
intermediate
2:00remaining
What is the sum of the quantity field?
Consider this Elasticsearch aggregation query. What sum value will it return for the quantity field?
Elasticsearch
{
  "size": 0,
  "aggs": {
    "total_quantity": {
      "sum": {
        "field": "quantity"
      }
    }
  }
}
A{"aggregations":{"total_quantity":{"value":null}}}
B{"aggregations":{"total_quantity":{"value":100}}}
C{"aggregations":{"total_quantity":{"value":0}}}
D{"aggregations":{"total_quantity":{"value":50}}}
Attempts:
2 left
💡 Hint
Sum aggregation adds all values of the specified field.
❓ Predict Output
advanced
2:00remaining
What is the minimum value returned by this aggregation?
Analyze this Elasticsearch aggregation query. What minimum value will it return for the rating field?
Elasticsearch
{
  "size": 0,
  "aggs": {
    "min_rating": {
      "min": {
        "field": "rating"
      }
    }
  }
}
A{"aggregations":{"min_rating":{"value":5}}}
B{"aggregations":{"min_rating":{"value":0}}}
C{"aggregations":{"min_rating":{"value":1}}}
D{"aggregations":{"min_rating":{"value":null}}}
Attempts:
2 left
💡 Hint
Minimum aggregation finds the smallest value in the field.
❓ Predict Output
advanced
2:00remaining
What maximum value does this aggregation return?
Given this Elasticsearch aggregation query, what is the maximum value returned for the score field?
Elasticsearch
{
  "size": 0,
  "aggs": {
    "max_score": {
      "max": {
        "field": "score"
      }
    }
  }
}
A{"aggregations":{"max_score":{"value":99}}}
B{"aggregations":{"max_score":{"value":0}}}
C{"aggregations":{"max_score":{"value":null}}}
D{"aggregations":{"max_score":{"value":50}}}
Attempts:
2 left
💡 Hint
Maximum aggregation finds the largest value in the field.
🧠 Conceptual
expert
2:00remaining
Which aggregation returns null value and why?
In Elasticsearch, when running a metric aggregation like avg, sum, min, or max on a field that has no matching documents, what value is returned and why?
AThe aggregation returns the default value of the field type, like 0 for numbers.
BThe aggregation returns 0 because Elasticsearch assumes zero when no data is found.
CThe aggregation throws a runtime error indicating no data found.
DThe aggregation returns <code>null</code> because there are no documents to calculate the metric.
Attempts:
2 left
💡 Hint
Think about what happens when there is no data to aggregate.

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

  1. Step 1: Understand the purpose of avg aggregation

    The avg aggregation calculates the average (mean) of values in a numeric field.
  2. Step 2: Compare with other metric aggregations

    Other aggregations like sum, min, and max calculate total, minimum, and maximum respectively, not average.
  3. Final Answer:

    The average value of a numeric field across documents -> Option A
  4. 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?
easy
A. "aggs": { "total_price": { "sum": { "field": "price" } } }
B. "aggs": { "total_price": { "avg": { "field": "price" } } }
C. "aggs": { "total_price": { "max": { "field": "price" } } }
D. "aggs": { "total_price": { "min": { "field": "price" } } }

Solution

  1. Step 1: Identify the aggregation type for sum

    The aggregation type to calculate sum is sum.
  2. Step 2: Check the syntax structure

    The correct syntax uses "sum": { "field": "price" } inside aggs.
  3. Final Answer:

    "aggs": { "total_price": { "sum": { "field": "price" } } } -> Option A
  4. Quick 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
A. 25
B. 30
C. 15
D. 10

Solution

  1. Step 1: Understand max aggregation

    The max aggregation returns the highest value of the specified field.
  2. Step 2: Identify the maximum price from the list

    From the prices 10, 25, 7, 30, 15, the maximum is 30.
  3. Final Answer:

    30 -> Option B
  4. 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

  1. Step 1: Check the syntax for min aggregation

    The min aggregation requires the field name inside an object with key "field".
  2. Step 2: Identify the error in the given query

    The query uses "min": "age" instead of "min": { "field": "age" } which is incorrect syntax.
  3. Final Answer:

    The field name should be inside a nested object with key "field" -> Option D
  4. 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?
hard
A. { "aggs": { "score_metrics": { "metric": [ { "avg": { "field": "score" } }, { "sum": { "field": "score" } } ] } } }
B. { "aggs": { "score_stats": { "stats": { "field": "score" } } } }
C. { "aggs": { "avg_score": { "avg": { "field": "score" } }, "sum_score": { "sum": { "field": "score" } }, "min_score": { "min": { "field": "score" } }, "max_score": { "max": { "field": "score" } } } }
D. { "aggs": { "score": { "avg": { "field": "score" }, "sum": { "field": "score" }, "min": { "field": "score" }, "max": { "field": "score" } } } }

Solution

  1. Step 1: Understand how to combine multiple metric aggregations

    Each metric aggregation must be a separate named aggregation inside aggs.
  2. 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 field score. { "aggs": { "score_stats": { "stats": { "field": "score" } } } } uses stats which 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.
  3. Final Answer:

    Separate named aggregations for avg, sum, min, and max inside aggs -> Option C
  4. Quick 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 strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard