Elasticsearch Aggregations

4 min read
Aggregation trong Elasticsearch giúp phân tích và tổng hợp dữ liệu từ kết quả tìm kiếm. Có các loại aggregation sau:
1. Bucket Aggregations
Tạo ra các nhóm (bucket) dựa trên các giá trị field. Mỗi bucket chứa các document phù hợp với điều kiện.
Nhóm aggregation này khi trả ra kết quả sẽ có trường doc_count, vì nó phân nhóm tài liệu dựa theo giá trị và đếm số lượng tài liệu trong mỗi nhóm
Loại Aggregation | Mô tả | Example |
terms | Nhóm theo giá trị cụ thể (giống GROUP BY trong SQL). | { "aggs": { "group_by_category": { "terms": { "field": "category.keyword" } } } } |
range | Nhóm theo khoảng giá trị số (ví dụ: 0-100, 100-200). | { "aggs": { "price_ranges": { "range": { "field": "price", "ranges": [ { "to": 100 }, { "from": 100, "to": 500 }, { "from": 500 } ] } } } } |
date_histogram | Nhóm theo mốc thời gian (ngày, tháng, năm...). | { "aggs": { "orders_by_month": { "date_histogram": { "field": "order_date", "calendar_interval": "month" } } } } |
histogram | Nhóm theo các khoảng giá trị cố định. | { "aggs": { "price_histogram": { "histogram": { "field": "price", "interval": 100 } } } } |
filters | Nhóm theo nhiều filter khác nhau. | { "aggs": { "product_type_filter": { "filters": { "filters": { "shoes": { "term": { "category.keyword": "shoes" } }, "shirts": { "term": { "category.keyword": "shirts" } } } } } } } |
… |
2. Metric Aggregations
Tính toán các giá trị số từ dữ liệu.
Nhóm aggregation này khi trả ra kết quả sẽ không có trường doc_count, vì nhóm này không phân nhóm tài liệu mà thực hiện tính toán trên dữ liệu
Loại Aggregation | Mô tả | Example |
avg | Tính trung bình. | { "aggs": { "avg_price": { "avg": { "field": "price" } } } } |
sum | Tính tổng. | { "aggs": { "total_revenue": { "sum": { "field": "amount" } } } } |
min | Giá trị nhỏ nhất. | { "aggs": { "min_price": { "min": { "field": "price" } } } } |
max | Giá trị lớn nhất. | { "aggs": { "max_price": { "max": { "field": "price" } } } } |
stats | Trả về: count, min, max, avg, sum. | { "aggs": { "price_stats": { "stats": { "field": "price" } } } } |
… |
3. Pipeline Aggregations
Tính toán dựa trên kết quả của aggregation khác.
Loại Aggregation | Mô tả | Example |
bucket_script | Viết script tùy chỉnh trên bucket. | Tính tỷ lệ doanh thu trung bình trên mỗi đơn hàng (total_revenue / order_count) { "aggs": { "orders_per_category": { "terms": { "field": "category.keyword" }, "aggs": { "total_revenue": { "sum": { "field": "amount" } }, "order_count": { "value_count": { "field": "order_id" } }, "avg_revenue_per_order": { "bucket_script": { "buckets_path": { "revenue": "total_revenue", "count": "order_count" }, "script": "params.revenue / params.count" } } } } } } |
bucket_selector | Filter bucket dựa trên điều kiện. | Chỉ giữ lại những category có tổng doanh thu > 10,000{ "aggs": { "orders_per_category": { "terms": { "field": "category.keyword" }, "aggs": { "total_revenue": { "sum": { "field": "amount" } }, "high_revenue_filter": { "bucket_selector": { "buckets_path": { "revenue": "total_revenue" }, "script": "params.revenue > 10000" } } } } } } |
….. |
4. Conclusion
Aggregation | Summary | Has doc_count | Has sub-aggregation |
Bucket Aggregation | Tạo ra các nhóm (bucket) dựa trên các giá trị field. Mỗi bucket chứa các document phù hợp với điều kiện | ✅ | ✅ |
Metric Aggregation | Tính toán các giá trị số từ dữ liệu. | ❌ | ❌ |
Pipeline Aggregation | Tính toán dựa trên kết quả của aggregation khác | ❌ | ❌ |
0
Subscribe to my newsletter
Read articles from littlenotes directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
