http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 5762|回复: 4

[Elasticsearch] 53-Elasticsearch字段折叠

[复制链接]
发表于 2019-1-10 14:35:35 | 显示全部楼层 |阅读模式
字段折叠

一个普遍的需求是需要通过特定字段进行分组。 例如我们需要按照用户名称 分组 返回最相关的博客文章。 按照用户名分组意味着进行 terms 聚合。 为能够按照用户 整体 名称进行分组,名称字段应保持 not_analyzed 的形式, 具体说明参考 聚合与分析:
[C#] 纯文本查看 复制代码
PUT /my_index/_mapping/blogpost

{

  "properties": {

    "user": {

      "properties": {

        "name": { 

          "type": "string",

          "fields": {

            "raw": { 

              "type":  "string",

              "index": "not_analyzed"

            }

          }

        }

      }

    }

  }

}



  • user.name 字段将用来进行全文检索。
  • user.name.raw 字段将用来通过 terms 聚合进行分组。


然后添加一些数据:

[C#] 纯文本查看 复制代码
PUT /my_index/user/1

{

  "name": "John Smith",

  "email": "john@smith.com",

  "dob": "1970/10/24"

}



PUT /my_index/blogpost/2

{

  "title": "Relationships",

  "body": "It's complicated...",

  "user": {

    "id": 1,

    "name": "John Smith"

  }

}



PUT /my_index/user/3

{

  "name": "Alice John",

  "email": "alice@john.com",

  "dob": "1979/01/04"

}



PUT /my_index/blogpost/4

{

  "title": "Relationships are cool",

  "body": "It's not complicated at all...",

  "user": {

    "id": 3,

    "name": "Alice John"

  }

}

现在我们来查询标题包含 relationships 并且作者名包含 John 的博客,查询结果再按作者名分组,感谢 top_hits aggregation 提供了按照用户进行分组的功能:

[C#] 纯文本查看 复制代码
GET /my_index/blogpost/_search

{

  "size" : 0, 

  "query": { 

    "bool": {

      "must": [

        { "match": { "title":     "relationships" }},

        { "match": { "user.name": "John"          }}

      ]

    }

  },

  "aggs": {

    "users": {

      "terms": {

        "field":   "user.name.raw",      

        "order": { "top_score": "desc" } 

      },

      "aggs": {

        "top_score": { "max":      { "script":  "_score"           }}, 

        "blogposts": { "top_hits": { "_source": "title", "size": 5 }}  

      }

    }

  }

}


  • 我们感兴趣的博客文章是通过 blogposts 聚合返回的,所以我们可以通过将 size 设置成 0 来禁止 hits 常规搜索。
  • query 返回通过 relationships 查找名称为 John 的用户的博客文章。
  • terms 聚合为每一个 user.name.raw 创建一个桶。
  • top_score 聚合对通过 users 聚合得到的每一个桶按照文档评分对词项进行排序。
  • top_hits 聚合仅为每个用户返回五个最相关的博客文章的 title 字段。


这里显示简短响应结果:
[C#] 纯文本查看 复制代码
...

"hits": {

  "total":     2,

  "max_score": 0,

  "hits":      [] 

},

"aggregations": {

  "users": {

     "buckets": [

        {

           "key":       "John Smith", 

           "doc_count": 1,

           "blogposts": {

              "hits": { 

                 "total":     1,

                 "max_score": 0.35258877,

                 "hits": [

                    {

                       "_index": "my_index",

                       "_type":  "blogpost",

                       "_id":    "2",

                       "_score": 0.35258877,

                       "_source": {

                          "title": "Relationships"

                       }

                    }

                 ]

              }

           },

           "top_score": { 

              "value": 0.3525887727737427

           }

        },

...



  • 因为我们设置 size 为 0 ,所以 hits 数组是空的。
  • 在顶层查询结果中出现的每一个用户都会有一个对应的桶。
  • 在每个用户桶下面都会有一个 blogposts.hits 数组包含针对这个用户的顶层查询结果。
  • 用户桶按照每个用户最相关的博客文章进行排序。


使用 top_hits 聚合等效执行一个查询返回这些用户的名字和他们最相关的博客文章,然后为每一个用户执行相同的查询,以获得最好的博客。但前者的效率要好很多。

每一个桶返回的顶层查询命中结果是基于最初主查询进行的一个轻量 迷你查询 结果集。这个迷你查询提供了一些你期望的常用特性,例如高亮显示以及分页功能。




1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2019-1-10 14:55:14 | 显示全部楼层
强烈支持楼主ing……
发表于 2019-1-10 15:34:18 | 显示全部楼层
强烈支持楼主ing……
发表于 2019-1-10 16:43:14 | 显示全部楼层
强烈支持楼主ing……
发表于 2019-1-10 16:44:57 | 显示全部楼层
我只是路过打酱油的。
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-4-19 16:27

© 2014-2021

快速回复 返回顶部 返回列表