美文网首页elasticsearch
elasticsearch aggregation

elasticsearch aggregation

作者: Grey____ | 来源:发表于2019-02-15 11:07 被阅读0次

简介

聚合可以看作是对查询结果的汇总。aggregation的强大在于它能嵌套并实现多级汇总。通常分为四类聚类:metric、bucket、pipeline、matrix
bucket和SQL的group by作用类似,常与metric结合使用,bucket是可以嵌套的;metric是对bucket中的一些统计信息;matrix在多字段上进行操作,从请求的文档的字段中提取信息,返回矩阵结果。

官方文档地址


单字段聚合

举例

第一步添加创建相关的数据
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" }
查询那个颜色的汽车销量最好?

使用http-restfull查询

GET /cars/transactions/_search
{
    "size" : 0,//不需要返回文档,所以直接设置为0.可以提高查询速度
    "aggs" : { //这个是aggregations的缩写,这边用户随意,可以写全称也可以缩写
        "popular_colors" : { //定义一个聚合的名字,与java的方法命名类似,建议用'_'线来分隔单词
            "terms" : { //定义单个桶(集合)的类型为 terms
              "field" : "color"(字段颜色进行分类,类似于sql中的group by color)
            }
        }
    }
}

使用java-api的形式查询

public void aggsTermsQuery() {
        SearchResponse response = transportClient.prepareSearch("cars")
                .setTypes("transactions")
                .addAggregation(
                        AggregationBuilders.terms("popular_colors")
                                .field("color"))
                .setSize(0)
                .get();
        Aggregation popular_colors = response.getAggregations().get("popular_colors");
    }

返回的结果

{
...
   "hits": {
      "hits": [] //因为我们设置了返回的文档数量为0,所以在这个文档里面是不会包含具体的文档的
   },
   "aggregations": {
      "popular_colors": { 
         "buckets": [
            {
               "key": "red", 
               "doc_count": 4 //在红色车子集合的数量
            },
            {
               "key": "blue",
               "doc_count": 2
            },
            {
               "key": "green",
               "doc_count": 2
            }
         ]
      }
   }
}

多字段聚合

先按照颜色划分,再汽车按照厂商划分(group by color, make)

http请求

GET /cars/transactions/_search
{
   "size" : 0,
   "aggs": {
      "colors": {
         "terms": {
            "field": "color"
         },
         "aggs": {
            "avg_price": { 
               "avg": {
                  "field": "price"
               }
            },
            "make": { //命名子集合的名字
                "terms": {
                    "field": "make" //按照字段'make'再次进行分类
                }
            }
         }
      }
   }
}

java-api请求方式

@Test
    public void subMertricsQuery(){
        SearchResponse response = transportClient.prepareSearch("cars")
                .setTypes("transactions")
                .addAggregation(
                        AggregationBuilders.terms("colors")
                                .field("color")
                                .subAggregation(AggregationBuilders
                                        .avg("avg_price")
                                        .field("price")
                                )
                                .subAggregation(AggregationBuilders
                                        .terms("make")//子集合的名字
                                        .field("make")//分类的字段
                                )
                )
                .setSize(0)
                .get();
        Aggregation colors = response.getAggregations().get("colors");
    }

返回结果

{
...
   "aggregations": {
      "colors": {
         "buckets": [
            {
               "key": "red",
               "doc_count": 4,
               "make": { //子集合的名字
                  "buckets": [
                     {
                        "key": "honda", 
                        "doc_count": 3
                     },
                     {
                        "key": "bmw",
                        "doc_count": 1
                     }
                  ]
               },
               "avg_price": {
                  "value": 32500 
               }
            },

...
}
在上面的结果基础上,在增加一个指标,就是查询出每个制造商生产的最贵和最便宜的车子的价格分别是多少

http请求

GET /cars/transactions/_search
{
   "size" : 0,
   "aggs": {
      "colors": {
         "terms": {
            "field": "color"
         },
         "aggs": {
            "avg_price": { "avg": { "field": "price" }
            },
            "make" : {
                "terms" : {
                    "field" : "make"
                },
                "aggs" : { 
                    "min_price" : { //自定义变量名字
                        "min": { //参数-最小值
                            "field": "price"
                            } 
                        }, 
                    "max_price" : {
                         "max": { //参数-最大值
                                 "field": "price"
                                 } 
                         } 
                }
            }
         }
      }
   }
}

java-api请求

@Test
    public void subMertricsQuery(){
        SearchResponse response = transportClient.prepareSearch("cars")
                .setTypes("transactions")
                .addAggregation(
                        AggregationBuilders.terms("colors")
                                .field("color")
                                .subAggregation(AggregationBuilders
                                        .avg("avg_price")
                                        .field("price")
                                )
                                .subAggregation(AggregationBuilders
                                        .terms("make")
                                        .field("make")
                                        .subAggregation(AggregationBuilders
                                                        .max("max_price")
                                                        .field("price")
                                        )
                                        .subAggregation(AggregationBuilders
                                                .min("min_price")
                                                .field("price")
                                        )
                                )
                )
                .setSize(0)
                .get();
        Aggregation colors = response.getAggregations().get("colors");
    }

返回结果

{
    ...
   "aggregations": {
      "colors": {
         "buckets": [
            {
               "key": "red",
               "doc_count": 4,
               "make": {
                  "buckets": [
                     {
                        "key": "honda",
                        "doc_count": 3,
                        "min_price": {
                           "value": 10000 
                        },
                        "max_price": {
                           "value": 20000 
                        }
                     },
                     {
                        "key": "bmw",
                        "doc_count": 1,
                        "min_price": {
                           "value": 80000
                        },
                        "max_price": {
                           "value": 80000
                        }
                     }
                  ]
               },
               "avg_price": {
                  "value": 32500
               }
            },
    ...

作者:ydw_武汉
来源:CSDN
原文:https://blog.csdn.net/ydwyyy/article/details/79487995
版权声明:本文为博主原创文章,转载请附上博文链接!


作者:haixwang
来源:CSDN
原文:https://blog.csdn.net/HaixWang/article/details/80350634
版权声明:本文为博主原创文章,转载请附上博文链接!

相关文章

网友评论

    本文标题:elasticsearch aggregation

    本文链接:https://www.haomeiwen.com/subject/dshpeqtx.html