> For the complete documentation index, see [llms.txt](https://xiaoxiami.gitbook.io/elasticsearch/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://xiaoxiami.gitbook.io/elasticsearch/ji-chu/35query-dsldslfang-shi-cha-8be229/356joining-cha-xun-ff08-lian-jie-cha-xun-ff09/parent-id-query.md).

# Parent Id Query

> 注意: 5.0.0中新添加

parent\_id 查询可以用来查找那些属于一个特殊父类型的子文档。给出如下映射的定义：

```
curl -XPUT 'localhost:9200/my_index?pretty' -d'
{
    "mappings": {
        "blog_post": {
            "properties": {
                "name": {
                    "type": "keyword"
                }
            }
        },
        "blog_tag": {
            "_parent": {
                "type": "blog_post"
            },
            "_routing": {
                "required": true
            }
        }
    }
}'
```

```
curl -XGET 'localhost:9200/my_index/_search?pretty' -d'
{
    "query": {
        "parent_id" : {
            "type" : "blog_tag",
                "id" : "1"
        }
    }
}'
```

上面的设置与使用下面的[`has_parent`](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-has-parent-query.html)查询在功能上相似，但是如果不需要做连接的话，下面的操作会更好。

```
curl -XGET 'localhost:9200/my_index/_search?pretty' -d'
{
  "query": {
    "has_parent": {
      "parent_type": "blog_post",
        "query": {
          "term": {
            "_id": "1"
        }
      }
    }
  }
}'
```

## Parameters(参数)

这个查询有两个必备的参数。

| type             | 子类型。必须是一个\_parent字段定义的类型                                                                        |
| ---------------- | ----------------------------------------------------------------------------------------------- |
| id               | 选择文档必须参照要求的parent文档id                                                                           |
| ignore\_unmapped | 当设置为true时，这个参数将忽略一个未被映射的类型，这个查询将不匹配任何文档。这个在可能产生不同的映射的多索引查询中起作用。当设置为flase(默认值)，如果类型未被映射，则查询将报异常。 |
