需求说明:
文档中存在字段year(年),volume(卷),issue(期)三个字段,现有需求如下:需要先根据year进行聚合并且降序,然后根据volume字段在年聚合后再次聚合,issue字段则在以上两字段的聚合后再次聚合,效果图如下:
DSL查询语句
{
"from": 0,
"size": 10,
"aggregations": {
"year": {
"terms": {
"field": "year", //指定根据year聚合
"size": 10,
"min_doc_count": 1,
"shard_min_doc_count": 0,
"show_term_doc_count_error": false,
"order": {
"_key": "desc" //根据year降序排序
}
},
"aggregations": { //子聚合
"volume": {
"terms": {
"field": "volume", //指定根据volume聚合
"size": 10,
"min_doc_count": 1,
"shard_min_doc_count": 0,
"show_term_doc_count_error": false,
"order": {
"_key": "desc" //根据volume降序排序
}
},
"aggregations": { //子聚合
"issue": {
"terms": {
"field": "issue", //指定根据issue聚合
"size": 10,
"min_doc_count": 1,
"shard_min_doc_count": 0,
"show_term_doc_count_error": false,
"order": {
"_key": "desc" //根据issue降序排序
}
}
}
}
}
}
}
}
}
检索结果:
SpringDataElasticSearch语句实现
nativeSearchQueryBuilder.addAggregation(AggregationBuilders.terms("year").field("year").order(BucketOrder.key(false))
.subAggregation(AggregationBuilders.terms("volume").field("volume").order(BucketOrder.key(false))
.subAggregation(AggregationBuilders.terms("issue").field("issue").order(BucketOrder.key(false))))
);
实现效果:
关于子聚合的官方文档
https://www.elastic.co/guide/en/elasticsearch/reference/7.17/search-aggregations-bucket-children-aggregation.html文章来源:https://www.toymoban.com/news/detail-562438.html
相关文档:https://blog.csdn.net/donghaixiaolongwang/article/details/58597058文章来源地址https://www.toymoban.com/news/detail-562438.html
到了这里,关于Elasticsearch的子聚合查询和使用SpringDataElasticSearch如何实现三级的子查询的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!