
Elasticsearch aggregations are a powerful tool for data analysis and visualization, allowing you to extract insights from large datasets.
They work by grouping data into categories, such as by date or location, and then calculating metrics like counts, sums, and averages for each group.
This makes it easy to identify trends and patterns in your data.
For example, you can use aggregations to group website traffic by country and calculate the total number of visitors from each country.
Elasticsearch provides a range of built-in aggregation types, including terms, range, and stats aggregations.
These can be combined in various ways to create complex aggregation pipelines.
By using aggregations, you can gain a deeper understanding of your data and make more informed decisions.
Elasticsearch Aggregation Basics
Elasticsearch supports a wide range of query types, including aggregations, which enable you to analyze and summarize large amounts of data in real-time.
You can use aggregations and aggs interchangeably, and every aggregation (or sub-aggregation) has a name.
For more insights, see: Elasticsearch Composite Aggregation
Aggregations are specified using the aggs parameter in Elasticsearch queries, allowing you to group, filter, and perform calculations on data without pre-aggregation or external processing.
Here are the different types of aggregations supported by Elasticsearch:
- Terms aggregation: groups documents by a field and calculates metrics for each group
- Bucket aggregation: groups documents into buckets based on a field or expression
- Metric aggregation: calculates a metric for each bucket
You can nest multiple aggregations to create a hierarchical structure of buckets, allowing you to group documents by multiple fields and calculate metrics for each group.
For another approach, see: Elasticsearch Group by
Syntax
In Elasticsearch, aggregations are specified using the aggs parameter in queries, which allows you to group documents by a specific field and calculate metrics for each group.
You can use the terms aggregation to group documents by a field, and within each group, you can use metric aggregations to calculate values such as averages or sums.
The basic building blocks of an aggregation include the aggs keyword, the name of the aggregation, and the type of aggregation being used.
Here are some key points to keep in mind when working with aggregations:
- You can use aggregations and aggs interchangeably.
- Every aggregation (or sub-aggregation) has a name.
- You can set the size of the results to 0 to get no hits in the response.
- You can nest bucket aggregations before running a metric aggregation on them.
The syntax for aggregations is straightforward: you specify the aggs parameter, followed by the name of the aggregation and its type. For example, you can use the terms aggregation to group documents by a field, and then use a metric aggregation to calculate a value for each group.
Filter
Filter is a powerful tool in Elasticsearch aggregation that helps you narrow down your search results to specific documents. It's like having a magnifying glass to zoom in on the data that matters most.
To implement a filter, you need to establish one first, as shown in Screenshot J, where the filter "eddie" is set up to filter documents based on the username.
The filter aggregation can then be used to calculate metrics within the filtered bucket, such as the average price of products purchased, as demonstrated in the example where the average price is calculated for the products purchased by "eddie".
Explore further: Elasticsearch Updating Documents
Nesting
Nesting allows you to create a hierarchical structure of buckets by nesting aggregations inside one another. This enables you to divide buckets into sub-buckets or calculate metrics from sub-buckets.
You can use aggregations and aggs interchangeably, and every aggregation has a name. In an e-commerce website that acts as a marketplace, you can use nesting to calculate the sum of quantities of products, grouped by country and then by vendor.
Nesting aggregations is not the same as nested fields. For example, if you want to separate exam results by gender of the pupil and then calculate the average results for each gender, you can use a nested aggregation.
You can nest multiple aggregations to create a hierarchical structure of buckets. For instance, if you want to group documents by the "category" field and then by the "brand" field, you can use a request with nested terms aggregations.
Here are some key points to keep in mind when nesting aggregations:
- Nesting aggregations allows you to create a hierarchical structure of buckets.
- You can use aggregations and aggs interchangeably.
- Every aggregation has a name.
- Nesting is not the same as nested fields.
By nesting aggregations, you can divide buckets into sub-buckets or calculate metrics from sub-buckets, which can be useful in various use cases, such as calculating the sum of quantities of products or separating exam results by gender.
Aggregation Types
Elasticsearch supports several types of aggregations, including metrics, bucket, and pipeline aggregations.
Metrics aggregations calculate various metrics on a set of documents, such as sum, min, max, and average. This can be useful for getting an overview of a dataset.
Recommended read: Elasticsearch Metrics
There are several types of aggregations, including bucket, metric, and pipeline aggregations. Bucket aggregations group documents into buckets based on certain criteria, such as a date range, terms, or geospatial coordinates.
Here are some examples of bucket aggregations: Histogram Aggregation, Range Aggregation, Terms Aggregation, Filter(s) Aggregations, Geo Distance Aggregation, and IP Range Aggregation.
Metric aggregations calculate metrics, such as a sum or average, from field values. Mainly refers to the mathematical calculations performed across a set of documents, usually based on the values of a numerical field present in the document.
Pipeline aggregations take input from other aggregations instead of documents or fields. These aggregations allow you to aggregate based on the result of another aggregation rather than from document sets.
You might like: Document in Elasticsearch
Key Types
Aggregations can be grouped into four main categories: bucket, metric, pipeline, and matrix. Bucket aggregations are a method of grouping documents, allowing you to create data buckets based on existing fields, customized filters, ranges, and more.
There are several types of aggregations that can be further classified. Metrics Aggregations, for example, calculate various metrics on a set of documents, such as sum, min, max, and average.
Some common types of bucket aggregations include Histogram Aggregation, Range Aggregation, Terms Aggregation, Filter(s) Aggregations, Geo Distance Aggregation, and IP Range Aggregation. These aggregations group documents into "buckets" based on field values, ranges, or other criteria in the document.
Metric aggregations, on the other hand, calculate metrics, such as a sum or average, from field values. Mainly refers to mathematical calculations performed across a set of documents, usually based on the values of a numerical field present in the document.
Here are the key types of aggregations:
Pipeline aggregations allow you to perform calculations on the output of other aggregations, such as computing a moving average or a percentile rank.
Aggregation Types
Bucket aggregations are used to group documents into buckets, also called bins, based on field values, ranges, or other criteria in the document. This allows you to divide a set of invoices into several buckets, one for each customer.
You can use different types of bucket aggregations, such as Histogram Aggregation, Range Aggregation, Terms Aggregation, Filter(s) Aggregations, Geo Distance Aggregation, and IP Range Aggregation.
Metric aggregations calculate metrics, such as a sum or average, from field values. This is useful for performing mathematical calculations across a set of documents, usually based on the values of a numerical field present in the document.
Some common metric aggregations include COUNT, SUM, MIN, MAX, and AVERAGE.
Pipeline aggregations take input from other aggregations instead of documents or fields. This allows you to aggregate based on the result of another aggregation rather than from document sets.
Here are the main types of aggregations:
- Bucket aggregations: group documents into buckets based on field values, ranges, or other criteria.
- Metric aggregations: calculate metrics, such as a sum or average, from field values.
- Pipeline aggregations: take input from other aggregations instead of documents or fields.
Bucketing and Grouping
Bucketing and Grouping is a powerful feature in Elasticsearch aggregations that allows you to group documents into buckets based on certain criteria. To perform bucketing aggregations on multiple fields, you can use techniques such as date_histogram aggregation, histogram aggregation, range aggregation, and terms aggregation.
You can also calculate multiple metrics per bucket, such as daily revenue and number of unique customers per day, by adding multiple metric aggregations per bucket. This is useful for finding the highest daily revenue to date, for example.
The terms aggregation generates buckets by field values, and you can sort buckets based on the "doc_count" values in descending or ascending order. For instance, you can find the top 5 customers with the highest number of transactions by performing a terms aggregation on the field "CustomerID" and setting the size parameter to 5.
Number of Buckets
The number of buckets in a terms aggregation can be controlled by the "size" parameter, which determines how many buckets are returned. This parameter is crucial in understanding the output of a terms aggregation.
You can specify the number of buckets you want to see by setting the "size" parameter within the terms aggregation, as seen in Example 3 where we set the size parameter to 5 to find the top 5 customers.

