What Is an Aggregation in Elasticsearch?

What Is an Aggregation in Elasticsearch?

A beginner-friendly, no-code walkthrough of how Elasticsearch moves beyond "find me matching documents" to answer questions like "how many," "what's the average," and "break this down by category."

Imagine walking into a large retail store and asking a staff member, “can you show me every red jacket you have?” That’s a simple search — find the matching items. Now imagine a completely different kind of question: “how many jackets did we sell last month, broken down by color, and what was the average price for each color?” That’s not a search for individual items anymore — it’s a request for a summary, a report built by grouping and calculating across the entire dataset. Elasticsearch’s answer to this second kind of question is called an aggregation, and it’s just as central to what makes Elasticsearch powerful as its search capabilities. This post explains what aggregations are, the different kinds available, and how to use them effectively.

1What Exactly Is an Aggregation?

Let’s define it clearly before exploring the different types.

The simple definition

An aggregation is a way of summarizing, grouping, or calculating statistics across a set of documents, rather than simply listing out individual matching documents one by one. Instead of returning a list of items, an aggregation returns computed answers — counts, averages, groupings by category, and more — built from the underlying data.

Simple Analogy

Think of a search as flipping through a filing cabinet to pull out specific folders that match a description. An aggregation, on the other hand, is like asking an accountant to go through every folder in the cabinet and hand you back a summary report — totals, averages, and breakdowns — without necessarily showing you a single individual folder.

i
Good to Know

Aggregations and search aren’t mutually exclusive — a single request can search for specific matching documents and calculate an aggregation over just those matches at the same time.

2Search vs. Aggregation: Two Different Questions

Understanding this distinction is the key to knowing when to reach for aggregations.

AspectSearchAggregation
Typical question“Which documents match this condition?”“What do the numbers look like across these documents?”
ReturnsA list of individual matching documentsComputed summaries — counts, averages, groupings
Example“Show me all orders from last week”“How many orders were placed each day last week?”
!
Common Misconception

Aggregations aren’t just “search with extra math.” They fundamentally answer a different kind of question — about patterns and summaries across many documents, rather than identifying specific individual documents.

3The Three Main Families of Aggregations

Nearly every aggregation you’ll encounter falls into one of three broad categories.

Family

Bucket Aggregations

Group documents into buckets based on shared criteria, like a category, a date range, or a price range.

Family

Metric Aggregations

Calculate a single number, or a small set of numbers, from a field’s values — like an average, sum, minimum, or maximum.

Family

Pipeline Aggregations

Take the output of other aggregations as their input, calculating further derived values, like a moving average across buckets.

Simple Analogy

Bucket aggregations are like sorting mail into labeled bins by recipient street. Metric aggregations are like counting how many letters landed in one bin. Pipeline aggregations are like comparing how that bin’s count has changed week over week.

4Deep Dive: Bucket Aggregations

The grouping mechanism behind almost every dashboard chart you’ve ever seen.

A bucket aggregation divides matching documents into groups — “buckets” — based on a chosen criterion, and reports how many documents fall into each one. This is precisely what powers common reporting features like “orders grouped by status,” “products grouped by category,” or “events grouped by hour.”

Terms Bucket

Groups documents by the exact distinct values found in a field, like grouping products by their brand name.

Date Histogram Bucket

Groups documents into fixed time intervals — like per day, per week, or per month — perfect for trend charts over time.

Range Bucket

Groups documents into custom-defined ranges, such as price brackets like “under 500,” “500 to 1000,” and “over 1000.”

i
Good to Know

Bucket aggregations answer “how are these documents grouped,” but by themselves, they typically just report how many documents fall into each group — that’s where metric aggregations come in for anything more detailed.

5Deep Dive: Metric Aggregations

These calculate the actual numbers people usually want from a report.

MetricWhat It Calculates
AverageThe mean value of a numeric field across matching documents
SumThe total of a numeric field’s values across matching documents
Min / MaxThe smallest or largest value found
CardinalityAn approximate count of distinct values in a field
StatsA combined bundle of count, min, max, average, and sum in one calculation
!
Common Misconception

Some metrics, like cardinality, are designed to be extremely fast and memory-efficient by giving a very close approximation rather than a perfectly exact distinct count — a deliberate, sensible trade-off at large scale.

6Deep Dive: Pipeline Aggregations

These take other aggregation results as their raw material, rather than looking at documents directly.

