ES单桶-Java API实现
数据准备
-
PUT cars { "mappings": { "transactions": { "properties": { "color": { "type": "keyword" }, "make": { "type": "text" }, "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" }
单桶
-
要求:哪个颜色的汽车销量最好
-
@Test public void test02(){ TermsAggregationBuilder termsAggregationBuilder = AggregationBuilders.terms("popular_colors").field("color"); SearchResponse searchResponse = elasticsearchTemplate.getClient().prepareSearch("cars") .setTypes("transactions") .addAggregation(termsAggregationBuilder) .execute() .actionGet(); StringTerms stringTerms = searchResponse.getAggregations().get("popular_colors"); List<StringTerms.Bucket> buckets = stringTerms.getBuckets(); for (StringTerms.Bucket bucket:buckets){ String key = bucket.getKeyAsString(); long count = bucket.getDocCount(); System.out.println(key+"---"+count); } }
-
查询结果 red---4 blue---2 green---2
-
AggregationBuilder抽象类的实现类由AggregationBuilders聚合条件构造器类创建
- 常用的聚合函数一般有Min(最小值)、Max(最大值)、Terms(精准匹配)、Avg(平均值)、ValueCount(数量)、Sum(总数)
- 返回值放在aggregations集合中,通过自定义的桶名称可以获取相应的StringTerms,聚合的结果存放在buckets集合中
- buckets集合中固定带有key(termBytes)和docCount(统计的数量)
添加度量指标
-
要求:每种颜色汽车的平均价格是多少?
-
@Test public void test02() { TermsAggregationBuilder termsAggregationBuilder = AggregationBuilders.terms("popular_colors").field("color"); AvgAggregationBuilder avgAggregationBuilder = AggregationBuilders.avg("avg_price").field("price"); termsAggregationBuilder.subAggregation(avgAggregationBuilder); SearchResponse searchResponse = elasticsearchTemplate.getClient().prepareSearch("cars") .setTypes("transactions") .addAggregation(termsAggregationBuilder) .execute() .actionGet(); StringTerms stringTerms = searchResponse.getAggregations().get("popular_colors"); List<StringTerms.Bucket> buckets = stringTerms.getBuckets(); for (StringTerms.Bucket bucket : buckets) { String key = bucket.getKeyAsString(); long count = bucket.getDocCount(); System.out.println(key + "---" + count); InternalAvg internalAvg = bucket.getAggregations().get("avg_price"); double value = internalAvg.getValue(); System.out.println(value); } }
-
先根据颜色分组,再求每个分组的平均值
- 返回值同样放在bucket里的aggregations中,不过注意的是,平均值取出来的类是InternalAvg,也是Aggregation接口的一个实现类
查询结果 red---4 32500.0 blue---2 20000.0 green---2 21000.0