ES的聚合操作

构建数据:


   @Test
   public void createIndex(){
       /**
        * 创建索引
        * */
       client.admin().indices().prepareCreate("player").get();
  }



   /**
    * 创建映射
    */
   @Test
   public void testCreateIndexMapping_boost() throws Exception{
       /**
        * 格式:
        "mappings": {
           "player": {
               "properties": {
                    "name": {"index": "not_analyzed","type": "string"},
                   "age": {"type": "integer"},
                   "salary": {"type": "integer"},
                   "team": {"index": "not_analyzed","type": "string"},
                   "position": {"index": "not_analyzed","type": "string"}
               }
           }
        }

        */
       //构建json的数据格式,创建映射
       XContentBuilder mappingBuilder = XContentFactory.jsonBuilder()
              .startObject()
              .startObject("player")
              .startObject("properties")
              .startObject("name").field("type","string").field("index", "not_analyzed").endObject()
              .startObject("age").field("type","integer").endObject()
              .startObject("salary").field("type","integer").endObject()
              .startObject("team").field("type","string").field("index", "not_analyzed").endObject()
              .startObject("position").field("type","string").field("index", "not_analyzed").endObject()
              .endObject()
              .endObject()
              .endObject();
       PutMappingRequest request = Requests.putMappingRequest("player")
              .type("player")
              .source(mappingBuilder);
       client.admin().indices().putMapping(request).get();
  }

   @Test
   public void BulkInsertDocument() throws IOException {
       BulkRequestBuilder bulkRequest = client.prepareBulk();

// either use client#prepare, or use Requests# to directly build index/delete requests
       bulkRequest.add(client.prepareIndex("player", "player", "1")
              .setSource(jsonBuilder()
                      .startObject()
                      .field("name", "郭德纲")
                      .field("age", 33)
                      .field("salary",3000)
                      .field("team" , "cav")
                      .field("position" , "sf")
                      .endObject()
              )
      );
       bulkRequest.add(client.prepareIndex("player", "player", "2")
              .setSource(jsonBuilder()
                      .startObject()
                      .field("name", "于谦")
                      .field("age", 25)
                      .field("salary",2000)
                      .field("team" , "cav")
                      .field("position" , "pg")
                      .endObject()
              )
      );
       bulkRequest.add(client.prepareIndex("player", "player", "3")
              .setSource(jsonBuilder()
                      .startObject()
                      .field("name", "岳云鹏")
                      .field("age", 29)
                      .field("salary",1000)
                      .field("team" , "war")
                      .field("position" , "pg")
                      .endObject()
              )
      );
       bulkRequest.add(client.prepareIndex("player", "player", "4")
              .setSource(jsonBuilder()
                      .startObject()
                      .field("name", "孙越")
                      .field("age", 26)
                      .field("salary",2000)
                      .field("team" , "war")
                      .field("position" , "sg")
                      .endObject()
              )
      );
       bulkRequest.add(client.prepareIndex("player", "player", "5")
              .setSource(jsonBuilder()
                      .startObject()
                      .field("name", "张云雷")
                      .field("age", 26)
                      .field("salary",2000)
                      .field("team" , "war")
                      .field("position" , "pf")
                      .endObject()
              )
      );
       bulkRequest.add(client.prepareIndex("player", "player", "6")
              .setSource(jsonBuilder()
                      .startObject()
                      .field("name", "爱迪生")
                      .field("age", 40)
                      .field("salary",1000)
                      .field("team" , "tim")
                      .field("position" , "pf")
                      .endObject()
              )
      );
       bulkRequest.add(client.prepareIndex("player", "player", "7")
              .setSource(jsonBuilder()
                      .startObject()
                      .field("name", "牛顿")
                      .field("age", 21)
                      .field("salary",500)
                      .field("team" , "tim")
                      .field("position" , "c")
                      .endObject()
              )
      );
       bulkRequest.add(client.prepareIndex("player", "player", "4")
              .setSource(jsonBuilder()
                      .startObject()
                      .field("name", "爱因斯坦")
                      .field("age", 21)
                      .field("salary",300)
                      .field("team" , "tim")
                      .field("position" , "sg")
                      .endObject()
              )
      );
       bulkRequest.add(client.prepareIndex("player", "player", "8")
              .setSource(jsonBuilder()
                      .startObject()
                      .field("name", "特斯拉")
                      .field("age", 20)
                      .field("salary",500)
                      .field("team" , "tim")
                      .field("position" , "sf")
                      .endObject()
              )
      );


       BulkResponse bulkResponse = bulkRequest.get();
       if (bulkResponse.hasFailures()) {
           // process failures by iterating through each bulk response item
      }
  }

