Terms Aggregation
The term aggregation groups documents based on the unique values in a field. Let’s group products by their category.
Query
GET /products/_search
{
"size": 0,
"aggs": {
"categories": {
"terms": {
"field": "category.keyword"
}
}
}
}
Output
{
"aggregations": {
"categories": {
"buckets": [
{
"key": "electronics",
"doc_count": 5
},
{
"key": "clothing",
"doc_count": 3
},
{
"key": "books",
"doc_count": 2
}
]
}
}
}
In this example, products are grouped by category, and the number of products in each category is counted.
Bucket Aggregation in Elasticsearch
Elasticsearch is a robust tool not only for full-text search but also for data analytics. One of the core features that make Elasticsearch powerful is its aggregation framework, particularly bucket aggregations. Bucket aggregations allow you to group documents into buckets based on certain criteria, making it easier to analyze and summarize your data.
This article will explain what bucket aggregations are, how they work, and provide detailed examples to help you understand their usage.