AI Hints
Home›Elasticsearch Tutorial›Better Ranking and Aggregations
Elasticsearch · Lesson 68 of 120

Aggregations

Numbers, not documents.

⏱ 2 min read▶ Video at 1:04:30🎓 Free
Watch this lesson in the Elasticsearch courseWatch on YouTube ↗Full Elasticsearch course

Searching finds documents. But sometimes you want numbers. How many products per brand? What's the average price? Sales per month? Aggregations answer those questions.

  • Per brand?
  • Average price?
  • Per month?

Your first aggregation

Aggs goes next to the query. We name it avg price, and use an avg aggregation on price. Size zero means, don't return documents, only the numbers. Run it. The average price of all forty nine products.

Dev Tools Consoletext
GET products/_search?filter_path=aggregations
{
  "size": 0,
  "aggs": {
    "avg_price": { "avg": { "field": "price" } }
  }
}
Output
{"aggregations": {"avg_price": {"value": 153.1124}}}
Also uses _load
catalog

Two families

Metric aggregations calculate a number, like average, sum or max. Bucket aggregations group documents into buckets, like one per brand or one per month. And you can put metrics inside buckets.

Metricavg, sum, min, max
Bucketterms, range, date_histogram
Nestedmetrics inside buckets

Summary

Aggregations calculate numbers over documents. Metrics give values, and buckets group documents. Next, the terms aggregation.

Key takeaways
  • aggs
  • size: 0
  • Metrics & buckets
Next up: terms Aggregation — count by value