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

2021-08-17 15:04:28 浏览数 (1)


概述

继续跟中华石杉老师学习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"
                  }
                }
              ]
            }
          }
        }
      ]
    }
  }
}

0 人点赞