1:分组求count

计算每个球队的球员数量:

select team, count(*) as player_count from player group by team;

@Test
public void groupAndCount() {
   //1:构建查询提交
   SearchRequestBuilder builder = client.prepareSearch("player").setTypes("player");
   //2:指定聚合条件
   TermsAggregationBuilder team = AggregationBuilders.terms("player_count").field("team");
   //3:将聚合条件放入查询条件中
   builder.addAggregation(team);
   //4:执行action,返回searchResponse
   SearchResponse searchResponse = builder.get();
   //5:将查询返回的searchResponse转换成map
   Map<String, Aggregation> aggregationMap = searchResponse.getAggregations().asMap();
   //6:取出聚合的字段
   StringTerms teamTerms = (StringTerms)aggregationMap.get("player_count");
   System.out.println(teamTerms);
   //7:对聚合的字段进行迭代
   Iterator<StringTerms.Bucket> iterator = teamTerms.getBuckets().iterator();
   while (iterator.hasNext()){
       StringTerms.Bucket bucket = iterator.next();
       System.out.println("每个球队 :" + bucket.getKey() + "有 【"+bucket.getDocCount()+"】 个人");
  }

}

2:Group by 多个字段

计算每个球队每个位置的球员数 select team, position, count(*) as pos_count from player group by team,position;

/**
* group by 多个字段
* 计算每个球队每个位置的球员数
* select team, position, count(*) as pos_count from player group by team, position;
* */
@Test
public void groupMutilFields() {
   //1:构建查询提交
   SearchRequestBuilder builder = client.prepareSearch("player").setTypes("player");
   //2:指定聚合条件
   TermsAggregationBuilder team = AggregationBuilders.terms("player_count").field("team");
   TermsAggregationBuilder potition = AggregationBuilders.terms("position_count").field("position");
   //注意父子关系
team.subAggregation(potition);
   //3:将聚合条件放入查询条件中
   builder.addAggregation(team).addAggregation(potition);
   //4:执行action,返回searchResponse
   SearchResponse searchResponse = builder.get();
   //5:将查询返回的searchResponse转换成map
   Map<String, Aggregation> aggregationMap = searchResponse.getAggregations().asMap();
   //6:取出聚合的字段
   StringTerms teamTerms = (StringTerms)aggregationMap.get("player_count");
   //7:对聚合的字段进行迭代
   Iterator<StringTerms.Bucket> iterator = teamTerms.getBuckets().iterator();
   while (iterator.hasNext()){
       StringTerms.Bucket bucket = iterator.next();
       //8:获取所有子聚合
       Map<String, Aggregation> position_map = bucket.getAggregations().asMap();
       StringTerms potitionTerms = (StringTerms)position_map.get("position_count");
       //9:对子集合下面的内容迭代 队名--位置--球员
       Iterator<StringTerms.Bucket> sub_iterator = potitionTerms.getBuckets().iterator();
       while (sub_iterator.hasNext()){
           StringTerms.Bucket sub_bucket = sub_iterator.next();
           System.out.println("球队 :" + bucket.getKey() + "下面的 "+sub_bucket.getKey()+"的位置 有"+sub_bucket.getDocCount());
      }
  }
}

3:分组求最大

计算每个球队年龄最大/最小/总/平均的球员年龄

select team, max(age) as max_age from player group by team;


