Aggregations
Turn the matching documents into counts, averages and buckets in the same request.
Open this lesson in the learning hubKey points
- An aggregation summarises whatever the query matched, and travels in the same request as the hits.
- Bucket aggregations group documents; metric aggregations compute a number per group.
- Set
"size": 0when you only want the numbers - it skips the fetch phase completely. - A
termsaggregation returns the top 10 by default and is approximate across shards. doc_count_error_upper_boundquantifies that error; raiseshard_sizeto shrink it.cardinalityis a HyperLogLog++ estimate, exact only below the default threshold of 3000.
Example
GET /articles/_search
{
"size": 0,
"query": { "term": { "status": "published" } },
"aggs": {
"by_tag": {
"terms": { "field": "tags", "size": 10 },
"aggs": { "avg_reads": { "avg": { "field": "read_count" } } }
}
}
}
Filter first, use size 0 when you want only numbers, and treat terms counts as approximate.
This is a reading copy. The full lesson — with the visual explainer, the interactive lab and a Run button for the code — lives in the Elasticsearch Course course, and every lesson in it is listed on the Elasticsearch Course contents page.