A pipeline aggregation doesn’t calculate anything from the original documents at all — instead, it operates on the output of other aggregations, such as a series of monthly totals produced by a date histogram bucket, to compute something further, like a moving average, a cumulative running total, or the overall trend direction.

Why This Two-Step Approach Matters

Some genuinely useful insights, like “is this metric trending up or down over time,” simply can’t be calculated directly from raw documents — they require first summarizing the data into buckets, and then analyzing the pattern across those bucket results.

7Combining and Nesting Aggregations Together

The real power of aggregations comes from layering them, not using them in isolation.

Aggregations can be nested inside one another, letting you ask increasingly detailed questions in a single request. For example, you might first bucket documents by product category, and then, within each category bucket, calculate the average price and the total number of units sold — producing a rich, multi-layered summary in one pass.

graph TD
    A[All Matching Documents] --> B["Bucket by: Category"]
    B --> C["Category: Electronics"]
    B --> D["Category: Clothing"]
    C --> E[Metric: Average Price]
    C --> F[Metric: Total Units Sold]
    D --> G[Metric: Average Price]
    D --> H[Metric: Total Units Sold]
        
FIG 1 — A bucket aggregation nested with metric aggregations, producing a detailed per-category summary.
i
Good to Know

This nesting can go several levels deep — for instance, grouping by category, then by month, then calculating an average within each combination — letting a single request answer surprisingly sophisticated analytical questions.

8How Aggregations Work Internally

A quick look at what happens behind the scenes when an aggregation request runs.

1

Matching Documents Identified

Any search conditions included in the request are first used to narrow down which documents are relevant.

2

Each Shard Calculates Its Own Partial Aggregation

Every relevant shard computes its own local buckets and metrics based only on the documents it holds.

3

Partial Results Are Sent to the Coordinating Node

Each shard’s partial buckets and calculations are forwarded back for combination.

4

Results Are Merged Into a Final Answer

The coordinating node combines all shards’ partial results into one accurate, unified aggregation response.

i
Good to Know

This shard-by-shard, then-merge approach is exactly the same scatter-and-gather pattern coordinating nodes use for regular search, applied to summarizing data instead of listing documents.

9Advantages and Trade-offs of Aggregations

Aggregations turn Elasticsearch into an analytics engine, but that power comes with a resource cost.

Advantages

  • Enables rich reporting and dashboards directly from stored data, without needing a separate analytics system
  • Runs in parallel across shards, taking advantage of the same distributed architecture that powers search
  • Supports deeply nested, multi-layered summaries in a single request
  • Combines seamlessly with search filtering, so summaries can be scoped to exactly the relevant subset of data

Disadvantages / Trade-offs

  • Aggregations over fields with very high cardinality can consume significant memory
  • Deeply nested aggregations can become computationally expensive
  • Certain metrics, like cardinality, trade perfect accuracy for speed and efficiency
“Search finds the needles. Aggregation tells you what the whole haystack looks like.”

10Performance and Memory Considerations

Not all aggregations cost the same — a few factors have an outsized impact.

FactorEffect on Performance
Field cardinality (number of distinct values)Very high cardinality fields, like unique user IDs, can make bucket aggregations memory-intensive
Depth of nested aggregationsEach additional nesting level multiplies the amount of computation and memory required
Number of buckets requestedRequesting an extremely large number of buckets increases both computation and response size
Whether the field is optimized for aggregationFields specifically prepared for fast aggregation perform noticeably better than ones that aren’t
!
Common Mistake

Running an unrestricted terms bucket aggregation on a field with extremely high cardinality, without any limit on the number of buckets returned, can put significant, unnecessary strain on the cluster.

11Security Considerations for Aggregations

Summaries can sometimes reveal information just as sensitively as raw documents can.

!
Risk: Indirect Data Exposure

Even without exposing individual documents, an aggregation summarizing sensitive data — like average salaries grouped by a small department — can indirectly reveal information that should have been protected.

Applying the Same Access Rules to Aggregations

Any filters and permission restrictions enforced for regular search should be applied consistently to aggregation requests as well, since an aggregation summarizing restricted data is still summarizing restricted data.

Limiting Resource-Heavy Aggregation Access

Restricting which users or applications are allowed to run expensive, high-cardinality aggregations helps prevent both accidental cluster strain and potential misuse for extracting sensitive patterns.

12Monitoring Aggregation Performance