/**
* 分组求:最大值、最小值、平均值
* 计算每个球队年龄最大/最小/总/平均的球员年龄
select team, max(age) as max_age from player group by team;
*
* */
@Test
public void groupMax(){
   //1:构建查询提交
   SearchRequestBuilder builder = client.prepareSearch("player").setTypes("player");
   //2:指定聚合条件
   TermsAggregationBuilder team = AggregationBuilders.terms("player_count").field("team");
   //3: 指定要查哪一个字段的最大值
   MaxAggregationBuilder ageFiled = AggregationBuilders.max("max_age").field("age");
   //: 找到聚合关系:父子关系
   team.subAggregation(ageFiled);
   //3:将聚合条件放入查询条件中
   builder.addAggregation(team);
   //4:执行action,返回searchResponse
   SearchResponse searchResponse = builder.get();
   //5:将查询返回的searchResponse转换成map
   Map<String, Aggregation> aggregationMap = searchResponse.getAggregations().asMap();
   //6:取出聚合的字段
   StringTerms teamTerms = (StringTerms)aggregationMap.get("player_count");
   //7:对聚合的字段进行迭代
   Iterator<StringTerms.Bucket> iterator = teamTerms.getBuckets().iterator();
   while (iterator.hasNext()){
       StringTerms.Bucket bucket = iterator.next();
       //8:获取所有子聚合
       Map age_map = bucket.getAggregations().asMap();
       int age = (int)((InternalMax) age_map.get("max_age")).getValue();
       System.out.println("球队 :" + bucket.getKey() + " 最大年龄: "+age);
  }
}

4:分组求最小

计算每个球队年龄最大/最小/总/平均的球员年龄 select team, min(age) as max_age from player group by team;

/**
* 分组求:最大值、最小值、平均值
* 计算每个球队年龄最大/最小/总/平均的球员年龄
select team, min(age) as max_age from player group by team;
*
* */
@Test
public void groupMin(){
   //1:构建查询提交
   SearchRequestBuilder builder = client.prepareSearch("player").setTypes("player");
   //2:指定聚合条件
   TermsAggregationBuilder team = AggregationBuilders.terms("player_count").field("team");
   //3: 指定要查哪一个字段的最大值
   MinAggregationBuilder ageFiled = AggregationBuilders.min("min_age").field("age");
   //: 找到聚合关系:父子关系
   team.subAggregation(ageFiled);
   //3:将聚合条件放入查询条件中
   builder.addAggregation(team);
   //4:执行action,返回searchResponse
   SearchResponse searchResponse = builder.get();
   //5:将查询返回的searchResponse转换成map
   Map<String, Aggregation> aggregationMap = searchResponse.getAggregations().asMap();
   //6:取出聚合的字段
   StringTerms teamTerms = (StringTerms)aggregationMap.get("player_count");
   //7:对聚合的字段进行迭代
   Iterator<StringTerms.Bucket> iterator = teamTerms.getBuckets().iterator();
   while (iterator.hasNext()){
       StringTerms.Bucket bucket = iterator.next();
       //8:获取所有子聚合
       Map age_map = bucket.getAggregations().asMap();
       int age = (int)((InternalMin) age_map.get("max_age")).getValue();
       System.out.println("球队 :" + bucket.getKey() + " 最小年龄: "+age);
  }
}

5:分组求平均值

计算每个球队年龄最大/最小/总/平均的球员年龄 select team, min(age) as max_age from player group by team;

/**
* 分组求:最大值、最小值、平均值
* 计算每个球队年龄最大/最小/总/平均的球员年龄
select team, min(age) as max_age from player group by team;
*
* */
@Test
public void groupAvg(){
   //1:构建查询提交
   SearchRequestBuilder builder = client.prepareSearch("player").setTypes("player");
   //2:指定聚合条件
   TermsAggregationBuilder team = AggregationBuilders.terms("player_count").field("team");
   //3: 指定要查哪一个字段的最大值
   AvgAggregationBuilder ageFiled = AggregationBuilders.avg("avg_age").field("age");
   //: 找到聚合关系:父子关系
   team.subAggregation(ageFiled);
   //3:将聚合条件放入查询条件中
   builder.addAggregation(team);
   //4:执行action,返回searchResponse
   SearchResponse searchResponse = builder.get();
   //5:将查询返回的searchResponse转换成map
   Map<String, Aggregation> aggregationMap = searchResponse.getAggregations().asMap();
   //6:取出聚合的字段
   StringTerms teamTerms = (StringTerms)aggregationMap.get("player_count");
   //7:对聚合的字段进行迭代
   Iterator<StringTerms.Bucket> iterator = teamTerms.getBuckets().iterator();
   while (iterator.hasNext()){
       StringTerms.Bucket bucket = iterator.next();
       //8:获取所有子聚合
       Map age_map = bucket.getAggregations().asMap();
       Double age = ((InternalAvg) age_map.get("avg_age")).getValue();
       System.out.println("球队 :" + bucket.getKey() + " 平均年龄: "+age);
  }
}

