java 通過聚合查詢實(shí)現(xiàn)elasticsearch的group by后的數(shù)量
通過聚合查詢獲取group by 后的數(shù)量
/**
* 獲取key的個數(shù)
*
* @param key 要group by的字段名
* @param index 索引名稱
* @return id的個數(shù)
*/
public static int getKeyCount(String key, String index) {
int count = 0;
TransportClient client = null;
try {
client = connectionPool.getConnection();
if (client == null) {
throw new Exception("沒有獲取到連接!");
}
SearchRequestBuilder search = client.prepareSearch(index);
//cardinality聚合查詢,相當(dāng)于groupby字段名
SearchResponse sr = search.addAggregation(AggregationBuilders.cardinality(key + "_count").field(key)).execute().actionGet();
//從返回?cái)?shù)據(jù)提取id總數(shù)
Cardinality result = sr.getAggregations().get(key + "_count");
long value = result.getValue();
count = (int) value;
} catch (InterruptedException e) {
} catch (Exception e) {
logger.error("getKeyCount錯誤", e);
} finally {
connectionPool.releaseConnection(client);
}
return count;
}
獲取group by后的所有key值
/**
* 獲取所有key
*
* @param key 被group by的字段名
* @param index 索引名稱
* @return 所有id
*/
public static List<String> getAllKey(String key, String index) {
int keyCount = getKeyCount(key, index);
List<String> strings = new ArrayList<>();
TransportClient client = null;
try {
client = connectionPool.getConnection();
if (client == null) {
throw new Exception("沒有獲取到數(shù)據(jù)庫連接!");
}
SearchRequestBuilder searchRequestBuilder = client.prepareSearch(index);
//使用聚合,實(shí)現(xiàn)去重查詢
SearchResponse searchResponse = searchRequestBuilder.
addAggregation(AggregationBuilders.terms("models").field(key).size(keyCount)).execute().actionGet();
Terms term = searchResponse.getAggregations().get("models");
List<? extends Terms.Bucket> buckets = term.getBuckets();
//遍歷結(jié)果,提取出id
for (Terms.Bucket bucket : buckets) {
String keyAsString = bucket.getKeyAsString();
strings.add(keyAsString);
}
buckets.clear();
} catch (InterruptedException e) {
} catch (Exception e) {
logger.error("getAllKey錯誤", e);
} finally {
connectionPool.releaseConnection(client);
}
return strings;
}
到此這篇關(guān)于Java通過聚合查詢獲取group by 后的數(shù)量的文章就介紹到這了,更多相關(guān)Java 聚合查詢獲取group by數(shù)量內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java使用screw來對比數(shù)據(jù)庫表和字段差異
這篇文章主要介紹了Java如何使用screw來對比數(shù)據(jù)庫表和字段差異,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-12-12
Java訪問者模式實(shí)現(xiàn)優(yōu)雅的對象結(jié)構(gòu)處理
Java訪問者模式是一種行為型設(shè)計(jì)模式,它通過將數(shù)據(jù)結(jié)構(gòu)和數(shù)據(jù)操作分離,實(shí)現(xiàn)對復(fù)雜對象結(jié)構(gòu)的處理。它將數(shù)據(jù)結(jié)構(gòu)中的每個元素都轉(zhuǎn)換為訪問者能夠識別的形式,從而使得數(shù)據(jù)操作可以在不影響數(shù)據(jù)結(jié)構(gòu)的前提下進(jìn)行擴(kuò)展和變化2023-04-04
Spring Security6配置方法(廢棄WebSecurityConfigurerAdapter)
本文主要介紹了Spring Security6配置方法(廢棄WebSecurityConfigurerAdapter),就像文章標(biāo)題所說的,SpringSecurity已經(jīng)廢棄了繼承WebSecurityConfigurerAdapter的配置方式,下面就來詳細(xì)的介紹一下,感興趣的可以了解一下2023-12-12
SpringBoot配置Druid數(shù)據(jù)監(jiān)控代碼實(shí)例
這篇文章主要介紹了SpringBoot配置Druid數(shù)據(jù)監(jiān)控代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06
SpringBoot調(diào)用WebService接口的實(shí)現(xiàn)示例
本文主要介紹了SpringBoot調(diào)用WebService接口的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-03-03

