白话Elasticsearch59-数据建模实战_ Nested Aggregation/ Reverse nested Aggregation对嵌套的博客评论数据进行聚合分析


在这里插入图片描述


概述

继续跟中华石杉老师学习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"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小小工匠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值