java使⽤elasticsearch分组进⾏聚合查询(groupby)-项⽬中实
际应⽤
java连接elasticsearch 进⾏聚合查询进⾏相应操作
⼀:对单个字段进⾏分组求和
1、表结构图⽚:
根据任务id分组,分别统计出每个任务id下有多少个⽂字标题
1.SQL:select id, count(*) as sum from task group by taskid;
java ES连接⼯具类
public class ESClientConnectionUtil {
public static TransportClient client=null;
public final static String HOST = "192.168.200.211"; //服务器部署
public final static Integer PORT = 9301; //端⼝
public static TransportClient  getESClient(){
System.setProperty("es.setty.runtime.available.processors", "false");
if (client == null) {
synchronized (ESClientConnectionUtil.class) {
try {
//设置集名称
Settings settings = Settings.builder().put("cluster.name", "es5").put("ansport.sniff", true).build();
//创建client
client = new PreBuiltTransportClient(settings).addTransportAddress(new ByName(HOST), PORT));
} catch (Exception ex) {
ex.printStackTrace();
System.out.Message());
}
}
}
return client;
}
public static TransportClient  getESClientConnection(){
if (client == null) {
System.setProperty("es.setty.runtime.available.processors", "false");
try {
//设置集名称
Settings settings = Settings.builder().put("cluster.name", "es5").put("ansport.sniff", true).build();
//创建client
client = new PreBuiltTransportClient(settings).addTransportAddress(new ByName(HOST), PORT));
} catch (Exception ex) {
ex.printStackTrace();
System.out.Message());
}
}
return client;
}
//判断索引是否存在
public static boolean judgeIndex(String index){
client= getESClientConnection();
IndicesAdminClient adminClient;
//查询索引是否存在
adminClient= client.admin().indices();
IndicesExistsRequest request = new IndicesExistsRequest(index);
IndicesExistsResponse responses = ists(request).actionGet();
if (responses.isExists()) {
return true;
}
return false;
}
}
java ES语句(根据单列进⾏分组求和)
//根据任务id分组进⾏求和
SearchRequestBuilder sbuilder = client.prepareSearch("hottopic").setTypes("hot");
//根据taskid进⾏分组统计,统计出的列别名叫sum
TermsAggregationBuilder termsBuilder = s("sum").field("taskid");
sbuilder.addAggregation(termsBuilder);
SearchResponse responses= ute().actionGet();
//得到这个分组的数据集合
Terms terms = Aggregations().get("sum");
List<BsKnowledgeInfoDTO> lists = new ArrayList<>();
for(int i=0;i&Buckets().size();i++){
//statistics
String id =Buckets().get(i).getKey().toString();//id
Long sum =Buckets().get(i).getDocCount();//数量
System.out.println("=="+Buckets().get(i).getDocCount()+"------"+Buckets().get(i).getKey());
}
//分别打印出统计的数量和id值
根据多列进⾏分组求和
//根据任务id分组进⾏求和
SearchRequestBuilder sbuilder = client.prepareSearch("hottopic").setTypes("hot");
//根据taskid进⾏分组统计,统计出的列别名叫sum
TermsAggregationBuilder termsBuilder = s("sum").field("taskid");
//根据第⼆个字段进⾏分组
groupby分组TermsAggregationBuilder aAggregationBuilder2 = s("region_count").field("birthplace");
//如果存在第三个,以此类推;
sbuilder.addAggregation(termsBuilder.subAggregation(aAggregationBuilder2));
SearchResponse responses= ute().actionGet();
//得到这个分组的数据集合
Terms terms = Aggregations().get("sum");
List<BsKnowledgeInfoDTO> lists = new ArrayList<>();
for(int i=0;i&Buckets().size();i++){
//statistics
String id =Buckets().get(i).getKey().toString();//id
Long sum =Buckets().get(i).getDocCount();//数量
System.out.println("=="+Buckets().get(i).getDocCount()+"------"+Buckets().get(i).getKey());
}
//分别打印出统计的数量和id值
对多个field求max/min/sum/avg
SearchRequestBuilder requestBuilder = client.prepareSearch("hottopic").setTypes("hot");
//根据taskid进⾏分组统计,统计别名为sum
TermsAggregationBuilder aggregationBuilder1 = s("sum").field("taskid")
//根据tasktatileid进⾏升序排列
.order(Order.aggregation("tasktatileid", true));
// 求tasktitleid 进⾏求平均数别名为avg_title
AggregationBuilder aggregationBuilder2 = AggregationBuilders.avg("avg_title").field("tasktitleid");
//
AggregationBuilder aggregationBuilder3 = AggregationBuilders.sum("sum_taskid").field("taskid");
requestBuilder.addAggregation(aggregationBuilder1.subAggregation(aggregationBuilder2).subAggregation(aggregationBuilder3));        SearchResponse response = ute().actionGet();
Terms aggregation = Aggregations().get("sum");
Avg terms2 = null;
Sum term3 = null;
for (Terms.Bucket bucket : Buckets()) {
terms2 = Aggregations().get("avg_title"); // org.elasticsearch.ics.avg.InternalAvg
term3 = Aggregations().get("sum_taskid"); // org.elasticsearch.ics.sum.InternalSum
System.out.println("编号=" + Key() + ";平均=" + Value() + ";总=" + Value());
}
如上内容若有不恰当⽀持,请各位多多包涵并进⾏点评。技术在于沟通!

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。