The total number of buckets will be multiplied for each level of aggregation when you have bucket aggregations nested at more than one level, as mentioned in Example 4. This means that the more levels of aggregation you have, the more buckets you'll end up with.
In Example 3, we used the "size" parameter to limit the output to the top 5 customers, which is a great way to focus on the most relevant information.
Bucketing on Multiple Fields
Bucketing on multiple fields can be a bit tricky, but don't worry, it's easier than you think. You can use nested terms aggregations to create a hierarchical structure of buckets. For example, you can group documents by the "category" field and then by the "brand" field.
The total number of buckets will be multiplied for each level of aggregation, so be sure to keep that in mind. This means that if you have a category with 5 buckets and a brand with 3 buckets, you'll end up with 15 buckets in total.

To perform bucketing aggregations on multiple fields, you can also use multi terms aggregations. This will create one bucket per combination of field values. For instance, if you want to group documents by the "category" and "brand" fields, you can use the multi_terms bucket aggregation.
Remember to always run aggregations on keyword type fields, not analyzed text. If you must run on analyzed text, use the "fielddata": "true" setting, but be aware that this can be memory-intensive.
Broaden your view: Run Elasticsearch Locally
Histogram
Histogram aggregation is a powerful tool for grouping documents into numerical intervals. It allows you to create buckets based on any numerical field, such as price or age.
For example, you can instruct Elasticsearch to run a histogram aggregation on a field like "UnitPrice" and configure the price interval to increase in increments of 10. This will return an array of buckets where each bucket represents a price interval, along with the number of documents placed in each bucket.
Readers also liked: Elasticsearch Refresh Interval
The histogram aggregation sorts buckets based on the "key" values, which are the numerical values that define each interval. By default, it sorts in ascending order, but you can easily change this by adding the order parameter and specifying "desc" for descending order.
Here are the ways you can group documents into buckets using histogram aggregation:
- Date_histogram aggregation
- Histogram aggregation
- Range aggregation
- Terms aggregation
To illustrate the flexibility of histogram aggregation, consider a scenario where you want to analyze sales data by price interval. By running a histogram aggregation on the "UnitPrice" field with a price interval increasing in increments of 10, you can identify patterns in customer purchasing behavior.
Metrics and Calculations
Metrics and Calculations are the backbone of Elasticsearch aggregation. You can compute multiple metric aggregations in a single request by specifying them in the "aggs" section, allowing you to calculate the average price and the total quantity of products in each category.
Metric aggregations can only be performed on fields that contain numeric values, such as "Quantity" and "UnitPrice". This is essential to know, as it will help you understand what fields can be used for metric aggregations.
Discover more: Elasticsearch Fields
You can calculate multiple metrics per bucket, making it possible to determine the daily revenue and the number of unique customers per day in one go. This is achieved by adding multiple metric aggregations per bucket, as shown in the example.
Here are the types of metric aggregations you can perform:
- Count
- Min
- Max
- Avg
- Sum
- Cardinality
These aggregations can be used to calculate various metrics, such as the sum of all unit prices in the index or the lowest unit price of an item. The stats aggregations can calculate all these metrics in one go, making it a convenient option.
To compute the sum of all unit prices in the index, you can use the "sum" aggregation type, as shown in the example. This will provide you with the total sum of all unit prices present in your index.
You can also use the "min" and "max" aggregation types to calculate the lowest and highest unit prices of an item, respectively. These aggregations can be performed on fields that contain numeric values, such as "UnitPrice".
Date and Range Aggregations
Date and Range Aggregations can be used together in Elasticsearch to create complex summaries of data.
The range aggregation is similar to the histogram aggregation in that it can create buckets based on any numerical interval.
You can customize the range aggregation to suit your use case by defining intervals of varying sizes. For example, you can group sales transactions into price ranges like "up to 100", "between 100 and 500", and "above 500".
To achieve this, you would use the "field" parameter to specify the field you want to group by (in this case, "price") and the "ranges" parameter to define the boundaries of each range.
Here's a summary of a range aggregation query:
- Field: price
- Ranges:
- Up to 100
- Between 100 and 500
- Above 500
Date Histogram
The date histogram is a powerful aggregation tool in Elasticsearch that allows you to group documents by a date field and create buckets based on a specified interval.
You can use the date histogram aggregation to group documents by the created_at field and create daily buckets, as seen in Example 1.
To define a time interval with date histogram aggregation, you can use either the fixed_interval or calendar_interval method.
The fixed_interval method creates buckets at a constant interval, such as every 8 hours, as shown in Example 2. This is useful when you want to analyze data at a consistent frequency.
The calendar_interval method, on the other hand, creates buckets at varying intervals, such as days, months, or leap seconds, as also shown in Example 2. This is useful when you want to analyze data at different frequencies, such as monthly revenue.
By default, the date histogram aggregation sorts buckets in ascending order of dates, with the earliest date first.
However, you can reverse this order by adding an order parameter to the aggregations and specifying that you want to sort buckets in descending order, as seen in Example 2. This is useful when you want to analyze the most recent data first.
Range
The range aggregation is a powerful tool that allows you to group data into customized intervals. It's similar to the histogram aggregation, but with more flexibility.
You can define intervals of varying sizes to suit your needs. For example, you can group sales transactions into three price ranges: up to 100, between 100 and 500, and above 500.
The range aggregation is sorted based on the input ranges you specify, and it cannot be sorted any other way. This means you can't sort the results in ascending order, but you can specify the ranges in any order you like.
To use the range aggregation, you need to specify the field you want to group by and the ranges you want to use. For example, you can use the range aggregation to group sales transactions into price ranges with the following query: "field": "price", "ranges": [{"from": 0, "to": 100}, {"from": 100, "to": 500}, {"from": 500, "to": null}].
The result of the range aggregation will be a summary of the sales data grouped by price ranges. For each range, you'll see the number of sales transactions that fall within that range.
Here's an example of what the result might look like:
This shows that over 400,000 transactions have occurred for items priced between 0 and 50, and that 855 transactions have occurred for items priced between 50 and 200.
Limiting and Sorting Aggregations
You can limit the scope of aggregations by adding a query clause to the aggregations request. This defines the subset of documents that aggregations should be performed on.
To limit the scope of aggregations, you can add a query clause that matches a specific value in a field, like "Germany" in the "Country" field. This is useful when you want to calculate aggregations for a specific subset of documents, such as e-commerce data from a particular country.
The query clause instructs Elasticsearch to query all documents that match the specified value. For example, to calculate the average unit price of items sold in Germany, you would query all documents that match the value "Germany" in the "Country" field.
Elasticsearch then runs aggregations on the queried data, allowing you to perform calculations like averages or sums on the specified subset of documents. This is demonstrated in the example where the average unit price of items sold in Germany is calculated.
You can also use bucket aggregations to perform aggregations on several subsets of documents. Bucket aggregations group documents into buckets based on a specified field, allowing you to perform calculations on each bucket separately.
Take a look at this: Elasticsearch Query
Implementation and Examples
In Elasticsearch, aggregations can be used to group and summarize data from multiple documents. This can be particularly useful for analyzing sales data, such as in the "sales" index, where each document represents a sales transaction.
Each document in the "sales" index has a "price" field that represents the amount of the sale. This field can be used to calculate various aggregations, such as the total sales amount or the average sale price.
To implement aggregations in Elasticsearch, you can use the "sales" index as an example, grouping documents by fields like "price" or "category" to gain insights into your sales data.
Implementation Examples
In Elasticsearch, an index called "sales" can be used to store documents representing sales transactions. Each document in this index has a "price" field that represents the amount of the sale.
We can explore some implementation examples of aggregations in Elasticsearch using the "sales" index. Aggregations allow us to perform calculations on groups of documents, such as summing up the prices of all sales transactions.

