- Elasticsearch 指南其他版本
- 8.17 中的新功能
- Elasticsearch 基础
- 快速入门
- 设置 Elasticsearch
- 升级 Elasticsearch
- 索引模块
- 映射
- 文本分析
- 索引模板
- 数据流
- 摄取管道
- 别名
- 搜索您的数据
- 重新排名
- 查询 DSL
- 聚合
- 地理空间分析
- 连接器
- EQL
- ES|QL
- SQL
- 脚本
- 数据管理
- 自动缩放
- 监视集群
- 汇总或转换数据
- 设置高可用性集群
- 快照和还原
- 保护 Elastic Stack 的安全
- Watcher
- 命令行工具
- elasticsearch-certgen
- elasticsearch-certutil
- elasticsearch-create-enrollment-token
- elasticsearch-croneval
- elasticsearch-keystore
- elasticsearch-node
- elasticsearch-reconfigure-node
- elasticsearch-reset-password
- elasticsearch-saml-metadata
- elasticsearch-service-tokens
- elasticsearch-setup-passwords
- elasticsearch-shard
- elasticsearch-syskeygen
- elasticsearch-users
- 优化
- 故障排除
- 修复常见的集群问题
- 诊断未分配的分片
- 向系统中添加丢失的层
- 允许 Elasticsearch 在系统中分配数据
- 允许 Elasticsearch 分配索引
- 索引将索引分配过滤器与数据层节点角色混合,以在数据层之间移动
- 没有足够的节点来分配所有分片副本
- 单个节点上索引的分片总数已超过
- 每个节点的分片总数已达到
- 故障排除损坏
- 修复磁盘空间不足的数据节点
- 修复磁盘空间不足的主节点
- 修复磁盘空间不足的其他角色节点
- 启动索引生命周期管理
- 启动快照生命周期管理
- 从快照恢复
- 故障排除损坏的存储库
- 解决重复的快照策略失败问题
- 故障排除不稳定的集群
- 故障排除发现
- 故障排除监控
- 故障排除转换
- 故障排除 Watcher
- 故障排除搜索
- 故障排除分片容量健康问题
- 故障排除不平衡的集群
- 捕获诊断信息
- REST API
- API 约定
- 通用选项
- REST API 兼容性
- 自动缩放 API
- 行为分析 API
- 紧凑和对齐文本 (CAT) API
- 集群 API
- 跨集群复制 API
- 连接器 API
- 数据流 API
- 文档 API
- 丰富 API
- EQL API
- ES|QL API
- 功能 API
- Fleet API
- 图表探索 API
- 索引 API
- 别名是否存在
- 别名
- 分析
- 分析索引磁盘使用量
- 清除缓存
- 克隆索引
- 关闭索引
- 创建索引
- 创建或更新别名
- 创建或更新组件模板
- 创建或更新索引模板
- 创建或更新索引模板(旧版)
- 删除组件模板
- 删除悬挂索引
- 删除别名
- 删除索引
- 删除索引模板
- 删除索引模板(旧版)
- 存在
- 字段使用情况统计信息
- 刷新
- 强制合并
- 获取别名
- 获取组件模板
- 获取字段映射
- 获取索引
- 获取索引设置
- 获取索引模板
- 获取索引模板(旧版)
- 获取映射
- 导入悬挂索引
- 索引恢复
- 索引段
- 索引分片存储
- 索引统计信息
- 索引模板是否存在(旧版)
- 列出悬挂索引
- 打开索引
- 刷新
- 解析索引
- 解析集群
- 翻转
- 收缩索引
- 模拟索引
- 模拟模板
- 拆分索引
- 解冻索引
- 更新索引设置
- 更新映射
- 索引生命周期管理 API
- 推理 API
- 信息 API
- 摄取 API
- 许可 API
- Logstash API
- 机器学习 API
- 机器学习异常检测 API
- 机器学习数据帧分析 API
- 机器学习训练模型 API
- 迁移 API
- 节点生命周期 API
- 查询规则 API
- 重新加载搜索分析器 API
- 存储库计量 API
- 汇总 API
- 根 API
- 脚本 API
- 搜索 API
- 搜索应用程序 API
- 可搜索快照 API
- 安全 API
- 身份验证
- 更改密码
- 清除缓存
- 清除角色缓存
- 清除权限缓存
- 清除 API 密钥缓存
- 清除服务帐户令牌缓存
- 创建 API 密钥
- 创建或更新应用程序权限
- 创建或更新角色映射
- 创建或更新角色
- 批量创建或更新角色 API
- 批量删除角色 API
- 创建或更新用户
- 创建服务帐户令牌
- 委托 PKI 身份验证
- 删除应用程序权限
- 删除角色映射
- 删除角色
- 删除服务帐户令牌
- 删除用户
- 禁用用户
- 启用用户
- 注册 Kibana
- 注册节点
- 获取 API 密钥信息
- 获取应用程序权限
- 获取内置权限
- 获取角色映射
- 获取角色
- 查询角色
- 获取服务帐户
- 获取服务帐户凭据
- 获取安全设置
- 获取令牌
- 获取用户权限
- 获取用户
- 授予 API 密钥
- 具有权限
- 使 API 密钥失效
- 使令牌失效
- OpenID Connect 准备身份验证
- OpenID Connect 身份验证
- OpenID Connect 注销
- 查询 API 密钥信息
- 查询用户
- 更新 API 密钥
- 更新安全设置
- 批量更新 API 密钥
- SAML 准备身份验证
- SAML 身份验证
- SAML 注销
- SAML 失效
- SAML 完成注销
- SAML 服务提供商元数据
- SSL 证书
- 激活用户配置文件
- 禁用用户配置文件
- 启用用户配置文件
- 获取用户配置文件
- 建议用户配置文件
- 更新用户配置文件数据
- 具有用户配置文件权限
- 创建跨集群 API 密钥
- 更新跨集群 API 密钥
- 快照和还原 API
- 快照生命周期管理 API
- SQL API
- 同义词 API
- 文本结构 API
- 转换 API
- 使用情况 API
- Watcher API
- 定义
- 迁移指南
- 发行说明
- Elasticsearch 版本 8.17.0
- Elasticsearch 版本 8.16.1
- Elasticsearch 版本 8.16.0
- Elasticsearch 版本 8.15.5
- Elasticsearch 版本 8.15.4
- Elasticsearch 版本 8.15.3
- Elasticsearch 版本 8.15.2
- Elasticsearch 版本 8.15.1
- Elasticsearch 版本 8.15.0
- Elasticsearch 版本 8.14.3
- Elasticsearch 版本 8.14.2
- Elasticsearch 版本 8.14.1
- Elasticsearch 版本 8.14.0
- Elasticsearch 版本 8.13.4
- Elasticsearch 版本 8.13.3
- Elasticsearch 版本 8.13.2
- Elasticsearch 版本 8.13.1
- Elasticsearch 版本 8.13.0
- Elasticsearch 版本 8.12.2
- Elasticsearch 版本 8.12.1
- Elasticsearch 版本 8.12.0
- Elasticsearch 版本 8.11.4
- Elasticsearch 版本 8.11.3
- Elasticsearch 版本 8.11.2
- Elasticsearch 版本 8.11.1
- Elasticsearch 版本 8.11.0
- Elasticsearch 版本 8.10.4
- Elasticsearch 版本 8.10.3
- Elasticsearch 版本 8.10.2
- Elasticsearch 版本 8.10.1
- Elasticsearch 版本 8.10.0
- Elasticsearch 版本 8.9.2
- Elasticsearch 版本 8.9.1
- Elasticsearch 版本 8.9.0
- Elasticsearch 版本 8.8.2
- Elasticsearch 版本 8.8.1
- Elasticsearch 版本 8.8.0
- Elasticsearch 版本 8.7.1
- Elasticsearch 版本 8.7.0
- Elasticsearch 版本 8.6.2
- Elasticsearch 版本 8.6.1
- Elasticsearch 版本 8.6.0
- Elasticsearch 版本 8.5.3
- Elasticsearch 版本 8.5.2
- Elasticsearch 版本 8.5.1
- Elasticsearch 版本 8.5.0
- Elasticsearch 版本 8.4.3
- Elasticsearch 版本 8.4.2
- Elasticsearch 版本 8.4.1
- Elasticsearch 版本 8.4.0
- Elasticsearch 版本 8.3.3
- Elasticsearch 版本 8.3.2
- Elasticsearch 版本 8.3.1
- Elasticsearch 版本 8.3.0
- Elasticsearch 版本 8.2.3
- Elasticsearch 版本 8.2.2
- Elasticsearch 版本 8.2.1
- Elasticsearch 版本 8.2.0
- Elasticsearch 版本 8.1.3
- Elasticsearch 版本 8.1.2
- Elasticsearch 版本 8.1.1
- Elasticsearch 版本 8.1.0
- Elasticsearch 版本 8.0.1
- Elasticsearch 版本 8.0.0
- Elasticsearch 版本 8.0.0-rc2
- Elasticsearch 版本 8.0.0-rc1
- Elasticsearch 版本 8.0.0-beta1
- Elasticsearch 版本 8.0.0-alpha2
- Elasticsearch 版本 8.0.0-alpha1
- 依赖项和版本
热门命中聚合
编辑热门命中聚合
编辑top_hits
指标聚合器会跟踪被聚合的最相关的文档。此聚合器旨在用作子聚合器,以便可以按存储桶聚合最匹配的文档。
我们不建议将 top_hits
用作顶层聚合。如果想要对搜索命中结果进行分组,请改用 collapse
参数。
top_hits
聚合器可以通过存储桶聚合器有效地按特定字段对结果集进行分组。一个或多个存储桶聚合器确定将结果集切分为哪些属性。
选项
编辑-
from
- 要获取的第一个结果的偏移量。 -
size
- 每个存储桶返回的最大匹配命中次数。默认情况下,返回前三个匹配的命中结果。 -
sort
- 应如何对最匹配的命中结果进行排序。默认情况下,命中结果按主查询的分数排序。
支持的每个命中的功能
编辑top_hits 聚合返回常规搜索命中结果,因此可以支持许多每个命中的功能。
如果 仅 需要 docvalue_fields
、size
和 sort
,则 热门指标 可能比热门命中聚合更有效。
top_hits
不支持 rescore
参数。查询重新评分仅适用于搜索命中结果,而不适用于聚合结果。要更改聚合使用的分数,请使用 function_score
或 script_score
查询。
示例
编辑在以下示例中,我们将销售额按类型分组,并显示每种类型的最后一次销售额。对于每次销售,仅在源中包含日期和价格字段。
resp = client.search( index="sales", size="0", aggs={ "top_tags": { "terms": { "field": "type", "size": 3 }, "aggs": { "top_sales_hits": { "top_hits": { "sort": [ { "date": { "order": "desc" } } ], "_source": { "includes": [ "date", "price" ] }, "size": 1 } } } } }, ) print(resp)
response = client.search( index: 'sales', size: 0, body: { aggregations: { top_tags: { terms: { field: 'type', size: 3 }, aggregations: { top_sales_hits: { top_hits: { sort: [ { date: { order: 'desc' } } ], _source: { includes: [ 'date', 'price' ] }, size: 1 } } } } } } ) puts response
const response = await client.search({ index: "sales", size: 0, aggs: { top_tags: { terms: { field: "type", size: 3, }, aggs: { top_sales_hits: { top_hits: { sort: [ { date: { order: "desc", }, }, ], _source: { includes: ["date", "price"], }, size: 1, }, }, }, }, }, }); console.log(response);
POST /sales/_search?size=0 { "aggs": { "top_tags": { "terms": { "field": "type", "size": 3 }, "aggs": { "top_sales_hits": { "top_hits": { "sort": [ { "date": { "order": "desc" } } ], "_source": { "includes": [ "date", "price" ] }, "size": 1 } } } } } }
可能的响应
{ ... "aggregations": { "top_tags": { "doc_count_error_upper_bound": 0, "sum_other_doc_count": 0, "buckets": [ { "key": "hat", "doc_count": 3, "top_sales_hits": { "hits": { "total" : { "value": 3, "relation": "eq" }, "max_score": null, "hits": [ { "_index": "sales", "_id": "AVnNBmauCQpcRyxw6ChK", "_source": { "date": "2015/03/01 00:00:00", "price": 200 }, "sort": [ 1425168000000 ], "_score": null } ] } } }, { "key": "t-shirt", "doc_count": 3, "top_sales_hits": { "hits": { "total" : { "value": 3, "relation": "eq" }, "max_score": null, "hits": [ { "_index": "sales", "_id": "AVnNBmauCQpcRyxw6ChL", "_source": { "date": "2015/03/01 00:00:00", "price": 175 }, "sort": [ 1425168000000 ], "_score": null } ] } } }, { "key": "bag", "doc_count": 1, "top_sales_hits": { "hits": { "total" : { "value": 1, "relation": "eq" }, "max_score": null, "hits": [ { "_index": "sales", "_id": "AVnNBmatCQpcRyxw6ChH", "_source": { "date": "2015/01/01 00:00:00", "price": 150 }, "sort": [ 1420070400000 ], "_score": null } ] } } } ] } } }
字段折叠示例
编辑字段折叠或结果分组是一项功能,可将结果集逻辑分组,并为每个组返回最上面的文档。组的顺序由组中第一个文档的相关性决定。在 Elasticsearch 中,可以通过一个包装 top_hits
聚合器作为子聚合器的存储桶聚合器来实现。
在下面的示例中,我们搜索抓取的网页。对于每个网页,我们存储正文和网页所属的域。通过在 domain
字段上定义一个 terms
聚合器,我们按域对网页的结果集进行分组。top_hits
聚合器随后被定义为子聚合器,以便为每个存储桶收集最匹配的命中结果。
此外,还定义了一个 max
聚合器,该聚合器由 terms
聚合器的 order 功能使用,以按存储桶中最相关文档的相关性顺序返回存储桶。
resp = client.search( index="sales", query={ "match": { "body": "elections" } }, aggs={ "top_sites": { "terms": { "field": "domain", "order": { "top_hit": "desc" } }, "aggs": { "top_tags_hits": { "top_hits": {} }, "top_hit": { "max": { "script": { "source": "_score" } } } } } }, ) print(resp)
response = client.search( index: 'sales', body: { query: { match: { body: 'elections' } }, aggregations: { top_sites: { terms: { field: 'domain', order: { top_hit: 'desc' } }, aggregations: { top_tags_hits: { top_hits: {} }, top_hit: { max: { script: { source: '_score' } } } } } } } ) puts response
const response = await client.search({ index: "sales", query: { match: { body: "elections", }, }, aggs: { top_sites: { terms: { field: "domain", order: { top_hit: "desc", }, }, aggs: { top_tags_hits: { top_hits: {}, }, top_hit: { max: { script: { source: "_score", }, }, }, }, }, }, }); console.log(response);
POST /sales/_search { "query": { "match": { "body": "elections" } }, "aggs": { "top_sites": { "terms": { "field": "domain", "order": { "top_hit": "desc" } }, "aggs": { "top_tags_hits": { "top_hits": {} }, "top_hit" : { "max": { "script": { "source": "_score" } } } } } } }
目前,需要 max
(或 min
) 聚合器来确保来自 terms
聚合器的存储桶按照每个域中最相关网页的分数排序。遗憾的是,top_hits
聚合器还不能用于 terms
聚合器的 order
选项中。
嵌套或反向嵌套聚合器中对 top_hits 的支持
编辑如果 top_hits
聚合器被包装在 nested
或 reverse_nested
聚合器中,则会返回嵌套命中结果。嵌套命中结果在某种意义上是隐藏的迷你文档,它们是常规文档的一部分,其中在映射中配置了嵌套字段类型。如果 top_hits
聚合器被包装在 nested
或 reverse_nested
聚合器中,则它具有取消隐藏这些文档的能力。请在 嵌套类型映射 中了解更多有关嵌套的信息。
如果配置了嵌套类型,则单个文档实际上会索引为多个 Lucene 文档,并且它们共享相同的 id。为了确定嵌套命中的身份,需要的不仅仅是 id,这就是为什么嵌套命中还包括它们的嵌套身份。嵌套身份保留在搜索命中结果中的 _nested
字段下,并且包括数组字段和嵌套命中所属的数组字段中的偏移量。偏移量从零开始。
让我们看看它如何与一个真实的示例一起使用。考虑以下映射
resp = client.indices.create( index="sales", mappings={ "properties": { "tags": { "type": "keyword" }, "comments": { "type": "nested", "properties": { "username": { "type": "keyword" }, "comment": { "type": "text" } } } } }, ) print(resp)
response = client.indices.create( index: 'sales', body: { mappings: { properties: { tags: { type: 'keyword' }, comments: { type: 'nested', properties: { username: { type: 'keyword' }, comment: { type: 'text' } } } } } } ) puts response
const response = await client.indices.create({ index: "sales", mappings: { properties: { tags: { type: "keyword", }, comments: { type: "nested", properties: { username: { type: "keyword", }, comment: { type: "text", }, }, }, }, }, }); console.log(response);
PUT /sales { "mappings": { "properties": { "tags": { "type": "keyword" }, "comments": { "type": "nested", "properties": { "username": { "type": "keyword" }, "comment": { "type": "text" } } } } } }
以及一些文档
resp = client.index( index="sales", id="1", refresh=True, document={ "tags": [ "car", "auto" ], "comments": [ { "username": "baddriver007", "comment": "This car could have better brakes" }, { "username": "dr_who", "comment": "Where's the autopilot? Can't find it" }, { "username": "ilovemotorbikes", "comment": "This car has two extra wheels" } ] }, ) print(resp)
response = client.index( index: 'sales', id: 1, refresh: true, body: { tags: [ 'car', 'auto' ], comments: [ { username: 'baddriver007', comment: 'This car could have better brakes' }, { username: 'dr_who', comment: "Where's the autopilot? Can't find it" }, { username: 'ilovemotorbikes', comment: 'This car has two extra wheels' } ] } ) puts response
const response = await client.index({ index: "sales", id: 1, refresh: "true", document: { tags: ["car", "auto"], comments: [ { username: "baddriver007", comment: "This car could have better brakes", }, { username: "dr_who", comment: "Where's the autopilot? Can't find it", }, { username: "ilovemotorbikes", comment: "This car has two extra wheels", }, ], }, }); console.log(response);
PUT /sales/_doc/1?refresh { "tags": [ "car", "auto" ], "comments": [ { "username": "baddriver007", "comment": "This car could have better brakes" }, { "username": "dr_who", "comment": "Where's the autopilot? Can't find it" }, { "username": "ilovemotorbikes", "comment": "This car has two extra wheels" } ] }
现在可以执行以下 top_hits
聚合(包装在 nested
聚合中)
resp = client.search( index="sales", query={ "term": { "tags": "car" } }, aggs={ "by_sale": { "nested": { "path": "comments" }, "aggs": { "by_user": { "terms": { "field": "comments.username", "size": 1 }, "aggs": { "by_nested": { "top_hits": {} } } } } } }, ) print(resp)
response = client.search( index: 'sales', body: { query: { term: { tags: 'car' } }, aggregations: { by_sale: { nested: { path: 'comments' }, aggregations: { by_user: { terms: { field: 'comments.username', size: 1 }, aggregations: { by_nested: { top_hits: {} } } } } } } } ) puts response
const response = await client.search({ index: "sales", query: { term: { tags: "car", }, }, aggs: { by_sale: { nested: { path: "comments", }, aggs: { by_user: { terms: { field: "comments.username", size: 1, }, aggs: { by_nested: { top_hits: {}, }, }, }, }, }, }, }); console.log(response);
POST /sales/_search { "query": { "term": { "tags": "car" } }, "aggs": { "by_sale": { "nested": { "path": "comments" }, "aggs": { "by_user": { "terms": { "field": "comments.username", "size": 1 }, "aggs": { "by_nested": { "top_hits": {} } } } } } } }
带有嵌套命中的热门命中响应代码段,该命中位于数组字段 comments
的第一个槽中
{ ... "aggregations": { "by_sale": { "by_user": { "buckets": [ { "key": "baddriver007", "doc_count": 1, "by_nested": { "hits": { "total" : { "value": 1, "relation": "eq" }, "max_score": 0.3616575, "hits": [ { "_index": "sales", "_id": "1", "_nested": { "field": "comments", "offset": 0 }, "_score": 0.3616575, "_source": { "comment": "This car could have better brakes", "username": "baddriver007" } } ] } } } ... ] } } } }
如果请求了 _source
,则仅返回嵌套对象的源部分,而不是整个文档的源。还可以通过位于 nested
或 reverse_nested
聚合器中的 top_hits
聚合器访问 嵌套 内部对象级别的存储字段。
只有嵌套命中结果的命中结果中才会有 _nested
字段,非嵌套(常规)命中结果则不会有 _nested
字段。
如果未启用 _source
,则 _nested
中的信息也可以用于在其他地方解析原始源。
如果在映射中定义了多个级别的嵌套对象类型,则 _nested
信息也可以是分层的,以便表达深度为两层或更多的嵌套命中的身份。
在下面的示例中,嵌套命中结果位于字段 nested_grand_child_field
的第一个槽中,该字段随后位于 nested_child_field
字段的第二个慢槽中
... "hits": { "total" : { "value": 2565, "relation": "eq" }, "max_score": 1, "hits": [ { "_index": "a", "_id": "1", "_score": 1, "_nested" : { "field" : "nested_child_field", "offset" : 1, "_nested" : { "field" : "nested_grand_child_field", "offset" : 0 } } "_source": ... }, ... ] } ...
在管道聚合中使用
编辑top_hits
可用于管道聚合,该管道聚合消耗每个存储桶的单个值,例如 bucket_selector
,它应用每个存储桶的过滤,类似于在 SQL 中使用 HAVING 子句。这需要将 size
设置为 1,并为要传递给包装聚合器的值指定正确的路径。后者可以是 _source
、_sort
或 _score
值。例如
resp = client.search( index="sales", size="0", aggs={ "top_tags": { "terms": { "field": "type", "size": 3 }, "aggs": { "top_sales_hits": { "top_hits": { "sort": [ { "date": { "order": "desc" } } ], "_source": { "includes": [ "date", "price" ] }, "size": 1 } }, "having.top_salary": { "bucket_selector": { "buckets_path": { "tp": "top_sales_hits[_source.price]" }, "script": "params.tp < 180" } } } } }, ) print(resp)
const response = await client.search({ index: "sales", size: 0, aggs: { top_tags: { terms: { field: "type", size: 3, }, aggs: { top_sales_hits: { top_hits: { sort: [ { date: { order: "desc", }, }, ], _source: { includes: ["date", "price"], }, size: 1, }, }, "having.top_salary": { bucket_selector: { buckets_path: { tp: "top_sales_hits[_source.price]", }, script: "params.tp < 180", }, }, }, }, }, }); console.log(response);
POST /sales/_search?size=0 { "aggs": { "top_tags": { "terms": { "field": "type", "size": 3 }, "aggs": { "top_sales_hits": { "top_hits": { "sort": [ { "date": { "order": "desc" } } ], "_source": { "includes": [ "date", "price" ] }, "size": 1 } }, "having.top_salary": { "bucket_selector": { "buckets_path": { "tp": "top_sales_hits[_source.price]" }, "script": "params.tp < 180" } } } } } }
bucket_path
使用 top_hits
名称 top_sales_hits
和提供聚合值的字段的关键字,即上面示例中的 _source
字段 price
。其他选项包括 top_sales_hits[_sort]
,用于筛选上面的排序值 date
,以及 top_sales_hits[_score]
,用于筛选热门命中的分数。