Nested Aggregation/ Reverse nested Aggregation对嵌套的博客评论数据进行聚合分析

模拟数据

PUT /blogs
{
  "mappings": {
    "properties": {
        "comments": {
          "type": "nested", 
          "properties": {
            "name":    { "type": "text"  },
            "comment": { "type": "text"  },
            "age":     { "type": "short"   },
            "stars":   { "type": "short"   },
            "date":    { "type": "date"    }
          }
        }
      }
  }
}

数据

PUT /blogs/_doc/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 /blogs/_doc/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 /blogs/_doc/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"
    }
  ]
}

需求一: 按照评论日期进行bucket划分,然后拿到每个月的评论的评分的平均值

GET /blogs/_search 
{
  "size": 0, 
  "aggs": {
    "comments_path": {
      "nested": {
        "path": "comments"
      }, 
      "aggs": {
        "group_by_comments_date": {
          "date_histogram": {
            "field": "comments.date",
            "calendar_interval": "month",
            "format": "yyyy-MM"
          },
          "aggs": {
            "avg_stars": {
              "avg": {
                "field": "comments.stars"
              }
            }
          }
        }
      }
    }
  }
}

需求二: 以年龄 10岁一个划分,看下都有哪些tag

GET /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"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

  基于nested object 下钻的聚合里面,可以用上它外面的field,比如下面的 nested 字段是 comments

 "nested": {
        "path": "comments"
      }

 

  想取tags 字段,这个时候就需要使用 reverse_nested

       

 

posted on 2021-10-15 08:25  溪水静幽  阅读(164)  评论(0)    收藏  举报