父 ID 查询
parent_id
查询返回其父文档具有指定 ID 的子文档。您可以通过使用 join 字段类型在同一索引中的文档之间建立父/子关系。
示例
在运行 parent_id
查询之前,您的索引必须包含 join 字段,以便建立父/子关系。索引映射请求使用以下格式:
PUT /example_index
{
"mappings": {
"properties": {
"relationship_field": {
"type": "join",
"relations": {
"parent_doc": "child_doc"
}
}
}
}
}
对于此示例,首先配置一个包含表示产品及其品牌的文档的索引,如 has_child
查询示例 中所述。
要搜索特定父文档的子文档,请使用 parent_id
查询。以下查询返回 ID 为 1
的父文档的子文档(产品):
GET testindex1/_search
{
"query": {
"parent_id": {
"type": "product",
"id": "1"
}
}
}
响应返回子产品:
{
"took": 57,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 0.87546873,
"hits": [
{
"_index": "testindex1",
"_id": "3",
"_score": 0.87546873,
"_routing": "1",
"_source": {
"name": "Mechanical watch",
"sales_count": 150,
"product_to_brand": {
"name": "product",
"parent": "1"
}
}
}
]
}
}
参数
下表列出了 parent_id
查询支持的所有顶级参数。
参数 | 必需/可选 | 描述 |
---|---|---|
type | 必需 | 指定 join 字段映射中定义的子关系的名称。 |
id | 必需 | 父文档的 ID。查询返回与此父文档关联的子文档。 |
ignore_unmapped | 可选 | 指示是否忽略未映射的 type 字段,并返回文档而不是抛出错误。在查询多个索引时可以提供此参数,其中一些索引可能不包含 type 字段。默认为 false 。 |