ElasticSearch Aggregation(一)
聚合将您的数据汇总为指标、统计数据或者其他信息。聚合操作可以回到你的以下问题:
- 我的网站的平均加载时间是多少?
- 根据交易量,谁是我最有价值的客户?
- 什么会被视为我的网络上的大文件?
- 每个产品类别中有多少产品?
ElasticSearch将聚合分为以下三类:
- 指标聚合,例如总和、平均值等
- 桶聚合,根据字段值、范围或者其他条件将文档分组为桶
- 以其他聚合结果作为输入的管道聚合
运行一个聚合
你可以通过search api 中的aggs 参数将聚合作为搜索的一部分来运行聚合操作。根据以下示例,在my-field 字段上运行术语聚合。
curl -X GET "localhost:9200/my-index-000001/_search?pretty" -H 'Content-Type: application/json' -d'
{
"aggs": {
"my-agg-name": {
"terms": {
"field": "my-field"
}
}
}
}
'
聚合结果被包含在响应中aggregations 对象中:
{
"took": 78,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 5,
"relation": "eq"
},
"max_score": 1.0,
"hits": [...]
},
"aggregations": {
"my-agg-name": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": []
}
}
}
修改聚合的范围
使用query 参数来限制对文档聚合的范围
curl -X GET "localhost:9200/my-index-000001/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"range": {
"@timestamp": {
"gte": "now-1d/d",
"lt": "now/d"
}
}
},
"aggs": {
"my-agg-name": {
"terms": {
"field": "my-field"
}
}
}
}
'
只返回聚合结果
默认情况下,包含聚合的搜索会返回搜索命中和聚合结果。要仅返回聚合结果,请将 size 设置为 0:
curl -X GET "localhost:9200/my-index-000001/_search?pretty" -H 'Content-Type: application/json' -d'
{
"size": 0,
"aggs": {
"my-agg-name": {
"terms": {
"field": "my-field"
}
}
}
}
'
运行多个聚合
你可以在一个请求中指定运行多个聚合
curl -X GET "localhost:9200/my-index-000001/_search?pretty" -H 'Content-Type: application/json' -d'
{
"aggs": {
"my-first-agg-name": {
"terms": {
"field": "my-field"
}
},
"my-second-agg-name": {
"avg": {
"field": "my-other-field"
}
}
}
}
'
运行子聚合
桶聚合支持桶和指标的子聚合。例如,一个带有avg 子聚合的术语为每一个桶中的文档聚合计算一个平均值。嵌套的子聚合没有深度和级别的限制。
curl -X GET "localhost:9200/my-index-000001/_search?pretty" -H 'Content-Type: application/json' -d'
{
"aggs": {
"my-agg-name": {
"terms": {
"field": "my-field"
},
"aggs": {
"my-sub-agg-name": {
"avg": {
"field": "my-other-field"
}
}
}
}
}
}
'
响应在其父聚合下嵌套子聚合结果:
{
...
"aggregations": {
"my-agg-name": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "foo",
"doc_count": 5,
"my-sub-agg-name": {
"value": 75.0
}
}
]
}
}
}
添加自定义元数据
使用meta 对象将自定义元数据和聚合关联起来:
curl -X GET "localhost:9200/my-index-000001/_search?pretty" -H 'Content-Type: application/json' -d'
{
"aggs": {
"my-agg-name": {
"terms": {
"field": "my-field"
},
"meta": {
"my-metadata-field": "foo"
}
}
}
}
'
响应值:
{
...
"aggregations": {
"my-agg-name": {
"meta": {
"my-metadata-field": "foo"
},
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": []
}
}
}
|