ES-JavaAPI实现 group by 查询
2021/5/10 12:25:33
本文主要是介绍ES-JavaAPI实现 group by 查询,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
public Set<String> queryBzms() { Set<String> result = new HashSet<>(); try { // 搜索出成功上报的原始数据 Object[] objects = new Object[]{}; // 查询的结果字段,类似于(select id, XSE from ..) String[] fields = {"BZM"}; FetchSourceContext sourceContext = new FetchSourceContext(true, fields, null); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.query(QueryBuilders.boolQuery().must(QueryBuilders.existsQuery("BZM"))); searchSourceBuilder.fetchSource(sourceContext); searchSourceBuilder.aggregation(AggregationBuilders.terms("BZM_group").field("BZM").size(Constant.max_es_size)); String[] indes = {this.getAliase()}; SearchRequest searchRequest = new SearchRequest(indes, searchSourceBuilder); searchSourceBuilder.size(0); SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); long total = 0; Terms terms = searchResponse.getAggregations().get("BZM_group"); for (Terms.Bucket bucket : terms.getBuckets()) { String bzm = bucket.getKeyAsString(); result.add(bzm); total += 1; } logger.info("-------------- query BZM number by SpecificationEs to success : {} ----------------", total); } catch (Exception e){ logger.error("-------------- query BZM by SpecificationEs to error : {} ----------------", e.getMessage()); } return result; }
这篇关于ES-JavaAPI实现 group by 查询的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-26Mybatis官方生成器资料详解与应用教程
- 2024-11-26Mybatis一级缓存资料详解与实战教程
- 2024-11-26Mybatis一级缓存资料详解:新手快速入门
- 2024-11-26SpringBoot3+JDK17搭建后端资料详尽教程
- 2024-11-26Springboot单体架构搭建资料:新手入门教程
- 2024-11-26Springboot单体架构搭建资料详解与实战教程
- 2024-11-26Springboot框架资料:新手入门教程
- 2024-11-26Springboot企业级开发资料入门教程
- 2024-11-26SpringBoot企业级开发资料详解与实战教程
- 2024-11-26Springboot微服务资料:新手入门全攻略