概述
继续跟中华石杉老师学习ES,第59篇
课程地址: https://www.roncoo.com/view/55
官网
Nested Aggregation:戳这里
示例
基于白话Elasticsearch58-数据建模实战_基于nested object实现博客与评论嵌套关系的数据
模拟数据
代码语言:javascript复制DELETE website
PUT /website
{
"mappings": {
"blogs": {
"properties": {
"comments": {
"type": "nested",
"properties": {
"name": { "type": "text" },
"comment": { "type": "text" },
"age": { "type": "short" },
"stars": { "type": "short" },
"date": { "type": "date" }
}
}
}
}
}
}
PUT /website/blogs/1
{
"title": "花无缺发表的一篇帖子",
"content": "我是花无缺,大家要不要考虑一下投资房产和买股票的事情啊。。。",
"tags": [
"投资",
"理财"
],
"comments": [
{
"name": "小鱼儿",
"comment": "什么股票啊?推荐一下呗",
"age": 28,
"stars": 4,
"date": "2016-09-01"
},
{
"name": "黄药师",
"comment": "我喜欢投资房产,风,险大收益也大",
"age": 31,
"stars": 5,
"date": "2016-10-22"
}
]
}
PUT /website/blogs/2
{
"title": "2花无缺发表的一篇帖子",
"content": "2我是花无缺,大家要不要考虑一下投资房产和买股票的事情啊。。。",
"tags": [
"房产",
"金融"
],
"comments": [
{
"name": "2小鱼儿",
"comment": "2什么股票啊?推荐一下呗",
"age": 44,
"stars": 4,
"date": "2016-09-01"
},
{
"name": "2黄药师",
"comment": "2我喜欢投资房产,风,险大收益也大",
"age": 55,
"stars": 5,
"date": "2016-10-22"
}
]
}
PUT /website/blogs/3
{
"title": "3花无缺发表的一篇帖子",
"content": "3我是花无缺,大家要不要考虑一下投资房产和买股票的事情啊。。。",
"tags": [
"房产",
"金融"
],
"comments": [
{
"name": "3小鱼儿",
"comment": "3什么股票啊?推荐一下呗",
"age": 43,
"stars": 4,
"date": "2016-09-01"
},
{
"name": "3黄药师",
"comment": "2我喜欢投资房产,风,险大收益也大",
"age": 76,
"stars": 5,
"date": "2016-10-22"
}
]
}
代码语言:javascript复制#查看mapping
GET /website/_mapping/blogs/
{
"website": {
"mappings": {
"blogs": {
"properties": {
"comments": {
"type": "nested",
"properties": {
"age": {
"type": "short"
},
"comment": {
"type": "text"
},
"date": {
"type": "date"
},
"name": {
"type": "text"
},
"stars": {
"type": "short"
}
}
},
"content": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"tags": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"title": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
}
}
需求一: 按照评论日期进行bucket划分,然后拿到每个月的评论的评分的平均值
代码语言:javascript复制GET /website/blogs/_search
{
"size": 0,
"aggs": {
"comments_path": {
"nested": {
"path": "comments"
},
"aggs": {
"group_by_comments_date": {
"date_histogram": {
"field": "comments.date",
"interval": "month",
"format": "yyyy-MM"
},
"aggs": {
"avg_stars": {
"avg": {
"field": "comments.stars"
}
}
}
}
}
}
}
}
返回:
需求二: 以年龄 10岁一个划分,看下都有哪些tag
reverse_nested
DSL:
代码语言:javascript复制GET /website/blogs/_search
{
"size": 0,
"aggs": {
"comments_path": {
"nested": {
"path": "comments"
},
"aggs": {
"group_by_comments_age": {
"histogram": {
"field": "comments.age",
"interval": 10,
"min_doc_count": 1
},
"aggs": {
"reverse_path": {
"reverse_nested": {},
"aggs": {
"group_by_tags": {
"terms": {
"field": "tags.keyword"
}
}
}
}
}
}
}
}
}
}
返回:
代码语言:javascript复制 {
"took": 5,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 0,
"hits": []
},
"aggregations": {
"comments_path": {
"doc_count": 6,
"group_by_comments_age": {
"buckets": [
{
"key": 20,
"doc_count": 1,
"reverse_path": {
"doc_count": 1,
"group_by_tags": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "投资",
"doc_count": 1
},
{
"key": "理财",
"doc_count": 1
}
]
}
}
},
{
"key": 30,
"doc_count": 1,
"reverse_path": {
"doc_count": 1,
"group_by_tags": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "投资",
"doc_count": 1
},
{
"key": "理财",
"doc_count": 1
}
]
}
}
},
{
"key": 40,
"doc_count": 2,
"reverse_path": {
"doc_count": 2,
"group_by_tags": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "房产",
"doc_count": 2
},
{
"key": "金融",
"doc_count": 2
}
]
}
}
},
{
"key": 50,
"doc_count": 1,
"reverse_path": {
"doc_count": 1,
"group_by_tags": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "房产",
"doc_count": 1
},
{
"key": "金融",
"doc_count": 1
}
]
}
}
},
{
"key": 70,
"doc_count": 1,
"reverse_path": {
"doc_count": 1,
"group_by_tags": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "房产",
"doc_count": 1
},
{
"key": "金融",
"doc_count": 1
}
]
}
}
}
]
}
}
}
}
reverse_nested
reverse_nested : 戳这里
简单来说:基于nested object 下钻的聚合里面,可以用上它外面的field
比如下面的 nested 字段是 comments
代码语言:javascript复制 "nested": {
"path": "comments"
}
我们想取tags 字段,这个时候就需要使用 reverse_nested
以下用6.4的版本演示
代码语言:javascript复制PUT /issues
{
"mappings": {
"issue": {
"properties": {
"tags": {
"type": "keyword"
},
"comments": {
"type": "nested",
"properties": {
"username": {
"type": "keyword"
},
"comment": {
"type": "text"
}
}
}
}
}
}
}
GET /issues/_search
{
"query": {
"match_all": {}
},
"aggs": {
"comments": {
"nested": {
"path": "comments"
},
"aggs": {
"top_usernames": {
"terms": {
"field": "comments.username"
},
"aggs": {
"comment_to_issue": {
"reverse_nested": {},
"aggs": {
"top_tags_per_comment": {
"terms": {
"field": "tags"
}
}
}
}
}
}
}
}
}
}