其他分享
首页 > 其他分享> > elasticsearch group by sum avg max min

elasticsearch group by sum avg max min

作者:互联网

一:对单个字段进行分组求和

1、表结构图片:

根据任务id分组,分别统计出每个任务id下有多少个文字标题

 

  1. 1.SQL:select id, count(*) as sum from task group by taskid;  

java ES连接工具类

  1. public class ESClientConnectionUtil {
  2. public static TransportClient client=null;
  3. public final static String HOST = "192.168.200.211"; //服务器部署
  4. public final static Integer PORT = 9301; //端口
  5.  
  6. public static TransportClient getESClient(){
  7. System.setProperty("es.set.netty.runtime.available.processors", "false");
  8. if (client == null) {
  9. synchronized (ESClientConnectionUtil.class) {
  10. try {
  11. //设置集群名称
  12. Settings settings = Settings.builder().put("cluster.name", "es5").put("client.transport.sniff", true).build();
  13. //创建client
  14. client = new PreBuiltTransportClient(settings).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(HOST), PORT));
  15. } catch (Exception ex) {
  16. ex.printStackTrace();
  17.  
  18. System.out.println(ex.getMessage());
  19. }
  20. }
  21. }
  22. return client;
  23. }
  24. public static TransportClient getESClientConnection(){
  25. if (client == null) {
  26. System.setProperty("es.set.netty.runtime.available.processors", "false");
  27. try {
  28. //设置集群名称
  29. Settings settings = Settings.builder().put("cluster.name", "es5").put("client.transport.sniff", true).build();
  30. //创建client
  31. client = new PreBuiltTransportClient(settings).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(HOST), PORT));
  32. } catch (Exception ex) {
  33. ex.printStackTrace();
  34. System.out.println(ex.getMessage());
  35. }
  36. }
  37. return client;
  38. }
  39.  
  40. //判断索引是否存在
  41. public static boolean judgeIndex(String index){
  42. client= getESClientConnection();
  43. IndicesAdminClient adminClient;
  44. //查询索引是否存在
  45. adminClient= client.admin().indices();
  46. IndicesExistsRequest request = new IndicesExistsRequest(index);
  47. IndicesExistsResponse responses = adminClient.exists(request).actionGet();
  48.  
  49. if (responses.isExists()) {
  50. return true;
  51. }
  52. return false;
  53. }
  54. }

java ES语句(根据单列进行分组求和)

  1. //根据 任务id分组进行求和
  2. SearchRequestBuilder sbuilder = client.prepareSearch("hottopic").setTypes("hot");
    //根据taskid进行分组统计,统计出的列别名叫sum
  3. TermsAggregationBuilder termsBuilder = AggregationBuilders.terms("sum").field("taskid");
  4. sbuilder.addAggregation(termsBuilder);
  5. SearchResponse responses= sbuilder.execute().actionGet();
  6. //得到这个分组的数据集合
  7. Terms terms = responses.getAggregations().get("sum");
  8. List<BsKnowledgeInfoDTO> lists = new ArrayList<>();
  9. for(int i=0;i<terms.getBuckets().size();i++){
  10. //statistics
  11. String id =terms.getBuckets().get(i).getKey().toString();//id
  12. Long sum =terms.getBuckets().get(i).getDocCount();//数量
  13. System.out.println("=="+terms.getBuckets().get(i).getDocCount()+"------"+terms.getBuckets().get(i).getKey());
  14. }
    //分别打印出统计的数量和id值

根据多列进行分组求和

    1. //根据 任务id分组进行求和
    2. SearchRequestBuilder sbuilder = client.prepareSearch("hottopic").setTypes("hot");
    3. //根据taskid进行分组统计,统计出的列别名叫sum
    4. TermsAggregationBuilder termsBuilder = AggregationBuilders.terms("sum").field("taskid");
    5. //根据第二个字段进行分组
    6. TermsAggregationBuilder aAggregationBuilder2 = AggregationBuilders.terms("region_count").field("birthplace");
      //如果存在第三个,以此类推;
    7. sbuilder.addAggregation(termsBuilder.subAggregation(aAggregationBuilder2));
    8. SearchResponse responses= sbuilder.execute().actionGet();
    9. //得到这个分组的数据集合
    10. Terms terms = responses.getAggregations().get("sum");
    11. List<BsKnowledgeInfoDTO> lists = new ArrayList<>();
    12. for(int i=0;i<terms.getBuckets().size();i++){
    13. //statistics
    14. String id =terms.getBuckets().get(i).getKey().toString();//id
    15. Long sum =terms.getBuckets().get(i).getDocCount();//数量
    16. System.out.println("=="+terms.getBuckets().get(i).getDocCount()+"------"+terms.getBuckets().get(i).getKey());
    17. }
    18. //分别打印出统计的数量和id值

   

对多个field求max/min/sum/avg

  1. SearchRequestBuilder requestBuilder = client.prepareSearch("hottopic").setTypes("hot");
  2. //根据taskid进行分组统计,统计别名为sum
  3. TermsAggregationBuilder aggregationBuilder1 = AggregationBuilders.terms("sum").field("taskid")
    //根据tasktatileid进行升序排列
  4. .order(Order.aggregation("tasktatileid", true));
    // 求tasktitleid 进行求平均数 别名为avg_title
  5. AggregationBuilder aggregationBuilder2 = AggregationBuilders.avg("avg_title").field("tasktitleid");
    //
  6. AggregationBuilder aggregationBuilder3 = AggregationBuilders.sum("sum_taskid").field("taskid");
  7. requestBuilder.addAggregation(aggregationBuilder1.subAggregation(aggregationBuilder2).subAggregation(aggregationBuilder3));
  8. SearchResponse response = requestBuilder.execute().actionGet();
  9.  
  10. Terms aggregation = response.getAggregations().get("sum");
  11. Avg terms2 = null;
  12. Sum term3 = null;
  13. for (Terms.Bucket bucket : aggregation.getBuckets()) {
  14. terms2 = bucket.getAggregations().get("avg_title"); // org.elasticsearch.search.aggregations.metrics.avg.InternalAvg
  15. term3 = bucket.getAggregations().get("sum_taskid"); // org.elasticsearch.search.aggregations.metrics.sum.InternalSum
  16. System.out.println("编号=" + bucket.getKey() + ";平均=" + terms2.getValue() + ";总=" + term3.getValue());
  17. }

标签:group,min,max,sum,get,client,taskid,terms,id
来源: https://www.cnblogs.com/cfas/p/16377834.html