简单的聚合查询:关键字aggs
- 在使用es聚合函数时应该需要知道一些必要的名次
度量
和桶
- 度量:
Metric Aggregations(度量聚合)
Metric聚合,主要针对数值类型的字段,类似于关系型数据库中的sum、avg、max、min等聚合类型
参考文章 - 桶聚合:
Buket Aggregations(桶聚合)
不像metrics Aggregations(度量聚合)那样计算字段上的度量,而是创建文档桶,每个文件桶有效地定义一个文档集。除了bucket本身之外,bucket聚合还计算并返回“落入”每个bucket的文档的数量。
就如同将一个大桶的水按一定的条件分到其他的小桶中。
与度量聚合相反,桶聚合可以嵌套子聚合。这些子聚合将为它们的“父”桶聚合创建的桶进行聚合。
参考文章;常用桶聚合一览
参考文章;Terms Aggregation与Significant Terms Aggregation
ES Bucket Aggregations对标关系型数据库的(group by)。
- 聚集的具体结构:
- 使用查询中同样的json请求来定义他们,而且需要试用版aggs或aggr...来进行标记。需要给每个聚集起一个名字,定义它的类型以及和该类型相关的选项
一些技术的番外篇
ElasticSearch - 嵌套对象 nested
ElasticSearch 桶内排序 bucket_sort bucket_sort 类似于aggs的一个聚合函数
实际操作json构建
{
"query": {
"range": {
"pay_time": {
"gte": "2020-11-01",
"lt": "2020-11-12",
"format": "yyyy-MM-dd"
}
}
},
"aggs": {
"temr_cityCode": {
"terms": {
"field": "city_code"
},
"aggs": {
"percentiles_free": {
"percentiles": {
"field": "actual_paid_fee",
"percents": [
1,
5,
25,
50,
75,
95,
99
]
}
},
"sum_free": {
"sum": {
"field": "actual_paid_fee"
}
},
"r_bucket_sort": {
"bucket_sort": {
"sort": {
"sum_free": {
"order": "desc"
}
}
}
}
}
}
},
"size": 0
}
{
"query": {
"bool": {
"must": [{
"term": {
"city_code": {
"value": "320400"
}
}
}],
"filter": {
"range": {
"pay_time": {
"gte": "2020-11-01",
"lt": "2020-11-12",
"format": "yyyy-MM-dd"
}
}
}
}
},
"aggs": {
"temr_cityCode": {
"terms": {
"field": "open_buyer_id",
"shard_size": 1000,
"size": 1000
},
"aggs": {
"percentiles_free": {
"percentiles": {
"field": "actual_paid_fee",
"percents": [
1,
5,
25,
50,
75,
95,
99
]
}
},
"sum_free": {
"sum": {
"field": "actual_paid_fee"
}
},
"r_bucket_sort": {
"bucket_sort": {
"sort": {
"sum_free": {
"order": "desc"
}
}
}
}
}
}
},
"size": 0
}{
"query": {
"bool": {
"must": [{
"term": {
"city_code": {
"value": "320400"
}
}
}],
"filter": {
"range": {
"pay_time": {
"gte": "2020-11-01",
"lt": "2020-11-12",
"format": "yyyy-MM-dd"
}
}
}
}
},
"aggs": {
"temr_cityCode": {
"terms": {
"field": "open_buyer_id",
"shard_size": 1000,
"size": 1000
},
"aggs": {
"percentiles_free": {
"percentiles": {
"field": "actual_paid_fee",
"percents": [
1,
5,
25,
50,
75,
95,
99
]
}
},
"sum_free": {
"sum": {
"field": "actual_paid_fee"
}
},
"r_bucket_sort": {
"bucket_sort": {
"sort": {
"sum_free": {
"order": "desc"
}
}
}
}
}
}
},
"size": 0
}