Elasticsearch-聚合

elasticsearch 聚合

概念:

大家都知道elasticsearch致力于查询数据(海量数据)犹如大海里面怎么捞针,es解决的就是大海捞针的方案。
聚合类似于DSL中的查询表达式,
也有条件,组合,分类。
有两个主要概念需要弄懂。

指标
我们来看下这个(类比)
select count(person) from table group by country
桶:count(person),sum(person),max(person)
指标:group by country

桶:文档的集合:
国籍:中国桶,美国桶,日本桶
性别:男人桶,女人桶,儿童桶
ES 有很多的桶划分方式:时间,年龄,地理位置,最受欢迎的词语
指标:一种计算的标准
平均值,最大的,最小的,总数等。
组合:桶+指标
中国总人数
中国平均年薪
举例
参考官网文档:
一个汽车4s店销售的汽车

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" }

需求:我们想知道那种颜色的车销量最好

curl -X GET "localhost:9200/cars/transactions/_search" -H 'Content-Type: application/json' -d'
{
    "size" : 0,
    "aggs" : { 
        "popular_colors" : { 
            "terms" : { 
              "field" : "color"
            }
        }
    }
}

解析:size:0 size设置为0因为我们不需要知道查询结果的具体内容只为了提高查询速度。
aggs(完整:aggregations同样有效)
桶:"terms" : { "field" : "color" }
指标:"popular_colors"
我们创建了一个颜色桶,terms桶会为每个颜色创建一个新桶。
运行结果:

{
...
   "hits": {
      "hits": [] 
   },
   "aggregations": {
      "popular_colors": { 
         "buckets": [
            {
               "key": "red", 
               "doc_count": 4 
            },
            {
               "key": "blue",
               "doc_count": 2
            },
            {
               "key": "green",
               "doc_count": 2
            }
         ]
      }
   }
}

上面我们可以想象成我们操作数据库时,从数据库中查询销量最好车的颜色。

由浅入深

(1)、嵌入指标

结合下面的例子理解
需求:我们想查询每种颜色汽车的平均价格?

GET /cars/transactions/_search
{
   "size" : 0,
   "aggs": {
      "colors": {
         "terms": {
            "field": "color"
         },
         "aggs": { 
            "avg_price": { 
               "avg": {
                  "field": "price" 
               }
            }
         }
      }
   }
}

结果:

{
...
   "aggregations": {
      "colors": {
         "buckets": [
            {
               "key": "red",
               "doc_count": 4,
               "avg_price": { 
                  "value": 32500
               }
            },
            {
               "key": "blue",
               "doc_count": 2,
               "avg_price": {
                  "value": 20000
               }
            },
            {
               "key": "green",
               "doc_count": 2,
               "avg_price": {
                  "value": 21000
               }
            }
         ]
      }
   }
...
}

从中我们就可以知道
红车平均价格:32500
蓝车平均价格:20000
绿车平均价格:21000

(2)、嵌套桶(桶中桶)

需求:我们想知道每种颜色的汽车制造商的分步?
在这里插入图片描述
仔细看一下桶和指标。
结果:`{

“aggregations”: {
“colors”: {
“buckets”: [
{
“key”: “red”,
“doc_count”: 4,
“make”: {
“buckets”: [
{
“key”: “honda”,
“doc_count”: 3
},
{
“key”: “bmw”,
“doc_count”: 1
}
]
},
“avg_price”: {
“value”: 32500
}
},


}`

猜你喜欢

转载自blog.csdn.net/weixin_39778417/article/details/85062828