6:分组求和

计算每个球队球员的平均年龄,同时又要计算总年薪 select team, avg(age)as avg_age, sum(salary) as total_salary from player group by team;

/**
* 分组求:最大值、最小值、平均值
* 计算每个球队球员的平均年龄,同时又要计算总年薪
select team, avg(age)as avg_age, sum(salary) as total_salary from player group by team;
*
* */
@Test
public void groupsum(){
   //1:构建查询提交
   SearchRequestBuilder builder = client.prepareSearch("player").setTypes("player");
   //2:指定聚合条件
   TermsAggregationBuilder team = AggregationBuilders.terms("player_count").field("team");
   //3: 指定要查哪一个字段
   AvgAggregationBuilder ageFiled = AggregationBuilders.avg("avg_age").field("age");

   SumAggregationBuilder salaryField= AggregationBuilders.sum("sum_salary").field("salary");
   //: 找到聚合关系:父子关系
   team.subAggregation(ageFiled).subAggregation(salaryField);
   //3:将聚合条件放入查询条件中
   builder.addAggregation(team);
   //4:执行action,返回searchResponse
   SearchResponse searchResponse = builder.get();
   //5:将查询返回的searchResponse转换成map
   Map<String, Aggregation> aggregationMap = searchResponse.getAggregations().asMap();
   //6:取出聚合的字段
   StringTerms teamTerms = (StringTerms)aggregationMap.get("player_count");
   //7:对聚合的字段进行迭代
   Iterator<StringTerms.Bucket> iterator = teamTerms.getBuckets().iterator();
   while (iterator.hasNext()){
       StringTerms.Bucket bucket = iterator.next();
       //8:获取所有子聚合
       Map age_map = bucket.getAggregations().asMap();

       Double age = ((InternalAvg) age_map.get("avg_age")).getValue();
       double sum_salary = ((InternalSum) age_map.get("sum_salary")).getValue();
       System.out.println("球队 :" + bucket.getKey() + " 平均年龄: "+age + " 球队总salary:" + sum_salary);
  }
}

7:聚合排序

计算每个球队总年薪,并按照总年薪倒序排列 select team, sum(salary) as total_salary from player group by team order by total_salary desc;

/**
* 排序
计算每个球队总年薪,并按照总年薪倒序排列
select team, sum(salary) as total_salary from player group by team order by total_salary desc;
* */
@Test
public void groupOrder(){
   /**
    *
    * TermsBuilder teamAgg= AggregationBuilders.terms("team").order(Order.aggregation("total_salary ", false);
    SumBuilder salaryAgg= AggregationBuilders.avg("total_salary ").field("salary");
    sbuilder.addAggregation(teamAgg.subAggregation(salaryAgg));
    * */

   //1:构建查询提交
   SearchRequestBuilder builder = client.prepareSearch("player").setTypes("player");
   //2:指定聚合条件
   TermsAggregationBuilder team = AggregationBuilders.terms("player_count").field("team")
          .order(Terms.Order.aggregation("total_salary", false));//false是降序,true是升序
   //3: 指定要查哪一个字段
   SumAggregationBuilder salaryField= AggregationBuilders.sum("total_salary").field("salary");
   //: 找到聚合关系:父子关系
   team.subAggregation(salaryField);
   //3:将聚合条件放入查询条件中
   builder.addAggregation(team);
   //4:执行action,返回searchResponse
   SearchResponse searchResponse = builder.get();
   //5:将查询返回的searchResponse转换成map
   Map<String, Aggregation> aggregationMap = searchResponse.getAggregations().asMap();
   //6:取出聚合的字段
   StringTerms teamTerms = (StringTerms)aggregationMap.get("player_count");
   //7:对聚合的字段进行迭代
   Iterator<StringTerms.Bucket> iterator = teamTerms.getBuckets().iterator();
   while (iterator.hasNext()){
       StringTerms.Bucket bucket = iterator.next();
       //8:获取所有子聚合
       Map age_map = bucket.getAggregations().asMap();

       double sum_salary = ((InternalSum) age_map.get("total_salary")).getValue();
       System.out.println("球队 :" + bucket.getKey() + " 球队总salary:" + sum_salary);
  }
}

 

posted @ 2017-05-22 23:36  niutao  阅读(574)  评论(0编辑  收藏  举报