白话Elasticsearch55-数据建模之对每个用户发表的博客进行分组 (Top Hits Aggregation)
【摘要】
文章目录
概述官网示例
概述
继续跟中华石杉老师学习ES,第55篇
课程地址: https://www.roncoo.com/view/55
官网
Top Hits A...
概述
继续跟中华石杉老师学习ES,第55篇
课程地址: https://www.roncoo.com/view/55
官网
Top Hits Aggregation : 戳这里
其他详见官网
示例
需求: 对每个用户发表的博客进行分组
模拟一批数据
PUT /blogs2/blogs2/2
{
"title": "2跟石杉老师学ES",
"content": "2-second blog",
"userInfo": {
"userId": 2,
"username": "2小工匠"
}
}
PUT /blogs2/blogs2/3
{
"title": "3跟石杉老师学ES",
"content": "3-second blog",
"userInfo": {
"userId": 3,
"username": "3小工匠"
}
}
PUT /blogs2/blogs2/4
{
"title": "4跟石杉老师学ES",
"content": "4-second blog",
"userInfo": {
"userId": 4,
"username": "4小工匠"
}
}
PUT /blogs2/blogs2/5
{
"title": "5跟石杉老师学ES",
"content": "5-second blog",
"userInfo": {
"userId": 2,
"username": "2小工匠"
}
}
PUT /blogs2/blogs2/6
{
"title": "6跟石杉老师学ES",
"content": "6-second blog",
"userInfo": {
"userId": 3,
"username": "3小工匠"
}
}
PUT /blogs2/blogs2/7
{
"title": "7跟石杉老师学ES",
"content": "7-second blog",
"userInfo": {
"userId": 4,
"username": "4小工匠"
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
DSL
#对每个用户发表的博客进行分组,取前5篇的标题
GET /blogs2/blogs2/_search
{
"size": 0,
"aggs": {
"group_by_userName": {
"terms": {
"field": "userInfo.username.keyword"
},
"aggs": {
"top_blog": {
"top_hits": {
"_source": {
"includes": "title"
},
"size": 5
}
}
}
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
返回:
{
"took": 9,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 7,
"max_score": 0,
"hits": []
},
"aggregations": {
"group_by_userName": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "2小工匠",
"doc_count": 2,
"first_blog": {
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "blogs2",
"_type": "blogs2",
"_id": "5",
"_score": 1,
"_source": {
"title": "5跟石杉老师学ES"
}
},
{
"_index": "blogs2",
"_type": "blogs2",
"_id": "2",
"_score": 1,
"_source": {
"title": "2跟石杉老师学ES"
}
}
]
}
}
},
{
"key": "3小工匠",
"doc_count": 2,
"first_blog": {
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "blogs2",
"_type": "blogs2",
"_id": "6",
"_score": 1,
"_source": {
"title": "6跟石杉老师学ES"
}
},
{
"_index": "blogs2",
"_type": "blogs2",
"_id": "3",
"_score": 1,
"_source": {
"title": "3跟石杉老师学ES"
}
}
]
}
}
},
{
"key": "4小工匠",
"doc_count": 2,
"first_blog": {
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "blogs2",
"_type": "blogs2",
"_id": "4",
"_score": 1,
"_source": {
"title": "4跟石杉老师学ES"
}
},
{
"_index": "blogs2",
"_type": "blogs2",
"_id": "7",
"_score": 1,
"_source": {
"title": "7跟石杉老师学ES"
}
}
]
}
}
},
{
"key": "小工匠",
"doc_count": 1,
"first_blog": {
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "blogs2",
"_type": "blogs2",
"_id": "1",
"_score": 1,
"_source": {
"title": "跟石杉老师学ES"
}
}
]
}
}
}
]
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
文章来源: artisan.blog.csdn.net,作者:小小工匠,版权归原作者所有,如需转载,请联系作者。
原文链接:artisan.blog.csdn.net/article/details/100176955
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)