聚合查询用于分组、汇总等操作,可与查询表达式结合使用。根据不同的汇总用途,主要分为4种不同的聚合。这4种聚合可以并列使用,用不同的自定义聚合结果名称区分。这4种聚合分别是:
一、Metric(指标聚合):
主要用于数值汇总计算,可以作为子聚合,但不能包含子聚合。
1、平均值avg:
GET http://$user:$passwd@$host:$port/$index/$type/_search
{
"size": 0,
"aggs": {
"count_avg": {//自定义聚合结果名称
"avg": {
"field": "count"
}
}
}
}
2、最大值max:
GET http://$user:$passwd@$host:$port/$index/$type/_search
{
"size": 0,
"aggs": {
"count_max": {//自定义聚合结果名称
"max": {
"field": "count"
}
}
}
}
3、最小值min:
GET http://$user:$passwd@$host:$port/$index/$type/_search
{
"size": 0,
"aggs": {
"count_min": {//自定义聚合结果名称
"min": {
"field": "count"
}
}
}
}
4、总和sum:
GET http://$user:$passwd@$host:$port/$index/$type/_search
{
"size": 0,
"aggs": {
"count_sum": {//自定义聚合结果名称
"sum": {
"field": "count"
}
}
}
}
5、记录总数、最小值、最大值、均值、求和,5种统计信息:
GET http://$user:$passwd@$host:$port/$index/$type/_search
{
"size": 0,
"aggs": {
"count_stats": {//自定义聚合结果名称
"stats": {
"field": "count"
}
}
}
}
6、去重后数据统计总数:
GET http://$user:$passwd@$host:$port/$index/$type/_search
{
"size": 0,
"aggs": {
"count_cardinality": {
"cardinality": {
"field": "count"
}
}
}
}
二、Bucketing(桶分聚合):
主要用于进行数据分组,可以作为子聚合,也可以包含子聚合。
1、terms:按指定的字段的值不同,分成不同的组,字段不能是text类型
GET http://$user:$passwd@$host:$port/$index/$type/_search
{
"size": 0,
"aggs": {
"count_group": {
"terms": {
"field": "count",
"size":2 //只查前两个组
}
}
}
}
2、range/date_range:按自定义的区间,分成不同的组,date_range用于对日期进行分组;
GET http://$user:$passwd@$host:$port/$index/$type/_search
{
"size": 0,
"aggs": {
"count_group": {
"range": {
"field": "count",
"ranges": [
{
"from": 100,
"to": 300
},
{
"from": 200,
"to": 500
}
]
}
}
}
}
3、filter/filters:经过单过滤/多过滤后进行聚合
GET http://$user:$passwd@$host:$port/$index/$type/_search
{
"size": 0,
"aggs": {
"filter_group": {
"filter": {
"term": {
"type": "3"
}
},
"aggs": {
"count_avg": {
"avg": {
"field": "count"
}
}
}
}
}
}
4、histogram/date_histogram:将指定字段的值以指定的值逐级递增,每级递增的区间作为分组,类似直方图。date_histogram用于对日期的直方图;
GET http://$user:$passwd@$host:$port/$index/$type/_search
{
"size": 0,
"aggs": {
"count_interval_group": {
"histogram": {
"field": "count",
"interval":20
}
}
}
}
三、Pipeline(管道聚合):
处理其他聚合输出的结果。管道聚合不能包含子聚合,但是某些类型的管道聚合可以链式使用。根据输入层级不同,主要分为2类:
1、parent:此类管道聚合的输入是是其父聚合的输出,一般不生成新的桶,而是对父聚合桶信息的增强。
GET http://$user:$passwd@$host:$port/$index/$type/_search
{
"size": 0,
"aggs": {
"sum_per_count": {
"histogram": {
"field": "count",
"interval": 10
},
"aggs": {
"count_sum_per_count": {
"sum": {
"field": "count"
}
},
"count_cumulative_sum_per_bucket": {//管道聚合:对权值在每一个桶中求所有之前的桶的该值累计的和
"cumulative_sum": {
"buckets_path": "count_sum_per_count"
}
}
}
}
}
}
2、sibling:此类管道聚合的输入是是其兄弟聚合的输出,并能在同级上计算新的聚合。
GET http://$user:$passwd@$host:$port/$index/$type/_search
{
"size": 0,
"aggs": {
"sum_per_count": {
"histogram": {
"field": "count",
"interval": 10
},
"aggs": {
"count_sum_per_count": {
"sum": {
"field": "count"
}
}
}
},
"count_avg_per_bucket": {//管道聚合:对所有桶取平均值
"avg_bucket": {
"buckets_path": "sum_per_count>count_sum_per_count"
}
}
}
}
四、Matrix(矩阵聚合):
此功能是测试版栖,在将来的版本中可能会完全更改或删除,暂不介绍。
由于查询参数是字符串,需要对一些特殊字符进行过滤,这些特殊字符有:\,~,*,?,|,+,(,),[,",{,&,如果不过滤,这些字符会全查或直接报语法错误。
更多详情请参考:https://www.elastic.co/guide/en/elasticsearch/reference/6.5/search-aggregations.html
https://blog.youkuaiyun.com/zyc88888/article/details/83016513