白话Elasticsearch59-数据建模实战_ Nested Aggregation/ Reverse nested Aggregation对嵌套的博客评论数据进行聚合分析
2019-09-02 20:31
1121 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/yangshangwei/article/details/100321773
文章目录
概述
继续跟中华石杉老师学习ES,第59篇
课程地址: https://www.roncoo.com/view/55
官网
Nested Aggregation:戳这里
示例
基于白话Elasticsearch58-数据建模实战_基于nested object实现博客与评论嵌套关系的数据
模拟数据
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" } ] }
#查看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划分,然后拿到每个月的评论的评分的平均值
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:
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" } } } } } } } } } }
返回:
{ "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
"nested": { "path": "comments" }
我们想取tags 字段,这个时候就需要使用 reverse_nested
以下用6.4的版本演示
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" } } } } } } } } } }
相关文章推荐
- elasticsearch核心知识--7.aggregation的 嵌套聚合,下钻分析,聚合分析[aggregation和ranges]
- 白话Elasticsearch60-数据建模实战_Join datatype 父子关系数据建模
- Elasticsearch学习之嵌套聚合,下钻分析,聚合分析
- elasticsearch的基础语句介绍 聚合功能很强大 可以分析数据
- 异常检测之基于随机变量的熵来进行数据建模和分析
- Python3数据分析与挖掘建模实战
- 利用python调用elasticsearch-api来分析数据并作图进行日报邮件发送
- 利用python进行数据分析-数据聚合与分组运算2
- 利用Python进行数据分析--数据聚合与分组运算
- 数据分析实战-利用K-Means进行航空公司客户价值识别
- 《数据分析与挖掘实战》总结及代码练习---chap5 挖掘建模---聚类分析 关联模型 离群点检测
- 利用python调用elasticsearch-api来分析数据并作图进行日报邮件发送
- python数据分析与挖掘实战(5)挖掘建模之聚类分析、关联规则
- 利用Python进行数据分析--数据聚合与分组运算1
- Elasticsearch学习之深入聚合分析三---案例实战
- 使用JestClient连接elasticsearch-5.x对数据进行分组聚合
- 利用python调用elasticsearch-api来分析数据并作图进行日报邮件发送
- elasticsearch聚合操作——本质就是针对搜索后的结果使用桶bucket(允许嵌套)进行group by,统计下分组结果,包括min/max/avg
- 2018最新Python数据分析实战教程视频 python数据分析班视频 Python数据分析基础教程 利用Python进行数据分析
- Python3数据分析与挖掘建模实战