日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java操作es聚合操作并显示其他字段_java使用elasticsearch分组进行聚合查询(group by)-项目中实际应用...

發布時間:2025/3/15 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java操作es聚合操作并显示其他字段_java使用elasticsearch分组进行聚合查询(group by)-项目中实际应用... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

java連接elasticsearch 進行聚合查詢進行相應操作

一:對單個字段進行分組求和

1、表結構圖片:

根據任務id分組,分別統計出每個任務id下有多少個文字標題

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

java ES連接工具類

public classESClientConnectionUtil {public static TransportClient client=null;public final static String HOST = "192.168.200.211"; //服務器部署

public final static Integer PORT = 9301; //端口

public staticTransportClient getESClient(){

System.setProperty("es.set.netty.runtime.available.processors", "false");if (client == null) {synchronized (ESClientConnectionUtil.class) {try{//設置集群名稱

Settings settings = Settings.builder().put("cluster.name", "es5").put("client.transport.sniff", true).build();//創建client

client = new PreBuiltTransportClient(settings).addTransportAddress(newInetSocketTransportAddress(InetAddress.getByName(HOST), PORT));

}catch(Exception ex) {

ex.printStackTrace();

System.out.println(ex.getMessage());

}

}

}returnclient;

}public staticTransportClient getESClientConnection(){if (client == null) {

System.setProperty("es.set.netty.runtime.available.processors", "false");try{//設置集群名稱

Settings settings = Settings.builder().put("cluster.name", "es5").put("client.transport.sniff", true).build();//創建client

client = new PreBuiltTransportClient(settings).addTransportAddress(newInetSocketTransportAddress(InetAddress.getByName(HOST), PORT));

}catch(Exception ex) {

ex.printStackTrace();

System.out.println(ex.getMessage());

}

}returnclient;

}//判斷索引是否存在

public static booleanjudgeIndex(String index){

client=getESClientConnection();

IndicesAdminClient adminClient;//查詢索引是否存在

adminClient=client.admin().indices();

IndicesExistsRequest request= newIndicesExistsRequest(index);

IndicesExistsResponse responses=adminClient.exists(request).actionGet();if(responses.isExists()) {return true;

}return false;

}

}

java ES語句(根據單列進行分組求和)

//根據 任務id分組進行求和

SearchRequestBuilder sbuilder = client.prepareSearch("hottopic").setTypes("hot");

//根據taskid進行分組統計,統計出的列別名叫sum

TermsAggregationBuilder termsBuilder= AggregationBuilders.terms("sum").field("taskid");

sbuilder.addAggregation(termsBuilder);

SearchResponse responses=sbuilder.execute().actionGet();//得到這個分組的數據集合

Terms terms = responses.getAggregations().get("sum");

List lists = new ArrayList<>();for(int i=0;i

String id =terms.getBuckets().get(i).getKey().toString();//id

Long sum =terms.getBuckets().get(i).getDocCount();//數量

System.out.println("=="+terms.getBuckets().get(i).getDocCount()+"------"+terms.getBuckets().get(i).getKey());

}

//分別打印出統計的數量和id值

根據多列進行分組求和

//根據 任務id分組進行求和

SearchRequestBuilder sbuilder = client.prepareSearch("hottopic").setTypes("hot");//根據taskid進行分組統計,統計出的列別名叫sum

TermsAggregationBuilder termsBuilder = AggregationBuilders.terms("sum").field("taskid");//根據第二個字段進行分組

TermsAggregationBuilder aAggregationBuilder2 = AggregationBuilders.terms("region_count").field("birthplace");

//如果存在第三個,以此類推;

sbuilder.addAggregation(termsBuilder.subAggregation(aAggregationBuilder2));

SearchResponse responses=sbuilder.execute().actionGet();//得到這個分組的數據集合

Terms terms = responses.getAggregations().get("sum");

List lists = new ArrayList<>();for(int i=0;i

String id =terms.getBuckets().get(i).getKey().toString();//id

Long sum =terms.getBuckets().get(i).getDocCount();//數量

System.out.println("=="+terms.getBuckets().get(i).getDocCount()+"------"+terms.getBuckets().get(i).getKey());

}//分別打印出統計的數量和id值

對多個field求max/min/sum/avg

SearchRequestBuilder requestBuilder =client.prepareSearch("hottopic").setTypes("hot");

//根據taskid進行分組統計,統計別名為sum

TermsAggregationBuilder aggregationBuilder1= AggregationBuilders.terms("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=requestBuilder.execute().actionGet();

Terms aggregation= response.getAggregations().get("sum");

Avg terms2= null;

Sum term3= null;for(Terms.Bucket bucket : 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操作es聚合操作并显示其他字段_java使用elasticsearch分组进行聚合查询(group by)-项目中实际应用...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。