ElasticSearch7.3学习(二十九)----聚合实战之使用Java api实现电视案例

🚀 优质资源分享 🚀

学习路线指引(点击解锁) 知识定位 人群定位
🧡 Python实战微信订餐小程序 🧡 进阶级 本课程是python flask+微信小程序的完美结合,从项目搭建到腾讯云部署上线,打造一个全栈订餐系统。
💛Python量化交易实战💛 入门级 手把手带你打造一个易扩展、更安全、效率更高的量化交易系统

一、数据准备

创建索引及映射

建立价格、颜色、品牌、售卖日期字段

PUT /tvs
PUT /tvs/_mapping
{
  "properties": {
    "price": {
      "type": "long"
    },
    "color": {
      "type": "keyword"
    },
    "brand": {
      "type": "keyword"
    },
    "sold\_date": {
      "type": "date"
    }
  }
}

插入数据

POST /tvs/_bulk
{"index":{}}
{"price":1000,"color":"红色","brand":"长虹","sold\_date":"2019-10-28"}
{"index":{}}
{"price":2000,"color":"红色","brand":"长虹","sold\_date":"2019-11-05"}
{"index":{}}
{"price":3000,"color":"绿色","brand":"小米","sold\_date":"2019-05-18"}
{"index":{}}
{"price":1500,"color":"蓝色","brand":"TCL","sold\_date":"2019-07-02"}
{"index":{}}
{"price":1200,"color":"绿色","brand":"TCL","sold\_date":"2019-08-19"}
{"index":{}}
{"price":2000,"color":"红色","brand":"长虹","sold\_date":"2019-11-05"}
{"index":{}}
{"price":8000,"color":"红色","brand":"三星","sold\_date":"2020-01-01"}
{"index":{}}
{"price":2500,"color":"蓝色","brand":"小米","sold\_date":"2020-02-12"}

二、 按照颜色分组,计算每个颜色卖出的个数

ES语句

GET /tvs/_search
{
  "size": 0,
  "query": {
    "match\_all": {}
  },
  "aggs": {
    "group\_by\_color": {
      "terms": {
        "field": "color"
      }
    }
  }
}

返回

{
  "took" : 1,
  "timed\_out" : false,
  "\_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 8,
      "relation" : "eq"
    },
    "max\_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "group\_by\_color" : {
      "doc\_count\_error\_upper\_bound" : 0,
      "sum\_other\_doc\_count" : 0,
      "buckets" : [
        {
          "key" : "红色",
          "doc\_count" : 4
        },
        {
          "key" : "绿色",
          "doc\_count" : 2
        },
        {
          "key" : "蓝色",
          "doc\_count" : 2
        }
      ]
    }
  }
}

Java代码

//按照颜色分组,计算每个颜色卖出的个数
    @Test
    public void testAggs() throws IOException {
        //1 构建请求
        SearchRequest searchRequest=new SearchRequest("tvs");
        //请求体
        SearchSourceBuilder searchSourceBuilder=new SearchSourceBuilder();
        searchSourceBuilder.size(0);
        searchSourceBuilder.query(QueryBuilders.matchAllQuery());
        TermsAggregationBuilder termsAggregationBuilder = AggregationBuilders.terms("group\_by\_color").field("color");
        searchSourceBuilder.aggregation(termsAggregationBuilder);
        //请求体放入请求头
        searchRequest.source(searchSourceBuilder);
        //2 执行
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
        Aggregations aggregations = searchResponse.getAggregations();
        Terms group\_by\_color = aggregations.get("group\_by\_color");
        List <span class="hljs-keyword"extends Terms.Bucket> buckets = group_by_color.getBuckets();
        for (Terms.Bucket bucket : buckets) {
            String key = bucket.getKeyAsString();
            System.out.println("key:"+key);
            long docCount = bucket.getDocCount();
            System.out.println("docCount:"+docCount);
            System.out.println("=================================");
        }
    }

结果

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值