PUT /my_index/doc/_bulk?refresh
{"index":{"_id":1}}
{"name":"小明","content":"深圳大厦很高的","age":12}
{"index":{"_id":2}}
{"name":"小明","content":"深圳天气还是不错的","age":14}
{"index":{"_id":3}}
{"name":"小红","content":"深圳交通很棒","age":13}
{"index":{"_id":4}}
{"name":"小明","content":"深圳是很好是吧","age":16}
{"index":{"_id":5}}
{"name":"小春","content":"美女在深圳很多","age":16}
{"index":{"_id":6}}
{"name":"小红","content":"深圳的帅哥很多","age":16}
GET /my_index/_search
{
"size": 0,
"aggs": {
"group_agg":{
"terms": {
"field": "name.keyword"
},
"aggs": {
"top_data": {
"top_hits": {
"size": 2,
"sort": [
{
"age": {
"order": "desc"
}
}
],
"_source": {
"includes": "content"
}
}
}
}
}
}
}
"aggregations": {
"group_agg": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "小明",
"doc_count": 3,
"top_data": {
"hits": {
"total": 3,
"max_score": null,
"hits": [
{
"_index": "my_index",
"_type": "doc",
"_id": "4",
"_score": null,
"_source": {
"content": "深圳是很好是吧"
},
"sort": [
16
]
},
{
"_index": "my_index",
"_type": "doc",
"_id": "2",
"_score": null,
"_source": {
"content": "深圳天气还是不错的"
},
"sort": [
14
]
}
]
}
}
},
{
"key": "小红",
"doc_count": 2,
"top_data": {
"hits": {
"total": 2,
"max_score": null,
"hits": [
{
"_index": "my_index",
"_type": "doc",
"_id": "6",
"_score": null,
"_source": {
"content": "深圳的帅哥很多"
},
"sort": [
16
]
},
{
"_index": "my_index",
"_type": "doc",
"_id": "3",
"_score": null,
"_source": {
"content": "深圳交通很棒"
},
"sort": [
13
]
}
]
}
}
},
{
"key": "小春",
"doc_count": 1,
"top_data": {
"hits": {
"total": 1,
"max_score": null,
"hits": [
{
"_index": "my_index",
"_type": "doc",
"_id": "5",
"_score": null,
"_source": {
"content": "美女在深圳很多"
},
"sort": [
16
]
}
]
}
}
}
]
}
}
结果会发现,top_hits的作用就是在每个组下面的数据进行筛选
size:每组显示的数据
sort:每组的排序
_source.includes:每组显示哪些属性值