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

简介: 白话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"
                  }
                }
              ]
            }
          }
        }
      ]
    }
  }
}


相关实践学习
以电商场景为例搭建AI语义搜索应用
本实验旨在通过阿里云Elasticsearch结合阿里云搜索开发工作台AI模型服务,构建一个高效、精准的语义搜索系统,模拟电商场景,深入理解AI搜索技术原理并掌握其实现过程。
ElasticSearch 最新快速入门教程
本课程由千锋教育提供。全文搜索的需求非常大。而开源的解决办法Elasricsearch(Elastic)就是一个非常好的工具。目前是全文搜索引擎的首选。本系列教程由浅入深讲解了在CentOS7系统下如何搭建ElasticSearch,如何使用Kibana实现各种方式的搜索并详细分析了搜索的原理,最后讲解了在Java应用中如何集成ElasticSearch并实现搜索。  
相关文章
|
存储 搜索推荐 数据建模
Elasticsearch 的数据建模与索引设计
【9月更文第3天】Elasticsearch 是一个基于 Lucene 的搜索引擎,广泛应用于全文检索、数据分析等领域。为了确保 Elasticsearch 的高效运行,合理的数据建模和索引设计至关重要。本文将探讨如何为不同的应用场景设计高效的索引结构,并分享一些数据建模的最佳实践。
559 2
elasticsearch分组求平均值
elasticsearch分组求平均值
194 0
elasticsearch分组求平均值
|
SQL
elasticsearch分组
elasticsearch分组
126 0
elasticsearch分组
|
数据建模 关系型数据库
Elasticsearch之数据建模实战通过应用层join实现用户与博客的关联
Elasticsearch之数据建模实战通过应用层join实现用户与博客的关联
212 0
|
存储 JSON 数据建模
Elasticsearch数据建模实战之基于nested object实现博客与评论嵌套关系
Elasticsearch数据建模实战之基于nested object实现博客与评论嵌套关系
184 0
|
数据建模
Elasticsearch数据建模实战之祖孙三层数据关系建模以及搜索实战
Elasticsearch数据建模实战之祖孙三层数据关系建模以及搜索实战
135 0
|
数据建模
Elasticsearch数据建模实战之对文件系统进行数据建模以及文件搜索实战
Elasticsearch数据建模实战之对文件系统进行数据建模以及文件搜索实战
122 0
|
数据建模 关系型数据库
白话Elasticsearch60-数据建模实战_Join datatype 父子关系数据建模
白话Elasticsearch60-数据建模实战_Join datatype 父子关系数据建模
216 0
|
8月前
|
安全 Java Linux
Linux安装Elasticsearch详细教程
Linux安装Elasticsearch详细教程
1562 64
|
7月前
|
JSON 安全 数据可视化
Elasticsearch(es)在Windows系统上的安装与部署(含Kibana)
Kibana 是 Elastic Stack(原 ELK Stack)中的核心数据可视化工具,主要与 Elasticsearch 配合使用,提供强大的数据探索、分析和展示功能。elasticsearch安装在windows上一般是zip文件,解压到对应目录。文件,elasticsearch8.x以上版本是自动开启安全认证的。kibana安装在windows上一般是zip文件,解压到对应目录。elasticsearch的默认端口是9200,访问。默认用户是elastic,密码需要重置。
3887 0

热门文章

最新文章