ElasticSearch6.2.4(19)——Top Hits Aggregation解析
阿新 • • 發佈:2018-12-19
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:每組顯示哪些屬性值