Aggregations in Elasticsearch
Aggregations in Elasticsearch
So far, you've learned how to:
- Store data in Elasticsearch.
- Search for documents.
- Filter search results.
- Sort information.
- Display results using pagination.
But what if you don't want to search for individual documents?
What if you want answers like:
- How many laptops are available?
- Which brand sells the most products?
- What is the average product price?
- Which category has the highest number of products?
Instead of returning documents, Elasticsearch can analyze your data and provide summaries.
This powerful feature is called Aggregation.
Think of aggregations as asking Elasticsearch to analyze your data rather than simply searching through it.
By the end of this lesson, you'll understand:
- What aggregations are.
- Why they are useful.
- Different types of aggregations.
- How businesses use aggregations every day.
- Real-world examples of data analysis.
What is an Aggregation?
An Aggregation is a feature that groups, counts, or calculates information from your data.
Instead of returning every matching document, Elasticsearch performs calculations and provides a summary.
Imagine you own a supermarket.
Instead of asking:
Show me every product.
You ask:
How many products are in each category?
Elasticsearch doesn't list every product.
Instead, it returns a summary such as:
Category | Number of Products |
| Electronics | 850 |
| Clothing | 620 |
| Grocery | 430 |
| Furniture | 210 |
This summarized information is the result of an aggregation.
Why Are Aggregations Important?
Imagine an online shopping website with:
- 10 million products
- 5 million customers
- Millions of daily orders
A manager doesn't want to read every order individually.
Instead, they want quick insights like:
- Total sales today
- Best-selling category
- Average product rating
- Number of new customers
Aggregations make this possible.
Real-Life Analogy
Imagine a classroom with 50 students.
Instead of checking every student's marks one by one, the teacher wants answers like:
- Highest mark
- Lowest mark
- Average mark
- Number of students who passed
The teacher is analyzing the data rather than reading each student's marks.
This is exactly how aggregations work.
Types of Aggregations
Elasticsearch provides several types of aggregations.
The most commonly used ones are:
- Bucket Aggregations
- Metric Aggregations
- Nested Aggregations
Let's understand each one.
Bucket Aggregations
A Bucket Aggregation groups documents into different categories.
Imagine sorting colored balls into separate baskets.
Before sorting:
🔴 🔵 🟢 🔴 🔵 🟡 🟢 🔴
After sorting:
🔴 Basket → 3
🔵 Basket → 2
🟢 Basket → 2
🟡 Basket → 1
Similarly, Elasticsearch groups documents into buckets.
Example
Suppose an online store sells:
Product | Category |
| Laptop | Electronics |
| Phone | Electronics |
| T-shirt | Clothing |
| Shoes | Clothing |
| Rice | Grocery |
A Bucket Aggregation groups them like this:
Category | Count |
| Electronics | 2 |
| Clothing | 2 |
| Grocery | 1 |
Instead of showing every product, Elasticsearch summarizes the categories.
Terms Aggregation
One of the most popular bucket aggregations is the Terms Aggregation.
It groups documents based on a specific field.
Suppose you have products from these brands:
- Samsung
- Apple
- Samsung
- Dell
- Apple
- Samsung
The result becomes:
Brand | Products |
| Samsung | 3 |
| Apple | 2 |
| Dell | 1 |
Businesses often use this to answer questions such as:
- Which brand has the most products?
- Which department has the most employees?
- Which city has the most customers?
Metric Aggregations
While bucket aggregations group data, Metric Aggregations perform mathematical calculations.
Examples include:
- Count
- Sum
- Average
- Minimum
- Maximum
Let's understand each one.
Count Aggregation
Suppose an online bookstore contains:
- Book 1
- Book 2
- Book 3
- Book 4
The count aggregation simply returns:
Total Books = 4
Sum Aggregation
Imagine these order values:
₹2,500
₹3,000
₹1,500
₹4,000
Elasticsearch adds them together.
Result:
Total Sales
₹11,000
This is commonly used in sales dashboards.
Average Aggregation
Suppose product ratings are:
4.5
4.8
4.2
4.7
Elasticsearch calculates:
Average Rating
4.55
This helps businesses understand customer satisfaction.
Minimum Aggregation
Suppose laptop prices are:
₹45,000
₹60,000
₹72,000
₹39,000
Minimum value:
₹39,000
Maximum Aggregation
Using the same prices:
Maximum value:
₹72,000
Nested Aggregations
Sometimes one aggregation isn't enough.
Imagine a shopping website.
First, group products by category.
Then, within each category, calculate the average price.
Result:
Category | Average Price |
| Electronics | ₹42,000 |
| Clothing | ₹1,500 |
| Grocery | ₹350 |
This combines grouping and calculations in one analysis.
How Dashboards Use Aggregations
Business dashboards depend heavily on aggregations.
Imagine opening a sales dashboard.
Instead of displaying every order, it shows:
Today's Sales
₹18,50,000
Orders Today
2,450
Top Category
Electronics
Average Order Value
₹755
These numbers are generated using aggregations.
Real-World Applications
Amazon
Aggregations help answer:
- Which products sell the most?
- Which categories generate the highest revenue?
- What is the average customer rating?
Netflix
Aggregations help analyze:
- Most watched genres
- Most popular movies
- Average viewing time
Banking
Banks use aggregations to calculate:
- Total transactions
- Average account balance
- Number of daily transactions
- Highest transaction amount
Hospitals
Hospitals analyze:
- Number of patients
- Average waiting time
- Patients by department
- Most common diseases
Universities
Universities use aggregations to calculate:
- Average student marks
- Students in each department
- Highest GPA
- Attendance statistics
Search vs Aggregation
Many beginners confuse searching with aggregations.
Here's the difference:
Search
Returns individual documents.
Example:
Show all laptops.
Result:
- Laptop A
- Laptop B
- Laptop C
Aggregation
Returns summarized information.
Example:
How many laptops are available?
Result:
Total Laptops
325
Search retrieves data.
Aggregations analyze data.
Combining Search and Aggregations
One of Elasticsearch's biggest strengths is that you can combine searching and aggregations.
Imagine a customer searches:
Smartphones
Elasticsearch finds matching products.
At the same time, it calculates:
- Brands available
- Price ranges
- Average ratings
- Available colors
This is why shopping websites can show filters immediately after you perform a search.










