如何在java中使用elasticsearch进行分组
作者
本篇文章给大家分享的是有关如何在java中使用elasticsearch进行分组,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
java连接elasticsearch 进行聚合查询进行相应操作
一:对单个字段进行分组求和
1、表结构图片:
根据任务id分组,分别统计出每个任务id下有多少个文字标题
1.SQL:select id, count(*) as sum from task group by taskid;
java ES连接工具类
publicclassESClientConnectionUtil{ publicstaticTransportClientclient=null; publicfinalstaticStringHOST="192.168.200.211";//服务器部署 publicfinalstaticIntegerPORT=9301;//端口 publicstaticTransportClientgetESClient(){ System.setProperty("es.set.netty.runtime.available.processors","false"); if(client==null){ synchronized(ESClientConnectionUtil.class){ try{ //设置集群名称 Settingssettings=Settings.builder().put("cluster.name","es5").put("client.transport.sniff",true).build(); //创建client client=newPreBuiltTransportClient(settings).addTransportAddress(newInetSocketTransportAddress(InetAddress.getByName(HOST),PORT)); }catch(Exceptionex){ ex.printStackTrace(); System.out.println(ex.getMessage()); } } } returnclient; } publicstaticTransportClientgetESClientConnection(){ if(client==null){ System.setProperty("es.set.netty.runtime.available.processors","false"); try{ //设置集群名称 Settingssettings=Settings.builder().put("cluster.name","es5").put("client.transport.sniff",true).build(); //创建client client=newPreBuiltTransportClient(settings).addTransportAddress(newInetSocketTransportAddress(InetAddress.getByName(HOST),PORT)); }catch(Exceptionex){ ex.printStackTrace(); System.out.println(ex.getMessage()); } } returnclient; } //判断索引是否存在 publicstaticbooleanjudgeIndex(Stringindex){ client=getESClientConnection(); IndicesAdminClientadminClient; //查询索引是否存在 adminClient=client.admin().indices(); IndicesExistsRequestrequest=newIndicesExistsRequest(index); IndicesExistsResponseresponses=adminClient.exists(request).actionGet(); if(responses.isExists()){ returntrue; } returnfalse; } }
java ES语句(根据单列进行分组求和)
//根据任务id分组进行求和 SearchRequestBuildersbuilder=client.prepareSearch("hottopic").setTypes("hot"); //根据taskid进行分组统计,统计出的列别名叫sum TermsAggregationBuildertermsBuilder=AggregationBuilders.terms("sum").field("taskid"); sbuilder.addAggregation(termsBuilder); SearchResponseresponses=sbuilder.execute().actionGet(); //得到这个分组的数据集合 Termsterms=responses.getAggregations().get("sum"); List<BsKnowledgeInfoDTO>lists=newArrayList<>(); for(inti=0;i<terms.getBuckets().size();i++){ //statistics Stringid=terms.getBuckets().get(i).getKey().toString();//id Longsum=terms.getBuckets().get(i).getDocCount();//数量 System.out.println("=="+terms.getBuckets().get(i).getDocCount()+"------"+terms.getBuckets().get(i).getKey()); } //分别打印出统计的数量和id值
根据多列进行分组求和
//根据任务id分组进行求和 SearchRequestBuildersbuilder=client.prepareSearch("hottopic").setTypes("hot"); //根据taskid进行分组统计,统计出的列别名叫sum TermsAggregationBuildertermsBuilder=AggregationBuilders.terms("sum").field("taskid"); //根据第二个字段进行分组 TermsAggregationBuilderaAggregationBuilder2=AggregationBuilders.terms("region_count").field("birthplace"); //如果存在第三个,以此类推; sbuilder.addAggregation(termsBuilder.subAggregation(aAggregationBuilder2)); SearchResponseresponses=sbuilder.execute().actionGet(); //得到这个分组的数据集合 Termsterms=responses.getAggregations().get("sum"); List<BsKnowledgeInfoDTO>lists=newArrayList<>(); for(inti=0;i<terms.getBuckets().size();i++){ //statistics Stringid=terms.getBuckets().get(i).getKey().toString();//id Longsum=terms.getBuckets().get(i).getDocCount();//数量 System.out.println("=="+terms.getBuckets().get(i).getDocCount()+"------"+terms.getBuckets().get(i).getKey()); } //分别打印出统计的数量和id值
对多个field求max/min/sum/avg
SearchRequestBuilderrequestBuilder=client.prepareSearch("hottopic").setTypes("hot"); //根据taskid进行分组统计,统计别名为sum TermsAggregationBuilderaggregationBuilder1=AggregationBuilders.terms("sum").field("taskid") //根据tasktatileid进行升序排列 .order(Order.aggregation("tasktatileid",true)); //求tasktitleid进行求平均数别名为avg_title AggregationBuilderaggregationBuilder2=AggregationBuilders.avg("avg_title").field("tasktitleid"); // AggregationBuilderaggregationBuilder3=AggregationBuilders.sum("sum_taskid").field("taskid"); requestBuilder.addAggregation(aggregationBuilder1.subAggregation(aggregationBuilder2).subAggregation(aggregationBuilder3)); SearchResponseresponse=requestBuilder.execute().actionGet(); Termsaggregation=response.getAggregations().get("sum"); Avgterms2=null; Sumterm3=null; for(Terms.Bucketbucket:aggregation.getBuckets()){ terms2=bucket.getAggregations().get("avg_title");//org.elasticsearch.search.aggregations.metrics.avg.InternalAvg term3=bucket.getAggregations().get("sum_taskid");//org.elasticsearch.search.aggregations.metrics.sum.InternalSum System.out.println("编号="+bucket.getKey()+";平均="+terms2.getValue()+";总="+term3.getValue()); }
以上就是如何在java中使用elasticsearch进行分组,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注恰卡编程网行业资讯频道。
目录
推荐阅读
0 条评论
本站已关闭游客评论,请登录或者注册后再评论吧~