首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >白话Elasticsearch55-数据建模之对每个用户发表的博客进行分组 (Top Hits Aggregation)

白话Elasticsearch55-数据建模之对每个用户发表的博客进行分组 (Top Hits Aggregation)

作者头像
小小工匠
发布2021-08-17 15:04:28
发布2021-08-17 15:04:28
8560
举报
文章被收录于专栏:小工匠聊架构小工匠聊架构


概述

继续跟中华石杉老师学习ES,第55篇

课程地址: https://www.roncoo.com/view/55


官网

Top Hits Aggregation : 戳这里

其他详见官网


示例

需求: 对每个用户发表的博客进行分组

模拟一批数据

代码语言:javascript
复制
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小工匠"
  }
}

DSL

代码语言:javascript
复制
#对每个用户发表的博客进行分组,取前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
          }
        }
      }
    }
  }
}

返回:

代码语言:javascript
复制
{
  "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"
                  }
                }
              ]
            }
          }
        }
      ]
    }
  }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/08/31 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 概述
  • 官网
  • 示例
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档