> 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/has-parent-query.md).

# Has Parent Query

has\_parent查询接受一个查询和父类型。查询是在父文档空间上执行的，它是被被父类型指定的。查询返回相关父文档匹配到的子文档。剩下的设置选项和has\_child查询一样

```
curl -XGET 'localhost:9200/_search?pretty' -d'
{
    "query": {
        "has_parent" : {
            "parent_type" : "blog",
                "query" : {
                    "term" : {
                        "tag" : "something"
                    }
                }
        }
    }
}'
```

## Scoring Capabilities(打分能力)

has\_parent也支持打分。默认的是flase，将忽略父文档的分数。这里的分数与has\_parent查询(默认为1)的分数等同。如果score设置为true，则匹配的 parent 文档的分数就被聚合在 child 文档中属于匹配的 parent 文档。打分模式可以使用 `score_mode` 在 `has_parent` query 中指定：

```
curl -XGET 'localhost:9200/_search?pretty' -d'
{
    "query": {
        "has_parent" : {
            "parent_type" : "blog",
                "score" : true,
                "query" : {
                    "term" : {
                        "tag" : "something"
                    }
                }
        }
    }
}'
```

## ignore unmapped(忽略未映射的)

当将ignore\_unmapped设置为true时，将忽略一个为被映射的类型，该查询不能匹配任何文档。这个在可能产生不同的映射的多索引查询中起作用。当设置为flase(默认值)，如果类型未被映射，则查询将报异常。

## Sorting(排序)

子文档不能以父文档中的排序字段来排序。如果你需要通过父文档中的字段来排序子文档，则你可以用function\_score查询，通过\_score来排序。

通过父文档中的view\_count字段排序tags:

```
curl -XGET 'localhost:9200/_search?pretty' -d'
{
    "query": {
        "has_parent" : {
            "parent_type" : "blog",
            "score" : true,
            "query" : {
                "function_score" : {
                    "script_score": {
                        "script": "_score * doc[\u0027view_count\u0027].value"
                    }
                }
            }
        }
    }
}'
```
