首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Elasticsearch:top_hits aggregation

Elasticsearch:top_hits aggregation

作者头像
腾讯云大数据
修改2021-01-08 15:59:24
1.1K0
修改2021-01-08 15:59:24
举报

腾讯云 Elasticsearch Service】高可用,可伸缩,云端全托管。集成X-Pack高级特性,适用日志分析/企业搜索/BI分析等场景


top_hits 指标聚合器跟踪要聚合的最相关文档。 该聚合器旨在用作子聚合器,以便可以按存储分区汇总最匹配的文档。

top_hits 聚合器可以有效地用于通过存储桶聚合器按某些字段对结果集进行分组。 一个或多个存储桶聚合器确定将结果集切成哪些属性。

选项:

  • from -要获取的第一个结果的偏移量。
  • size -每个存储桶要返回的最匹配匹配项的最大数目。 默认情况下,返回前三个匹配项。
  • 排序 - 匹配的热门匹配的排序方式。 默认情况下,命中按主要查询的分数排序。

我们还是来用一个例子来展示如何使用这个:

准备数据:

我们选用 Kibana 里带的官方的 Sample web logs 来作为我们的索引:

然后加载我们的索引:

这样我们的数据就加载完成了。

Top hits aggregation

首先,我们先做一个简单的基于 hosts 的 aggregation:

GET kibana_sample_data_logs/_search{  "size": 0,  "aggs": {    "hosts": {      "terms": {        "field": "host.keyword",        "size": 2      }    }  }}

上面的搜索的结果是我们想得到2个桶的数据(这里为了说明问题的方便,设定为2)。而这两个桶是基于 hosts 的值。搜索的结果是:

"aggregations" : {    "hosts" : {      "doc_count_error_upper_bound" : 0,      "sum_other_doc_count" : 2807,      "buckets" : [        {          "key" : "artifacts.elastic.co",          "doc_count" : 6488        },        {          "key" : "www.elastic.co",          "doc_count" : 4779        }      ]    }  }

现在的要求是:我们想针对这里的每个桶得到按照我们需要排序的前面的几个结果,比如下面的搜索:

GET kibana_sample_data_logs/_search{  "size": 0,  "aggs": {    "hosts": {      "terms": {        "field": "host.keyword",        "size": 2      },      "aggs": {        "most_bytes": {          "top_hits": {            "sort": [              {                "bytes": {                  "order": "desc"                }              }            ],            "_source": {              "includes": [                "bytes",                "hosts",                "ip",                "clientip"              ]            },            "size": 2          }        }      }    }  }}

上面实际上是一个 pipeline 的聚合。它在针对上面的桶来做了一个 top_hits 的聚合。针对每个桶,我们需要按照 bytes 的大小,降序排列,并且每个桶只需要两个数据:

  "aggregations" : {    "hosts" : {      "doc_count_error_upper_bound" : 0,      "sum_other_doc_count" : 2807,      "buckets" : [        {          "key" : "artifacts.elastic.co",          "doc_count" : 6488,          "most_bytes" : {            "hits" : {              "total" : {                "value" : 6488,                "relation" : "eq"              },              "max_score" : null,              "hits" : [                {                  "_index" : "kibana_sample_data_logs",                  "_type" : "_doc",                  "_id" : "dnNIHm8BjrINWI3xXlRc",                  "_score" : null,                  "_source" : {                    "bytes" : 19929,                    "ip" : "127.155.255.9",                    "clientip" : "127.155.255.9"                  },                  "sort" : [                    19929                  ]                },                {                  "_index" : "kibana_sample_data_logs",                  "_type" : "_doc",                  "_id" : "OXNIHm8BjrINWI3xX1td",                  "_score" : null,                  "_source" : {                    "bytes" : 19904,                    "ip" : "100.177.58.231",                    "clientip" : "100.177.58.231"                  },                  "sort" : [                    19904                  ]                }              ]            }          }        },        {          "key" : "www.elastic.co",          "doc_count" : 4779,          "most_bytes" : {            "hits" : {              "total" : {                "value" : 4779,                "relation" : "eq"              },              "max_score" : null,              "hits" : [                {                  "_index" : "kibana_sample_data_logs",                  "_type" : "_doc",                  "_id" : "4nNIHm8BjrINWI3xYWQl",                  "_score" : null,                  "_source" : {                    "bytes" : 19986,                    "ip" : "233.204.30.48",                    "clientip" : "233.204.30.48"                  },                  "sort" : [                    19986                  ]                },                {                  "_index" : "kibana_sample_data_logs",                  "_type" : "_doc",                  "_id" : "wnNIHm8BjrINWI3xW0Rj",                  "_score" : null,                  "_source" : {                    "bytes" : 19956,                    "ip" : "129.237.102.30",                    "clientip" : "129.237.102.30"                  },                  "sort" : [                    19956                  ]                }              ]            }          }        }      ]    }  }

从上面的返回结果可以看出来两个 hosts artifacts.elastic.co 及 www.elastic.co 各返回两个结果,并且它们是按照 bytes 的大小进行降序排列的。

细心的读者可能会发现这个和我之前介绍的 field collapsing 有些类似。只是 field collapsing 里针对每个桶有一个结果,并且是按照我们的要求进行排序的最高结果的那个。当然我们也可以含有多几个返回结果在 inner_hits 之中。

参考:

【1】https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html


最新活动

包含文章发布时段最新活动,前往ES产品介绍页,可查找ES当前活动统一入口

Elasticsearch Service自建迁移特惠政策>>

Elasticsearch Service 新用户特惠狂欢,最低4折首购优惠 >>

Elasticsearch Service 企业首购特惠,助力企业复工复产>>

关注“腾讯云大数据”公众号,技术交流、最新活动、服务专享一站Get~

本文系转载,前往查看

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

本文系转载前往查看

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 【腾讯云 Elasticsearch Service】高可用,可伸缩,云端全托管。集成X-Pack高级特性,适用日志分析/企业搜索/BI分析等场景
  • 准备数据:
  • Top hits aggregation
  • 最新活动
相关产品与服务
Elasticsearch Service
腾讯云 Elasticsearch Service(ES)是云端全托管海量数据检索分析服务,拥有高性能自研内核,集成X-Pack。ES 支持通过自治索引、存算分离、集群巡检等特性轻松管理集群,也支持免运维、自动弹性、按需使用的 Serverless 模式。使用 ES 您可以高效构建信息检索、日志分析、运维监控等服务,它独特的向量检索还可助您构建基于语义、图像的AI深度应用。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档