解码 Elasticsearch 查询 DSL:利用 Elasticsearch 中的 has_child 和 has_parent 查询进行父子文档搜索

今天,让我们深入研究 has_child 查询和 has_parent 查询,这将帮助我们将 2 个不同的文档组合到一个索引中,从而使我们能够将它们与关系关联起来。 这样做会对我们搜索相关文档时有很大帮助。 在使用 has_child 及 has_parent 这种关系时,我们必须使用 join 数据类型。更多有关 join 数据类型的介绍,请参考文章 "Elasticsearch: Join 数据类型"。在实际的使用时,我们必须注意的一点是: join 不能跨索引,Elasticsearch 的重点在于速度,而传统 join 的运行速度太慢。 因此,子文档和父文档都必须位于相同的索引和相同的分片中。

了解亲子关系

Elasticsearch 中的父子关系涉及将一个索引中的文档作为父项与另一索引中的文档相关联。 这种关系对于数据组织需要分层结构、需要分层搜索的场景非常重要。 has_child 和 has_parent 查询为我们提供了在这些关系中轻松准确地导航和搜索的选项。

has_child 查询

has_child 查询允许我们根据关联子文档的内容搜索父文档。 当我们想根据孩子的属性对父母进行过滤或评分时,此查询主要有用。

句法

markdown 复制代码
1.  {
2.    "query": {
3.      "has_child": {
4.        "type": "child_type",
5.        "query": {
6.          "match": {
7.            "field": "value"
8.          }
9.        }
10.      }
11.    }
12.  }

让我们考虑一下,我们的 "fruits" 索引中有父子关系,其中每个 fruit 文档都是父文档,这些水果的 nutritional_info 信息存储为子文档。 has_child 查询可用于查找具有特定营养价值的水果。

json 复制代码
1.  {
2.    "query": {
3.      "has_child": {
4.        "type": "nutritional_info",
5.        "query": {
6.          "range": {
7.            "vitamin_c": {
8.              "gte": 20
9.            }
10.          }
11.        }
12.      }
13.    }
14.  }
json 复制代码
1.  {
2.    "query": {
3.      "has_child": {
4.        "type": "nutritional_info",
5.        "query": {
6.          "range": {
7.            "vitamin_c": {
8.              "gte": 20
9.            }
10.          }
11.        }
12.      }
13.    }
14.  }

在此示例中,我们正在搜索具有子文档(营养信息)的水果,其中维生素 C 含量大于或等于 20。

json 复制代码
1.  {
2.    "query": {
3.      "has_parent": {
4.        "parent_type": "parent_type",
5.        "query": {
6.          "term": {
7.            "field": "value"
8.          }
9.        }
10.      }
11.    }
12.  }

在与我们上面使用的具有父子关系的 "Fruits" 索引相同的示例中,我们假设每个水果文档都是父文档,并且营养信息存储为子文档。 has_child 查询可用于查找具有特定营养价值的水果。 这是一个例子:

json 复制代码
1.  {
2.    "query": {
3.      "has_child": {
4.        "type": "nutritional_info",
5.        "query": {
6.          "range": {
7.            "vitamin_c": {
8.              "gte": 20
9.            }
10.          }
11.        }
12.      }
13.    }
14.  }

在此示例中,我们正在搜索具有子文档(营养信息)的水果,其中维生素 C 含量大于或等于 20。

所以基本上 has_child 和 has_parent 查询只是彼此的关系。

实时用例:

分层数据建模

让我们考虑一个场景,我们正在对组织结构或产品类别等分层数据进行建模。 has_child 和 has_parent 查询使我们能够检索层次结构各个级别的相关信息,从而促进高效的数据导航。

内容过滤

在内容管理系统中,我们可能有代表文章的父文档和代表评论的子文档。 利用 has_child 查询,我们可以根据评论中的特定条件过滤文章,并为客户提供精致的搜索体验。

虽然 has_child 和 has_parent 查询为相关文档搜索提供了强大的选项,但我们应该始终小心常见问题,例如索引之间的映射不一致、较大层次结构的查询性能问题等。

为了最大限度地提高 has_child 和 has_parent 查询的有效性,我们应该确保始终遵循以下最佳实践。

  1. 索引设置:索引设置应配置为支持有效的父子关系。 根据用例适当调整 "index.mapping.single_type" 等设置。
  2. 查询优化:应使用缓存和过滤等查询优化技术来增强搜索性能。

因此,如果这是用于理解 has_child 和 has_parent 查询的入门知识,Elastic 的官方文档可以帮助我们更多地了解有关相同内容的更多详细信息。

更多阅读:Elasticsearch:在 Elasticsearch 中的 join 数据类型父子关系

相关推荐
vx-程序开发16 小时前
【计算机毕设】django校园跑腿服务系统83141
java·数据库·vue.js·spring boot·spring·elasticsearch·django
Elasticsearch17 小时前
jina-ocr-v1:在低成本 GPU 上实现更快速的文档解析
elasticsearch
深海鱼肝油ya18 小时前
向量数据库Elasticsearch(四)搜索文档——各种方式
人工智能·elasticsearch·向量数据库·es搜索文档·只能体·非结构化数据库
Elasticsearch1 天前
SNAP 支付错误检测:Elastic 如何帮助美国各州应对 FY2028 的处罚
elasticsearch
Elastic 中国社区官方博客1 天前
使用 Elasticsearch 和 Jina 进行 AI 视频搜索:精准找到你需要的视频片段秒数
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai·全文检索
Elasticsearch1 天前
2026 年唯一获得满分 Endpoint Prevention and Response(EPR)评分的是 Elastic
elasticsearch
Elasticsearch2 天前
没有小到无法记录的日志文件:Elastic Agent 如何跟踪低于 1 KiB 阈值的文件
elasticsearch
Elasticsearch2 天前
使用 Elasticsearch 和 Jina 进行 AI 视频搜索:精准找到你需要的视频片段秒数
elasticsearch
Hrain-AI2 天前
AI 爬虫三档授权工程落地:搜索放行、训练拦截、Agent 分层(附脚本)
人工智能·elasticsearch·milvus
智搜广告2 天前
智搜广告:科技行业AI回答优化公司如何破局
大数据·python·elasticsearch·geo