(六)ElasticSearch 6.1.1聚合查询

本文详细介绍了Elasticsearch 6.1.1的聚合查询,包括基本操作如导入数据和terms桶,区间聚合如histogram桶,范围限定,结果排序以及nested类型的聚合查询。通过实例展示了如何统计各品牌汽车的销售总额,控制空桶返回,添加度量指标,以及对nested文档进行聚合。
摘要由CSDN通过智能技术生成

1 普通类型

1.1 基本操作

1.1.1 导入实战数据

数据字段如下:

字段 类型 作用
price long 汽车售价
color text 汽车颜色
make text 汽车品牌
sold date 销售日期
# 创建索引
PUT /cars
{
  "mappings" : {
      "transactions" : {
        "properties" : {
          "color" : {
            "type" : "keyword"
          },
          "make" : {
            "type" : "keyword"
          },
          "price" : {
            "type" : "long"
          },
          "sold" : {
            "type" : "date"
          }
        }
      }
    }
}

# 导入数据
POST /cars/transactions/_bulk
{ "index": {}}
{ "price" : 10000, "color" : "red", "make" : "honda", "sold" : "2014-10-28" }
{ "index": {}}
{ "price" : 20000, "color" : "red", "make" : "honda", "sold" : "2014-11-05" }
{ "index": {}}
{ "price" : 30000, "color" : "green", "make" : "ford", "sold" : "2014-05-18" }
{ "index": {}}
{ "price" : 15000, "color" : "blue", "make" : "toyota", "sold" : "2014-07-02" }
{ "index": {}}
{ "price" : 12000, "color" : "green", "make" : "toyota", "sold" : "2014-08-19" }
{ "index": {}}
{ "price" : 20000, "color" : "red", "make" : "honda", "sold" : "2014-11-05" }
{ "index": {}}
{ "price" : 80000, "color" : "red", "make" : "bmw", "sold" : "2014-01-01" }
{ "index": {}}
{ "price" : 25000, "color" : "blue", "make" : "ford", "sold" : "2014-02-12" }

1.1.2 最简单的聚合:terms桶

# 第一个聚合命令是terms桶,相当于SQL中的group by,将所有记录按照颜色聚合
GET /cars/transactions/_search
{
  "size":0,
  "aggs":{
   "popular_colors":{
     "terms": {
       "field": "color"
     }
   } 
  }
}

运行结果

1.1.3 添加度量指标

  • 上面的示例返回的是每个桶中的文档数量,接下es支持丰富的指标,例如平均值(Avg)、最大值(Max)、最小值(Min)、累加和(Sum)等,接下来试试累加和的用法;
  • 下面请求的作用是统计每种颜色汽车的销售总额:
GET /cars/transactions/_search
{
  "size":0,
  "aggs":{
   "colors":{
     "terms": {
       "field": "color"
     },
     "aggs":{
       "sales":{
         "sum":{
           "field":"price"
         }
       }
     }
   } 
  }
}

# 解释
GET /cars/transactions/_search
{
  "size":0,
  "aggs":{         ------和前面一样,指定聚合操作
   "colors":{      ------别名
     "terms": {    ------桶类型是按指定字段聚合
       "field": "color" ------按照color字段聚合
     },
     "aggs":{      ------新增的aggs对象,用于处理聚合在每个桶内的文档
       "sales":{   ------别名
         "sum":{   ------度量指标是指定字段求和
           "field":"price" ---求和的字段是price
         }
       }
     }
   } 
  }
}

 "aggregations" : {               ------聚合结果
    "colors" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [               ------这个json数组的每个对象代表一个桶
        {
          "key" : "red",        ------该桶将所有color等于red的文档聚合进来
          "doc_count" : 4,      ------有4个color等于red的文档
          "sales" : {           ------这里面是sum计算后的结果  
            "value" : 130000.0  ------所有color等于red的汽车销售总额
          }
        },
        {
          "key" : "blue",
          "doc_count" : 2,
          "sales" : {
            "value" : 40000.0  ------所有color等于blue的汽车销售总额
          }
        },

1.2 区间聚合

1.2.1条形图(histogram桶)

以汽车销售记录为例做一次聚合查询,为售价创建histogram桶,以20000作为间隔,每个桶负责的区间如上图所示,相关的销售记录就会被放入对应的桶中,请求参数和说明如下:

# 可执行查询
GET /cars/transactions/_search
{
  "size":0,                  
  "aggs":{                   
   "price":{                 
     "histogram": {          
       "field": "price",     
       "interval": 20000     
     }
   } 
  }
}
# 解释
GET /cars/transactions/_search
{
  "size":0,                  ---令返回值的hits对象为空
  "aggs":{                   ---聚合命令
   "price":{                 ---聚合字段名称
     "histogram": {          ---桶类型
       "field": "price",     ---指定price字段的值作为判断条件
       "interval": 20000     ---每个桶负责的区间大小为20000
     }
   } 
  }
}


# 返回结果
  "aggregations" : {             ---聚合结果
    "price" : {                  ---请求参数中指定的名称
      "buck
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值