对数据分组并返回分组中特定排序的多少个
Top Hits Aggregation
- top_hits指标聚合器跟踪正在聚合的最相关文档。 此聚合器旨在用作子聚合器,以便可以按桶聚合最匹配的文档。
- top_hits聚合器可以有效地用于通过桶聚合器按特定字段对结果集进行分组。 一个或多个存储桶聚合器确定结果集被切入的属性。
选项
- from - 要获取的第一个结果的偏移量。
- size - 每个桶返回的最大匹配匹配数的最大数量。默认情况下,返回前三个匹配的匹配。
- sort - 如何对最匹配的匹配进行排序。默认情况下,命中按主查询的分数排序。
例子
请求:
GET ztjy-growth-info-alpha_*/_search
{"query": {
"match": {
"schoolId": "uOdq9KBA7xYpbmvC4Bf"
}
},
"size": 0,
"aggs": {
"userId111": {
"terms": {
"field": "userId",
"size": 10
},
"aggs": {
"publishTime111": {
"top_hits": {
"sort": [
{
"publishTime": {
"order": "desc"
}
}
],
"size": 1,
"_source": {
"includes": ["publishTime","feedId"]
}
}
}
}
}
}
}
结果
{
"took": 17,
"timed_out": false,
"_shards": {
"total": 20,
"successful": 20,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 555,
"max_score": 0,
"hits": []
},
"aggregations": {
"userId111": {
"doc_count_error_upper_bound": 6,
"sum_other_doc_count": 187,
"buckets": [
{
"key": "9d43450b8d9d4ce06a10",
"doc_count": 307,
"publishTime111": {
"hits": {
"total": 307,
"max_score": null,
"hits": [
{
"_index": "ztjy-growth-info-alpha_2",
"_type": "growth_feed_info",
"_id": "10-1494489",
"_score": null,
"_source": {
"publishTime": 1575862793007,
"feedId": 1174312823360864300
},
"sort": [
1575862793007
]
}
]
}
}
},
{
"key": "49f5874d546638f306c9",
"doc_count": 61,
"publishTime111": {
"hits": {
"total": 61,
"max_score": null,
"hits": [
{
"_index": "ztjy-growth-info-alpha_2",
"_type": "growth_feed_info",
"_id": "24-1440124",
"_score": null,
"_source": {
"publishTime": 1572959152208,
"feedId": 1162134068869742600
},
"sort": [
1572959152208
]
}
]
}
}
}
]
}
}
}