实现方式:(以会员编码分组分页展示会员最近一条时间记录排序为例):
1、查询实现
// 桶排序聚合
BucketSortPipelineAggregationBuilder bucketSortAggregation = PipelineAggregatorBuilders.bucketSort(
"sortCustomer", Lists.emptyList()).from((pageNo.intValue() - 1) * pageSize.intValue()).size(pageSize.intValue());
//分页指标--用于统计分页total总数
CardinalityAggregationBuilder cardinalityAggregation = AggregationBuilders.cardinality("custCard").field("customer_no.keyword");
//返回字段取最新一条记录
TopHitsAggregationBuilder topHitsAggregation = AggregationBuilders.topHits("latestCust")
.fetchSource(new String[]{"customer_no", "customer_name", "identify_no", "visit_time", "service_item_names", "organization_name", "id", "type"} , null).size(1) .sort("visit_time_long", SortOrder.DESC);
//以会员编码分组
TermsAggregationBuilder termsAggregationBuilder = AggregationBuilders.terms("topCustomer").field("customer_no.keyword")
.size(pageNo.intValue() * pageSize.intValue())
.subAggregation(bucketSortAggregation)
.subAggregation(topHitsAggregation);
//es查询 分页指标和分组terms要同级
//hit查询返回0条数据
searchSourceBuilder.size(0);
searchSourceBuilder.from(0);
//排序
searchRequest.source(searchSourceBuilder.sort("visit_time_long", SortOrder.DESC));
//query条件--正常查询条件
searchRequest.source(searchSourceBuilder.query(boolBuilder));
//聚合条件 分组+分页指标 searchRequest.source(searchSourceBuilder.aggregation(termsAggregationBuilder));
searchRequest.source(searchSourceBuilder.aggregation(cardinalityAggregation));
2、es语句
GET /xxxxx/_search
{
"from": 0,
"size": 0,
"query": {
"bool": {
"must": [
{
"term": {
"del_flag": {
"value": false,
"boost": 1
}
}
}
],
"adjust_pure_negative": true,
"boost": 1
}
},
"sort": [
{
"visit_time_long": {
"order": "desc"
}
}
],
"aggregations": {
"topCustomer": {
"terms": {
"field": "customer_no.keyword",
"size": 5,
"min_doc_count": 1,
"shard_min_doc_count": 0,
"show_term_doc_count_error": false,
"order": [
{
"_count": "desc"
},
{
"_key": "asc"
}
]
},
"aggregations": {
"latestCust": {
"top_hits": {
"from": 0,
"size": 1,
"version": false,
"seq_no_primary_term": false,
"explain": false,
"_source": {
"includes": [
"customer_no",
"customer_name",
![img](https://img-blog.csdnimg.cn/img_convert/514de98a34516f82ea70c989a2aea5d8.png)
![img](https://img-blog.csdnimg.cn/img_convert/ae487e780d38f1a623ab9b08ce486fbc.png)
**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**
**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618545628)**
**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**
/618545628)**
**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**