> 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-child-query.md).

# Has Child Query

has\_child 过滤接受一个查询和child类型来执行，产生其子文档能匹配查询的父文档。这有一个例子：

```
curl -XGET 'localhost:9200/_search?pretty' -d'
{
    "query": {
        "has_child" : {
            "type" : "blog_tag",
                "query" : {
                    "term" : {
                        "tag" : "something"
                    }
                }
        }
    }
}'
```

## Scoring capabilities(打分能力)

has\_child也支持打分。支持的模式有min,max,sum,avg,或者none.默认的是none，产生和以前的版本相同的行为。如果打分模式设置为none意外的其他值，匹配子文档的所有分数将被聚合在有关联的父文档。has\_child查询中的score\_mode字段指定打分类型的值：

```
curl -XGET 'localhost:9200/_search?pretty' -d'
{
    "query": {
        "has_child" : {
            "type" : "blog_tag",
                "score_mode" : "min",
                "query" : {
                    "term" : {
                        "tag" : "something"
                    }
                }
        }
    }
}'
```

## Min/Max Children(最小/最大子文档)

has\_child 查询允许你指定最小或者(和)最大数目的子文档需要匹配，来使父文档被当做一个匹配：

```
curl -XGET 'localhost:9200/_search?pretty' -d'
{
    "query": {
        "has_child" : {
            "type" : "blog_tag",
            "score_mode" : "min",
            "min_children": 2,
            "max_children": 10, 
            "query" : {
                "term" : {
                    "tag" : "something"
                }
            }
        }
    }
}'
```

```
min_children和max_children都是可选的
```

`min_children` 和 `max_children` 参数可以和score\_mode参数结合使用

## ignore unmapped(忽略未映射)

如果把Ignore\_unmapped选项设置为true,将忽略一个没被索引的类型，从该查询中不会匹配任何文档。这个选项在查询不同的映射导致的多索引查询时有用。当设置为false(默认值),如果类型没有被映射时查询将抛出异常。

## Sorting(排序)

父文档不能以经常作为子文档的排序字段来排序。如果你需要用子文档中的字段来给父文档排序时，你可以用function\_score查询，使用\_score来排序。

根据子文档的click\_count字段排序：

```
curl -XGET 'localhost:9200/_search?pretty' -d'
{
    "query": {
        "has_child" : {
            "type" : "blog_tag",
            "score_mode" : "max",
            "query" : {
                "function_score" : {
                    "script_score": {
                        "script": "_score * doc[\u0027click_count\u0027].value"
                    }
                }
            }
        }
    }
}'
```
