三人行必有我师

分组聚合后,怎么给TopHits中的列表数据排序

Elasticsearch | 作者 张鹏 | 发布于2018年06月07日 | 阅读数:11203

java api查询我是这个使用的,现在查出来的结果数据过程如下:
TermsBuilder teamAgg= AggregationBuilders.terms("agg").field("person_id") .subAggregation( AggregationBuilders.topHits("top") .setFrom((params.getPageNo()-1)*params.getPageRows()).setSize(params.getPageRows()) ).minDocCount(Long.valueOf(1)) .size(Integer.valueOf(10));
 

Terms agg = response.getAggregations().get("agg");

for (Terms.Bucket entry : agg.getBuckets()) {
      
TopHits topHits = entry.getAggregations().get("top");
}
 
topHits 中数据根据时间字段排序该怎么排,目前是散乱的
已邀请:

laoyang360 - 《一本书讲透Elasticsearch》作者,Elastic认证工程师 [死磕Elasitcsearch]知识星球地址:http://t.cn/RmwM3N9;微信公众号:铭毅天下; 博客:https://elastic.blog.csdn.net

赞同来自: zyb1994111

参考:
POST /sales/_search?size=0
{
"aggs": {
"top_tags": {
"terms": {
"field": "type",
"size": 3
},
"aggs": {
"top_sales_hits": {
"top_hits": {
"sort": [
{
"date": {
"order": "desc"
}
}
],
"_source": {
"includes": [ "date", "price" ]
},
"size" : 1
}
}
}
}
}
}
tophits结合sort

张鹏

赞同来自:

还是不可以{
  "query" : {
    "bool" : {
      "must" : [ {
        "range" : {
          "fcap_time" : {
            "from" : 1528300800,
            "to" : 1528387200,
            "include_lower" : true,
            "include_upper" : true
          }
        }
      }, {
        "bool" : {
          "should" : {
            "terms" : {
              "fcap_dcid" : [ "f94bc77ef21e4c8bad38c8400e854c8b" ]
            }
          }
        }
      } ]
    }
  },
  "explain" : true,
  "sort" : [ {
    "fcap_dcid" : {
      "order" : "desc"
    }
  } ],
  "aggregations" : {
    "agg" : {
      "terms" : {
        "field" : "person_id",
        "size" : 100,
        "order" : {
          "_term" : "desc"
        }
      },
      "aggregations" : {
        "top" : {
          "top_hits" : {
            "size" : 1,
            "sort" : [ {
              "fcap_time" : {
                "order" : "desc"
              }
            } ]
          }
        },
        "agg" : {
          "terms" : {
            "field" : "fcap_time",
            "order" : {
              "_term" : "desc"
            }
          }
        }
      }
    }
  }
}
 
 
 

image.png

 

张鹏

赞同来自:

目前是这样一个问题,数据有   组id,时间     ,每组取时间最新的那一个,总的数据按时间降序,大神帮忙看下,目前总的顺序是乱的

wssmao

赞同来自:

如果是多个字段的聚合,那么tophits下再加子聚合吗?

johnsontang

赞同来自:

这个问题解决了吗?我也碰到这个问题了

要回复问题请先登录注册