A few signals worth tracking as aggregation-heavy dashboards and reports grow in use.

SignalWhy It Matters
Search/aggregation latencyRising latency on aggregation-heavy queries can point to overly broad or deeply nested requests
Node memory usage during aggregation-heavy periodsHigh-cardinality aggregations can cause noticeable memory spikes
Circuit breaker tripsElasticsearch’s built-in safeguards may reject aggregations that would use excessive memory, which is worth tracking rather than ignoring

13Design Patterns and Anti-patterns

Lessons drawn from real dashboards and reports that pushed aggregations too far.

ANTI-PATTERN-01 Avoid
Problem

Running a terms bucket aggregation on a very high-cardinality field, like a unique session ID, without setting any limit on the number of buckets returned.

Why It’s Harmful

This can attempt to create an enormous number of buckets, consuming excessive memory and slowing the entire cluster.

Correct Approach

Limit the number of buckets requested to a reasonable size, and reconsider whether a high-cardinality field genuinely needs bucket-style grouping at all.

Pattern: Filter First, Aggregate Second

Combining a filter-context condition to narrow the relevant document set before running an aggregation keeps the calculation focused and efficient, rather than aggregating across an entire, much larger index unnecessarily.

14Best Practices for Using Aggregations

Practical guidance for anyone building reports or dashboards on top of Elasticsearch.

i
Best Practice

Combine aggregations with filter-context search conditions whenever possible, so calculations run only over the genuinely relevant subset of data.

i
Best Practice

Set sensible limits on the number of buckets requested, especially for fields with a large number of distinct values.

!
Common Mistake

Assuming approximate metrics like cardinality are always perfectly exact — for use cases requiring precise counts, this trade-off should be understood and accounted for.

15Real-World Examples

Aggregations power the analytics layer behind many familiar tools and dashboards.

Business Intelligence Dashboards

Sales dashboards showing revenue broken down by region, product category, and month rely on layered bucket and metric aggregations running directly against stored transaction data.

Log and Monitoring Platforms

Charts showing error counts per service over time, or the distribution of response times, are built almost entirely from date histogram and metric aggregations.

E-commerce Faceted Navigation

The sidebar filters showing “Brand (with counts)” or “Price Range (with counts)” on a shopping site are powered by bucket aggregations calculated alongside the main product search.

Security and Fraud Detection

Detecting an unusual spike in login attempts from a particular region relies on aggregations grouping and counting events over time to reveal patterns that a single search couldn’t surface.

16Frequently Asked Questions

Q1Can I run a search and an aggregation in the same request?

Yes — this is extremely common. Search conditions narrow down which documents are considered, and the aggregation then summarizes just that narrowed set.

Q2Do aggregations return individual matching documents too?

Not by default — an aggregation-focused request often returns zero individual documents alongside the summary, though you can request a small sample of documents if needed.

Q3Why is cardinality described as “approximate”?

Calculating a perfectly exact count of distinct values across a huge dataset is resource-intensive. The cardinality aggregation deliberately trades a small amount of precision for dramatically better speed and memory efficiency.

Q4How many levels deep can aggregations be nested?

Technically quite deep, but practically, each additional level increases computational cost, so nesting should be driven by genuine analytical need rather than convenience alone.

Q5Are aggregations only useful for numbers?

Not at all — bucket aggregations frequently group by text-like category values, dates, or ranges, and metric aggregations are just one part of the broader aggregation toolkit.

17Summary and Key Takeaways

Aggregations shift Elasticsearch from answering “which documents match” to answering “what does the data as a whole look like” — grouping documents into buckets, calculating metrics, and even deriving further insights through pipeline aggregations. Understanding the three core families, how they nest together, and the performance trade-offs of high-cardinality fields is what separates a simple report from a genuinely powerful, efficient analytics feature built directly on your search data.

Key Takeaways

  • Aggregations summarize and calculate across documents, rather than simply listing matches.
  • Bucket, metric, and pipeline aggregations are the three core families, each answering a different kind of question.
  • Aggregations can be nested to build rich, multi-layered summaries in a single request.
  • They follow the same scatter-and-gather pattern as search — computed per shard, then merged by the coordinating node.
  • High-cardinality fields and deep nesting are the main performance risks worth watching.
  • Aggregations should respect the same security and access rules as search, since summaries can indirectly expose sensitive data.
  • Combining filters with aggregations keeps calculations focused, efficient, and relevant.