直接上代码:
// 创建一个查询条件对象
BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
// 拼接查询条件
queryBuilder.should(QueryBuilders.termQuery("字段", "值"));
// 创建聚合查询条件
TermsAggregationBuilder agg = AggregationBuilders.terms("聚合名称,自定义,取出时会用到").field("聚合字段").size(100);// size是查询聚合出来的条数
// 创建查询对象
SearchQuery build = new NativeSearchQueryBuilder()
.withQuery(queryBuilder) //添加查询条件
.addAggregation(agg) // 添加聚合条件
.withPageable(PageRequest.of(0, 1)) //符合查询条件的文档分页(不是聚合的分页)
.build();
// 执行查询
AggregatedPage<TestEntity> testEntities = elasticsearchTemplate.queryForPage(build, TestEntity.class);
// 取出聚合结果
Aggregations entitiesAggregations = testEntities.getAggregations();
Terms terms = (Terms)propertyInfoEntities.getAggregation("聚合名称,之前自定义的");
// 遍历取出聚合字段列的值,与对应的数量
for (Terms.Bucket bucket : terms.getBuckets()) {
String keyAsString = bucket.getKeyAsString(); // 聚合字段列的值
long docCount = bucket.getDocCount();// 聚合字段对应的数量
}
多字段聚合:
// 创建一个查询条件对象
BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
// 拼接查询条件
queryBuilder.should(QueryBuilders.termQuery("字段", "值"));
// 创建聚合查询条件
TermsAggregationBuilder agg1 = AggregationBuilders.terms("聚合名称2").field(聚合字段2).size(100)
TermsAggregationBuilder agg = AggregationBuilders.terms("聚合名称1").field("聚合字段1").size(100).subAggregation(agg1)//拼接上上一个聚合条件
// 创建查询对象
SearchQuery build = new NativeSearchQueryBuilder()
.withQuery(queryBuilder) //添加查询条件
.addAggregation(agg) // 添加聚合条件
.withPageable(PageRequest.of(0, 1)) //符合查询条件的文档分页(不是聚合的分页)
.build();
// 执行查询
AggregatedPage<TestEntity> testEntities = elasticsearchTemplate.queryForPage(build, TestEntity.class);
// 取出聚合结果
Aggregations entitiesAggregations = testEntities.getAggregations();
Terms terms = (Terms)propertyInfoEntities.getAggregation("聚合名称");
// 遍历最外层的聚合结果
for (Terms.Bucket bucket : terms.getBuckets()) {
String keyAsString = bucket.getKeyAsString(); // 聚合字段列的名称
long docCount = bucket.getDocCount();// 聚合字段对应的数量
// 获取内部的聚合字段信息
Aggregations aggregations = bucket.getAggregations();
Terms terms2 = aggregations.get("聚合名称2");
// 遍历获取agg1的聚合信息
for (Terms.Bucket bucket1 : terms2.getBuckets()) {
String keyAsString2 = bucket.getKeyAsString(); // 聚合字段列的名称
long docCount2 = bucket.getDocCount();// 聚合字段对应的数量
}
}
多个字段一次性聚合:
// 创建一个查询条件对象
BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
// 拼接查询条件
queryBuilder.should(QueryBuilders.termQuery("字段", "值"));
// 创建聚合查询条件
String script = "doc[‘" + 字段1 + "‘].values +‘/‘+ doc[‘" + 字段2 + "‘].values +‘/‘+ doc[‘" + 字段3 + "‘].values"; // 编写script语句
Script script1 = new Script(script); // 新建一个script对象
TermsAggregationBuilder agg = AggregationBuilders.terms("聚合名称").script(script1).size(99999); // 创建一个聚合查询对象
SearchQuery build = new NativeSearchQueryBuilder()
.withQuery(queryBuilder) //添加查询条件
.addAggregation(agg) // 添加聚合条件
.withPageable(PageRequest.of(0, 1)) //符合查询条件的文档分页(不是聚合的分页)
.build();
// 执行查询
AggregatedPage<TestEntity> testEntities = elasticsearchTemplate.queryForPage(build, TestEntity.class);
Terms terms = (Terms) shopDbEntities.getAggregation("聚合名称");
for (Terms.Bucket bucket : terms.getBuckets()) { // 循环取出聚合的值
String keyAsString = bucket.getKeyAsString();
String[] split = keyAsString.split("/"); // 切割取出每个字段
System.out.println("字段1"+split[0]);
System.out.println("字段2"+split[1]);
System.out.println("字段3"+split[2]);
}
更多字段聚合按照以上类推,如果实在对于结构不理解可以将查询语句打印出来,将打印语句拿到Kibana之类的查询工具中查询看下结构,或者直接Debug看下数据结构,就比较好理解了