6.2 Metric Aggregations (指标聚合) 6.2 Metric Aggregations (指标聚合) 指标聚合主要用于计算字段的统计指标,例如最小值、最大值、平均值、总和、方差、标准差等。它们是数据分析的基础,可以帮助我们快速了解数据的分布和特征。 6.2.1 核心概念 Metric Aggregations (指标聚合) 的核心思想是针对文档集合中的数值字段,计算出一个或多个统计指标。这些指标可以是: 单值指标 (Single-Value Metrics): 返回一个单独的数值,例如 , , , 。 多值指标 (Multi-Value Metrics): 返回多个数值,例如 , , 。 6.2.
指标聚合主要用于计算字段的统计指标,例如最小值、最大值、平均值、总和、方差、标准差等。它们是数据分析的基础,可以帮助我们快速了解数据的分布和特征。
Metric Aggregations (指标聚合) 的核心思想是针对文档集合中的数值字段,计算出一个或多个统计指标。这些指标可以是:
单值指标 (Single-Value Metrics): 返回一个单独的数值,例如 min,max,avg,sum。
多值指标 (Multi-Value Metrics): 返回多个数值,例如 stats,extended_stats,percentiles。
以下是一些常用的指标聚合类型:
min Aggregation: 计算字段的最小值。
max Aggregation: 计算字段的最大值。
avg Aggregation: 计算字段的平均值。
sum Aggregation: 计算字段的总和。
count Aggregation: 计算文档的数量 (实际上不是基于字段,而是整个文档集)。
stats Aggregation: 同时计算 min, max, avg, sum, 和 count。
extended_stats Aggregation: 在 stats 的基础上,还计算 sum_of_squares (平方和), variance (方差), std_deviation (标准差) 等。
percentiles Aggregation: 计算字段的百分位数。
cardinality Aggregation: 计算字段的近似不同值数量 (基数)。
value_count Aggregation: 计算字段中具有值的文档数量。
为了演示 Metric Aggregations,我们假设有一个 products 索引,包含以下字段:
name (keyword): 产品名称
price (double): 产品价格
stock (integer): 库存数量
category (keyword): 产品类别
准备数据
首先,我们需要创建 products 索引并插入一些数据。
PUT /products { "mappings": { "properties": { "name": { "type": "keyword" }, "price": { "type": "double" }, "stock": { "type": "integer" }, "category": { "type": "keyword" } } } } POST /products/_bulk { "index": { "_id": 1 }} { "name": "T-Shirt", "price": 25.0, "stock": 100, "category": "Clothing" } { "index": { "_id": 2 }} { "name": "Jeans", "price": 75.0, "stock": 50, "category": "Clothing" } { "index": { "_id": 3 }} { "name": "Laptop", "price": 1200.0, "stock": 10, "category": "Electronics" } { "index": { "_id": 4 }} { "name": "Mouse", "price": 20.0, "stock": 200, "category": "Electronics" } { "index": { "_id": 5 }} { "name": "Keyboard", "price": 80.0, "stock": 80, "category": "Electronics" } { "index": { "_id": 6 }} { "name": "Book", "price": 15.0, "stock": 150, "category": "Books" } { "index": { "_id": 7 }} { "name": "Notebook", "price": 5.0, "stock": 500, "category": "Books" }
1. min Aggregation (最小值)
计算所有产品的最低价格:
GET /products/_search { "size": 0, "aggs": { "min_price": { "min": { "field": "price" } } } }
响应:
{ "took": 2, "timed_out": false, "_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 7, "relation": "eq" }, "max_score": null, "hits": [] }, "aggregations": { "min_price": { "value": 5.0 } } }
解释: min_price aggregation 返回了 price 字段的最小值,即 5.0。
2. max Aggregation (最大值)
计算所有产品的最高价格:
GET /products/_search { "size": 0, "aggs": { "max_price": { "max": { "field": "price" } } } }
3. avg Aggregation (平均值)
计算所有产品的平均价格:
GET /products/_search { "size": 0, "aggs": { "avg_price": { "avg": { "field": "price" } } } }
4. sum Aggregation (总和)
计算所有产品的总库存数量:
GET /products/_search { "size": 0, "aggs": { "total_stock": { "sum": { "field": "stock" } } } }
5. stats Aggregation (统计)
一次性计算 min, max, avg, sum, 和 count:
GET /products/_search { "size": 0, "aggs": { "price_stats": { "stats": { "field": "price" } } } }
响应:
{ "took": 1, "timed_out": false, "_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 7, "relation": "eq" }, "max_score": null, "hits": [] }, "aggregations": { "price_stats": { "count": 7, "min": 5.0, "max": 1200.0, "avg": 231.42857142857142, "sum": 1620.0 } } }
6. extended_stats Aggregation (扩展统计)
在 stats 的基础上,还计算 sum_of_squares, variance, std_deviation 等:
GET /products/_search { "size": 0, "aggs": { "price_extended_stats": { "extended_stats": { "field": "price" } } } }
7. percentiles Aggregation (百分位数)
计算价格的 25th, 50th, 75th, 和 95th 百分位数:
GET /products/_search { "size": 0, "aggs": { "price_percentiles": { "percentiles": { "field": "price", "percents": [ 25, 50, 75, 95 ] } } } }
响应:
{ "took": 1, "timed_out": false, "_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 7, "relation": "eq" }, "max_score": null, "hits": [] }, "aggregations": { "price_percentiles": { "values": { "25.0": 22.5, "50.0": 75.0, "75.0": 437.5, "95.0": 1140.0 } } } }
解释: 25.0 百分位数为 22.5,意味着 25% 的产品的价格低于 22.5。
8. cardinality Aggregation (基数)
计算不同产品类别的数量:
GET /products/_search { "size": 0, "aggs": { "category_count": { "cardinality": { "field": "category" } } } }
9. value_count Aggregation (值计数)
计算有多少产品有价格信息:
GET /products/_search { "size": 0, "aggs": { "price_count": { "value_count": { "field": "price" } } } }
Metric Aggregations 通常与 Bucket Aggregations 结合使用,以分析特定分组的数据。 例如,我们可以计算每个产品类别的平均价格:
GET /products/_search { "size": 0, "aggs": { "categories": { "terms": { "field": "category" }, "aggs": { "avg_price": { "avg": { "field": "price" } } } } } }
响应:
{ "took": 1, "timed_out": false, "_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 7, "relation": "eq" }, "max_score": null, "hits": [] }, "aggregations": { "categories": { "doc_count_error_upper_bound": 0, "sum_other_doc_count": 0, "buckets": [ { "key": "Electronics", "doc_count": 3, "avg_price": { "value": 433.3333333333333 } }, { "key": "Clothing", "doc_count": 2, "avg_price": { "value": 50.0 } }, { "key": "Books", "doc_count": 2, "avg_price": { "value": 10.0 } } ] } } }
解释: 这个查询首先使用 terms aggregation 将产品按类别分组,然后在每个类别内计算平均价格。
Metric Aggregations 还可以使用脚本来计算更复杂的指标。 例如,我们可以计算价格乘以库存的总价值:
GET /products/_search { "size": 0, "aggs": { "total_value": { "sum": { "script": { "source": "doc['price'].value * doc['stock'].value" } } } } }
以下是一个简单的 mermaid 图,展示了 Metric Aggregations 的基本结构:
解释:
Search Request: 发起 Elasticsearch 搜索请求。
Aggregations: 搜索请求中包含的聚合部分。
Metric Aggregation: 一种特定的聚合类型,用于计算指标。
Field: price: 指定要计算指标的字段 (例如 price)。
Type: avg, min, max, sum, etc.: 指定要计算的指标类型 (例如平均值、最小值等)。
Result: 聚合计算的结果。
Metric Aggregations 是 Elasticsearch 中强大的数据分析工具,可以帮助我们快速了解数据的统计特征。 通过灵活地组合不同的 Metric Aggregations 和 Bucket Aggregations,我们可以进行深入的数据挖掘和分析。 结合 scripting 可以实现更复杂的需求。 理解 Metric Aggregations 的原理和用法,对于高效地使用 Elasticsearch 进行数据分析至关重要。