For example, we can use the "sales" index to calculate the total revenue from all sales transactions. This can be done by summing up the "price" field of all documents in the index.
By using aggregations in Elasticsearch, we can gain valuable insights into our sales data and make more informed business decisions.
A different take: Elasticsearch Index Api
Building a Job Search Engine with Elasticsearch and Golang
Aggregations are a powerful feature in Elasticsearch that enable you to analyze and summarize large amounts of data. They allow you to group, filter, and perform calculations on data in real-time, without the need for pre-aggregation or external processing.
Elasticsearch supports a wide range of query types, including various types of aggregations.
To get the most out of aggregations, you need to understand how they work. Aggregations can be used to group data by various fields, such as job title, location, or industry.
Elasticsearch supports a wide range of aggregation types, including terms, histogram, and range aggregations.
Aggregations can be used to filter data in real-time, making it an essential feature for building a job search engine.
Elasticsearch's aggregation feature allows you to perform calculations on data in real-time, making it an ideal choice for building a job search engine.
A unique perspective: Elasticsearch Search Template
Performance
Performance is crucial when it comes to Elasticsearch aggregations, as they are typically carried out in RAM memory.
Aggregations require a different document access structure than a search query, which is obtained from the inverted index. This difference in structure has significant implications for performance.
To construct efficient aggregations, it's essential to consider the performance implications, particularly the most important considerations, which are related to document access and query optimization.
In general, aggregations can be computationally expensive, so it's vital to optimize them to avoid performance issues.
Check this out: Elasticsearch Query Builder
Advanced Topics
Elasticsearch aggregation can be used to calculate the average rating of movies in a bucket, but it's not limited to just numerical fields, it can also be used with date fields to calculate the average release date of movies in a bucket.
To calculate the average rating, you can use the avg aggregation with the rating field, which is a numerical field.
Elasticsearch aggregation can also be used to calculate the average rating of movies in a specific genre, by using a filter aggregation to filter the movies by genre, and then using the avg aggregation on the filtered results.
The avg aggregation can be used with multiple fields, but it's not possible to calculate the average of a string field, as it doesn't make sense to calculate an average of a string.
To calculate the average rating of movies in a specific genre, you can use a script aggregation to calculate the average rating, and then use the avg aggregation to calculate the average of the calculated values.
Elasticsearch aggregation can also be used to calculate the average rating of movies in a specific year, by using a date histogram aggregation to group the movies by year, and then using the avg aggregation on the grouped results.
The avg aggregation can be used to calculate the average of a single field, but it's not possible to calculate the average of multiple fields in a single aggregation.
To calculate the average rating of movies in a specific year and genre, you can use a combination of filter aggregations and script aggregations to calculate the average rating, and then use the avg aggregation to calculate the average of the calculated values.
Additional reading: Elasticsearch Script
Frequently Asked Questions
What is the difference between facets and aggregation in Elasticsearch?
Facets in Elasticsearch have limitations, whereas aggregations offer more flexibility and depth in data exploration. Aggregations were designed to overcome the limitations of facets and provide deeper insights into documents.
How many buckets are there in Elasticsearch aggregation?
Elasticsearch aggregation defaults to 10,000 buckets. You can adjust this limit with the size attribute, but be aware of potential performance impacts.
Featured Images: pexels.com


