ElasticSearch6.x 之分组聚合分析

熊孩纸 阅读:227 2021-03-31 17:03:27 评论:0

数据准备:新建索引my_index,初始化数据

# 创建my_index索引 
Put 请求:http://192.168.1.74:9200/my-index/ 
#  索引数据初始化 
PUT请求: http://192.168.1.74:9200/my-index/persion/1 
{ 
  "name":"张三", 
  "age":27, 
  "gender":"男", 
  "salary":15000, 
  "dep":"bigdata" 
} 
 
PUT请求: http://192.168.1.74:9200/my-index/persion/2 
{ 
  "name":"李四", 
  "age":26, 
  "gender":"女", 
  "salary":15000, 
  "dep":"bigdata" 
} 
 
PUT请求: http://192.168.1.74:9200/my-index/persion/3 
{ 
  "name":"王五", 
  "age":26, 
  "gender":"男", 
  "salary":17000, 
  "dep":"AI" 
} 
PUT请求: http://192.168.1.74:9200/my-index/persion/4 
{ 
  "name":"刘六", 
  "age":27, 
  "gender":"女", 
  "salary":18000, 
  "dep":"AI" 
} 
 
PUT请求: http://192.168.1.74:9200/my-index/persion/5 
{ 
  "name":"程裕强", 
  "age":31, 
  "gender":"男", 
  "salary":20000, 
  "dep":"bigdata" 
} 
PUT请求: http://192.168.1.74:9200/my-index/persion/6 
{ 
  "name":"hadron", 
  "age":30, 
  "gender":"男", 
  "salary":20000, 
  "dep":"AI" 
}

分组聚合之Terms 分组(Terms Aggregation)

【例子】根据薪资水平进行分组,统计每个薪资水平的人数

POST 请求:http://192.168.1.74:9200/my-index/_search/ 
{ 
  "size": 0,  
  "aggs": { 
    "group_count": { 
      "terms": {"field": "salary"} 
    } 
  } 
}

【例子】统计上面每个分组的平均年龄

POST请求: http://192.168.1.74:9200/my-index/_search/ 
{ 
  "size": 0,  
  "aggs": { 
    "group_count": { 
      "terms": {"field": "salary"}, 
      "aggs":{ 
        "avg_age":{ 
          "avg":{"field": "age"} 
        } 
      } 
    } 
  } 
}

分组聚合之Filter分组(Filter Aggregation)

Filter聚合用于过滤器聚合,把满足过滤器条件的文档分到一组。

【例子】计算男人的平均年龄 

POST请求: http://192.168.1.74:9200/my-index/_search/ 
{ 
  "size": 0,  
  "aggs": { 
    "group_count": { 
      "filter": { 
        "term":{"gender": "男"} 
      }, 
      "aggs":{ 
        "avg_age":{ 
          "avg":{"field": "age"} 
        } 
      } 
    } 
  } 
}

【例子】统计男女员工的平均年龄

POST请求: http://192.168.1.74:9200/my-index/_search/ 
{ 
  "size": 0,  
  "aggs": { 
    "group_count": { 
      "filters":{ 
        "filters": [ 
          {"match":{"gender": "男"}}, 
          {"match":{"gender": "女"}} 
        ] 
      }, 
      "aggs":{ 
        "avg_age":{ 
            "avg":{"field": "age"} 
        } 
      } 
    } 
  } 
}

分组聚合之Range分组(RangeAggregation)

from..to区间范围是[from,to),也就是说包含from点,不包含to点 

【例子】查询薪资在[0,10000),[10000,20000),[2000,+无穷大)三个范围的员工数

POST请求: http://192.168.1.74:9200/my-index/_search/ 
{ 
  "size": 0,  
  "aggs": { 
    "group_count": { 
      "range": { 
        "field": "salary", 
        "ranges": [ 
            {"to": 10000}, 
            {"from": 10000,"to":20000},   
            {"from": 20000} 
        ] 
      } 
 
    } 
  } 
}

【例子】查询发布日期在2016-12-01之前、2016-12-01至2017-01-01、2017-01-01之后三个时间区间的文档数

POST请求:http://192.168.1.74:9200/website/_search/ 
{ 
   "size": 0,  
  "aggs": { 
    "group_count": { 
      "range": { 
        "field": "postdate", 
        "format":"yyyy-MM-dd", 
        "ranges": [ 
            {"to": "2016-12-01"}, 
            {"from": "2016-12-01","to":"2017-01-01"},   
            {"from": "2017-01-01"} 
        ] 
      } 
    } 
  } 
}

分组聚合之Date分组(Date Aggregation)

       专用于日期值的范围聚合。 这种聚合和正常范围聚合的主要区别在于,起始和结束值可以在日期数学表达式中表示,并且还可以指定返回起始和结束响应字段的日期格式。 请注意,此聚合包含from值并排除每个范围的值。

【例子】计算一年前之前发表的博文数和从一年前以来发表的博文总数

POST请求:http://192.168.1.74:9200/website/_search/ 
{ 
  "size": 0,  
  "aggs": { 
    "group_count": { 
      "range": { 
        "field": "postdate", 
        "format":"yyyy-MM-dd", 
        "ranges": [ 
            {"to": "now-12M/M"}, 
            {"from": "now-12M/M"} 
        ] 
      } 
    } 
  } 
}

 

声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

发表评论
搜索
KIKK导航

KIKK导航

排行榜
关注我们

一个IT知识分享的公众号