ES 条形图 histogram

ES 条形图 histogram

  • ES 中的聚合还有一个优点,可以很容易的转化成图表和图形,直方图 histogram

数据准备

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

使用

  • 需求:为售价创建一个直方图,可以将间隔设为 20,000,对每个区间内已售汽车的售价求和

  • GET /cars/transactions/_search
    {
       "size" : 0,
       "aggs":{
          "price":{
             "histogram":{ 
                "field": "price",
                "interval": 20000
             },
             "aggs":{
                "revenue": {
                   "sum": { 
                     "field" : "price"
                   }
                 }
             }
          }
       }
    }
    
  • 返回结果
    "aggregations": {
        "price": {
          "buckets": [
            {
              "key": 0,
              "doc_count": 3,
              "revenue": {
                "value": 37000
              }
            },
            {
              "key": 20000,
              "doc_count": 4,
              "revenue": {
                "value": 95000
              }
            },
            {
              "key": 40000,
              "doc_count": 0,
              "revenue": {
                "value": 0
              }
            },
            {
              "key": 60000,
              "doc_count": 0,
              "revenue": {
                "value": 0
              }
            },
            {
              "key": 80000,
              "doc_count": 1,
              "revenue": {
                "value": 80000
              }
            }
          ]
        }
      }
    
  • @Test
    public void test05(){
        HistogramAggregationBuilder histogramAggregationBuilder = AggregationBuilders.histogram("popular_price").field("price").interval(20000);
        SumAggregationBuilder sumAggregationBuilder = AggregationBuilders.sum("revenue").field("price");
        histogramAggregationBuilder.subAggregation(sumAggregationBuilder);
        SearchResponse searchResponse = elasticsearchTemplate.getClient().prepareSearch("cars")
                .setTypes("transactions")
                .addAggregation(histogramAggregationBuilder)
                .execute()
                .actionGet();
        InternalHistogram internalHistogram = searchResponse.getAggregations().get("popular_price");
        List<InternalHistogram.Bucket> buckets = internalHistogram.getBuckets();
        for (InternalHistogram.Bucket bucket : buckets){
            String keyAsString = bucket.getKeyAsString();
            System.out.print(keyAsString+"---");
            InternalSum internalSum = bucket.getAggregations().get("revenue");
            double value = internalSum.getValue();
            System.out.println(value);
        }
    }
    
  • 返回结果
    0.0---37000.0
    20000.0---95000.0
    40000.0---0.0
    60000.0---0.0
    80000.0---80000.0
    
  • 在这里插入图片描述

  • histogram 桶要求两个参数:一个数值字段以及一个定义桶大小间隔。

  • sum 度量嵌套在每个售价区间内,用来显示每个区间内的总收入。

  • 直方图的键值是区间的下限。键 0 代表区间 0-19,999 ,键 20000 代表区间 20,000-39,999等等

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值