網站首頁 編程語言 正文
?
public void bucketGroup() {
SearchRequest request = new SearchRequest();
/**
* 查詢條件
*/
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
request.indices("smsp_collect_info_new");
// 查詢某城市
searchSourceBuilder.query(QueryBuilders.matchQuery("city", integer));
// 構建聚合查詢,并統計數量
// AggregationBuilders.terms("your_agg_name").field("master_port"),表示對master_port字
// 段進行聚合,相當于sql group by master_port,其中your_agg_name是我們對聚合字段起的別名
// ,到時候我們就可以通過這個別名獲取到聚合的數量
// AggregationBuilders.count("count_number").field("id"))表示對id字段進行count,就是計數,
// 其中count_number是我們為計數字段起的別名,同上方一個道理, 此轉換為sql應該是select count(id)
// from xx where city=xx group by master_port;
searchSourceBuilder.aggregation(AggregationBuilders
.terms("your_agg_name")
.field("master_port")
.subAggregation(AggregationBuilders.count("count_number")
.field("id")).size(10000));
//放入文檔中
request.source(searchSourceBuilder);
//遠程查詢
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
//元素數量
SearchHits hits1 = response.getHits();
// 獲取聚合結果給
Terms termsAggregation = response.getAggregations().get("your_agg_name");
// 桶聚合
for (Terms.Bucket bucket : termsAggregation.getBuckets()) {
// 獲取聚合的key的值,也就是master_port的每一項的值
String key = bucket.getKeyAsString();
// 獲取當前的master_count的聚合的數量
ValueCount countAggregation = bucket.getAggregations().get("count_number");
long countValue = countAggregation.getValue();
}
}
原文鏈接:https://blog.csdn.net/qq_53679247/article/details/131471297
- 上一篇:沒有了
- 下一篇:沒有了
相關推薦
- 2022-12-13 C++?Boost?Pool超詳細講解_C 語言
- 2022-01-13 使用element插件中Descriptions遇到的坑
- 2022-10-26 go并發編程sync.Cond使用場景及實現原理_Golang
- 2022-07-12 git同步fork倉庫同步upstream倉庫
- 2022-08-04 Android解決所有雙擊優化的問題_Android
- 2022-08-11 利用python繪制線型圖_python
- 2022-11-26 Python?Django教程之模板的使用_python
- 2022-08-17 python運行腳本文件的三種方法實例_python
- 欄目分類
-
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支