构建数据:


   @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);
  }
}

ES的聚合操作的更多相关文章

  1. (转载)es进行聚合操作时提示Fielddata is disabled on text fields by default

    原文地址:http://blog.csdn.net/u011403655/article/details/71107415 根据es官网的文档执行 GET /megacorp/employee/_se ...

  2. (转)es进行聚合操作时提示Fielddata is disabled on text fields by default

    根据es官网的文档执行 GET /megacorp/employee/_search { "aggs": { "all_interests": { " ...

  3. es进行聚合操作时提示Fielddata is disabled on text fields by default

    在进行派粗前,先执行以下操作 { "properties": { "updatedate": { "type": "text&qu ...

  4. ElasticSearch 学习记录之ES几种常见的聚合操作

    ES几种常见的聚合操作 普通聚合 POST /product/_search { "size": 0, "aggs": { "agg_city&quo ...

  5. ES Terms 聚合数据不确定性

    Elasticsearch是一个分布式的搜索引擎,每个索引都可以有多个分片,用来将一份大索引的数据切分成多个小的物理索引,解决单个索引数据量过大导致的性能问题,另外每个shard还可以配置多个副本,来 ...

  6. 【ELK】4.spring boot 2.X集成ES spring-data-ES 进行CRUD操作 完整版+kibana管理ES的index操作

    spring boot 2.X集成ES 进行CRUD操作  完整版 内容包括: ============================================================ ...

  7. Elasticsearch学习(4) spring boot整合Elasticsearch的聚合操作

    之前已将spring boot原生方式介绍了,接下将结介绍的是Elasticsearch聚合操作.聚合操作一般来说是解决一下复杂的业务,比如mysql中的求和和分组,由于博主踩的坑比较多,所以博客可能 ...

  8. java es 骤合操作

    ElasticSearch java API - 聚合查询 以球员信息为例,player索引的player type包含5个字段,姓名,年龄,薪水,球队,场上位置.index的mapping为: &q ...

  9. 36.分组聚合操作—bucket进行多层嵌套

    主要知识点: 分组聚合操作-嵌套bucket.         本讲以前面电商实例,从颜色到品牌进行下钻分析,每种颜色的平均价格,以及找到每种颜色每个品牌的平均价格. 比如说,现在红色的电视有4台,同 ...

随机推荐

  1. js swich

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  2. 所遇Oracle错误代码

    ORA-00905: 缺失关键字   少了空格或关键字写错 ORA-00922: 选项缺失或无效 错误原因:一般是语句的语法有问题.比如命名不对,关键字写错等等.对于非标准的命名,一般采用双引号来创建 ...

  3. IntelliJ IDEA 部署 Web 项目,终于搞懂了!

    这篇牛逼: IDEA 中最重要的各种设置项,就是这个 Project Structre 了,关乎你的项目运行,缺胳膊少腿都不行. 最近公司正好也是用之前自己比较熟悉的IDEA而不是Eclipse,为了 ...

  4. vue-sticky组件详解

    sticky简介 sticky的本意是粘的,粘性的,使用其进行的布局被称为粘性布局. sticky是position属性新推出的值,属于CSS3的新特性,常用与实现吸附效果. 设置了sticky布局的 ...

  5. mysql两种备份方法总结:mysqldump 和 xtrabackup

    mysqldump工具基本使用 1. mysqldump [OPTIONS] database [tables…] 还原时库必须存在,不存在需要手动创建     --all-databases: 备份 ...

  6. 关于Windows 10上MarkdownPad2无法预览的解决办法

    升级win10后,发现一直可以用的MarkdownPad2预览功能不可以用了.于是在网上搜索了一下,刚开始没有解决.不过现在可以了.现在把解决方案记录下来.Windows10上使用MarkdownPa ...

  7. DRF框架之视图类

    前后端分离的项目 >: pip3 install djangorestframework   一.视图类传递参数给序列化类 视图层:views.py 需求: (1)在视图类中实列化对象是,可以设 ...

  8. Linux架构之Nginx 动静分离

    案例No.51:Nginx动静分离 1.web01配置静态资源 [root@web01 ~]# cd /etc/nginx/conf.d/#配置静态资源[root@web01 conf.d]# cat ...

  9. fhq_treap || BZOJ 3223: Tyvj 1729 文艺平衡树 || Luogu P3391 【模板】文艺平衡树(Splay)

    题面: [模板]文艺平衡树(Splay) 题解:无 代码: #include<cstdio> #include<cstring> #include<iostream> ...

  10. libevent cs

    int evutil_make_listen_socket_reuseable(evutil_socket_t sock): 相当于执行以下操作 int one = 1; setsockopt(soc ...