Elasticsearch Aggregations

littlenoteslittlenotes
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 AggregationMô tảExample
termsNhóm theo giá trị cụ thể (giống GROUP BY trong SQL).{ "aggs": { "group_by_category": { "terms": { "field": "category.keyword" } } } }
rangeNhó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_histogramNhó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" } } } }
histogramNhóm theo các khoảng giá trị cố định.{ "aggs": { "price_histogram": { "histogram": { "field": "price", "interval": 100 } } } }
filtersNhó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 AggregationMô tảExample
avgTính trung bình.{ "aggs": { "avg_price": { "avg": { "field": "price" } } } }
sumTính tổng.{ "aggs": { "total_revenue": { "sum": { "field": "amount" } } } }
minGiá trị nhỏ nhất.{ "aggs": { "min_price": { "min": { "field": "price" } } } }
maxGiá trị lớn nhất.{ "aggs": { "max_price": { "max": { "field": "price" } } } }
statsTrả 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 AggregationMô tảExample
bucket_scriptViế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_selectorFilter 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

AggregationSummaryHas doc_countHas sub-aggregation
Bucket AggregationTạ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 AggregationTính toán các giá trị số từ dữ liệu.
Pipeline AggregationTí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

littlenotes
littlenotes