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

本文涉及的产品
检索分析服务 Elasticsearch 版,2核4GB开发者规格 1个月
简介: 白话Elasticsearch55-数据建模之对每个用户发表的博客进行分组 (Top Hits Aggregation)

20190806092132811.jpg


概述

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

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


官网

Top Hits Aggregation : 戳这里



20190831205128644.png

2019083120520884.png

20190831205226970.png


其他详见官网


示例

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

模拟一批数据


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

#对每个用户发表的博客进行分组,取前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
          }
        }
      }
    }
  }
}


返回:

{
  "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"
                  }
                }
              ]
            }
          }
        }
      ]
    }
  }
}


相关实践学习
利用Elasticsearch实现地理位置查询
本实验将分别介绍如何使用Elasticsearch7.10版本进行全文检索、多语言检索和地理位置查询三个Elasticsearch基础检索子场景的实现。
ElasticSearch 入门精讲
ElasticSearch是一个开源的、基于Lucene的、分布式、高扩展、高实时的搜索与数据分析引擎。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr(也是基于Lucene)。 ElasticSearch的实现原理主要分为以下几个步骤: 用户将数据提交到Elastic Search 数据库中 通过分词控制器去将对应的语句分词,将其权重和分词结果一并存入数据 当用户搜索数据时候,再根据权重将结果排名、打分 将返回结果呈现给用户 Elasticsearch可以用于搜索各种文档。它提供可扩展的搜索,具有接近实时的搜索,并支持多租户。
相关文章
|
18天前
elasticsearch分组求平均值
elasticsearch分组求平均值
14 0
elasticsearch分组求平均值
|
18天前
|
SQL
elasticsearch分组
elasticsearch分组
14 0
elasticsearch分组
|
11月前
|
数据建模 关系型数据库
Elasticsearch之数据建模实战通过应用层join实现用户与博客的关联
Elasticsearch之数据建模实战通过应用层join实现用户与博客的关联
|
11月前
|
存储 JSON 数据建模
Elasticsearch数据建模实战之基于nested object实现博客与评论嵌套关系
Elasticsearch数据建模实战之基于nested object实现博客与评论嵌套关系
|
11月前
|
数据建模
Elasticsearch数据建模实战之祖孙三层数据关系建模以及搜索实战
Elasticsearch数据建模实战之祖孙三层数据关系建模以及搜索实战
|
11月前
|
数据建模
Elasticsearch数据建模实战之对文件系统进行数据建模以及文件搜索实战
Elasticsearch数据建模实战之对文件系统进行数据建模以及文件搜索实战
|
数据建模 关系型数据库
白话Elasticsearch60-数据建模实战_Join datatype 父子关系数据建模
白话Elasticsearch60-数据建模实战_Join datatype 父子关系数据建模
44 0
|
数据建模
白话Elasticsearch59-数据建模实战_ Nested Aggregation/ Reverse nested Aggregation对嵌套的博客评论数据进行聚合分析
白话Elasticsearch59-数据建模实战_ Nested Aggregation/ Reverse nested Aggregation对嵌套的博客评论数据进行聚合分析
60 0
|
19天前
Elasticsearch安装配置文件
Elasticsearch安装配置文件
15 0
|
4天前
|
自然语言处理 搜索推荐
在Elasticsearch 7.9.2中安装IK分词器并进行自定义词典配置
在Elasticsearch 7.9.2中安装IK分词器并进行自定义词典配置
7 1