本篇文章給大家分享的是有關(guān)如何在java中使用elasticsearch進(jìn)行分組,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
公司主營業(yè)務(wù):成都網(wǎng)站制作、成都做網(wǎng)站、移動網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實(shí)現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競爭能力。成都創(chuàng)新互聯(lián)是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊(duì)。公司秉承以“開放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對我們的高要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會用頭腦與智慧不斷的給客戶帶來驚喜。成都創(chuàng)新互聯(lián)推出孟津免費(fèi)做網(wǎng)站回饋大家。
java連接elasticsearch 進(jìn)行聚合查詢進(jìn)行相應(yīng)操作
一:對單個(gè)字段進(jìn)行分組求和
1、表結(jié)構(gòu)圖片:
根據(jù)任務(wù)id分組,分別統(tǒng)計(jì)出每個(gè)任務(wù)id下有多少個(gè)文字標(biāo)題
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"; //服務(wù)器部署 public final static Integer PORT = 9301; //端口 public static TransportClient getESClient(){ System.setProperty("es.set.netty.runtime.available.processors", "false"); if (client == null) { synchronized (ESClientConnectionUtil.class) { try { //設(shè)置集群名稱 Settings settings = Settings.builder().put("cluster.name", "es5").put("client.transport.sniff", true).build(); //創(chuàng)建client client = new PreBuiltTransportClient(settings).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(HOST), PORT)); } catch (Exception ex) { ex.printStackTrace(); System.out.println(ex.getMessage()); } } } return client; } public static TransportClient getESClientConnection(){ if (client == null) { System.setProperty("es.set.netty.runtime.available.processors", "false"); try { //設(shè)置集群名稱 Settings settings = Settings.builder().put("cluster.name", "es5").put("client.transport.sniff", true).build(); //創(chuàng)建client client = new PreBuiltTransportClient(settings).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(HOST), PORT)); } catch (Exception ex) { ex.printStackTrace(); System.out.println(ex.getMessage()); } } return client; } //判斷索引是否存在 public static boolean judgeIndex(String index){ client= getESClientConnection(); IndicesAdminClient adminClient; //查詢索引是否存在 adminClient= client.admin().indices(); IndicesExistsRequest request = new IndicesExistsRequest(index); IndicesExistsResponse responses = adminClient.exists(request).actionGet(); if (responses.isExists()) { return true; } return false; } }
java ES語句(根據(jù)單列進(jìn)行分組求和)
//根據(jù) 任務(wù)id分組進(jìn)行求和 SearchRequestBuilder sbuilder = client.prepareSearch("hottopic").setTypes("hot"); //根據(jù)taskid進(jìn)行分組統(tǒng)計(jì),統(tǒng)計(jì)出的列別名叫sum TermsAggregationBuilder termsBuilder = AggregationBuilders.terms("sum").field("taskid"); sbuilder.addAggregation(termsBuilder); SearchResponse responses= sbuilder.execute().actionGet(); //得到這個(gè)分組的數(shù)據(jù)集合 Terms terms = responses.getAggregations().get("sum"); Listlists = new ArrayList<>(); for(int i=0;i 根據(jù)多列進(jìn)行分組求和
//根據(jù) 任務(wù)id分組進(jìn)行求和 SearchRequestBuilder sbuilder = client.prepareSearch("hottopic").setTypes("hot"); //根據(jù)taskid進(jìn)行分組統(tǒng)計(jì),統(tǒng)計(jì)出的列別名叫sum TermsAggregationBuilder termsBuilder = AggregationBuilders.terms("sum").field("taskid"); //根據(jù)第二個(gè)字段進(jìn)行分組 TermsAggregationBuilder aAggregationBuilder2 = AggregationBuilders.terms("region_count").field("birthplace"); //如果存在第三個(gè),以此類推; sbuilder.addAggregation(termsBuilder.subAggregation(aAggregationBuilder2)); SearchResponse responses= sbuilder.execute().actionGet(); //得到這個(gè)分組的數(shù)據(jù)集合 Terms terms = responses.getAggregations().get("sum"); Listlists = new ArrayList<>(); for(int i=0;i 對多個(gè)field求max/min/sum/avg
SearchRequestBuilder requestBuilder = client.prepareSearch("hottopic").setTypes("hot"); //根據(jù)taskid進(jìn)行分組統(tǒng)計(jì),統(tǒng)計(jì)別名為sum TermsAggregationBuilder aggregationBuilder1 = AggregationBuilders.terms("sum").field("taskid") //根據(jù)tasktatileid進(jìn)行升序排列 .order(Order.aggregation("tasktatileid", true)); // 求tasktitleid 進(jìn)行求平均數(shù) 別名為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中使用elasticsearch進(jìn)行分組,小編相信有部分知識點(diǎn)可能是我們?nèi)粘9ぷ鲿姷交蛴玫降?。希望你能通過這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)頁標(biāo)題:如何在java中使用elasticsearch進(jìn)行分組
文章路徑:http://weahome.cn/article/ggoise.html