> 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/nested-queryqian-tao-cha-8be229.md).

# Nested Query(嵌套查询)

嵌套查询允许查询被嵌套的对象/文档(参考[nested mapping](https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html))。查询是在被索引分隔开的文档/嵌套的对象(内部)执行的，返回的结果是根文档(或者父嵌套映射)。这里是我们将用到的映射示例：

```
curl -XPUT 'localhost:9200/my_index?pretty' -d'
{
    "mappings": {
        "type1" : {
            "properties" : {
                "obj1" : {
                    "type" : "nested"
                }
            }
        }
    }
}'
```

这里是嵌套查询用法的示例：

```
curl -XGET 'localhost:9200/_search?pretty' -d'
{
    "query": {
        "nested" : {
            "path" : "obj1",
            "score_mode" : "avg",
            "query" : {
                "bool" : {
                    "must" : [
                    { "match" : {"obj1.name" : "blue"} },
                    { "range" : {"obj1.count" : {"gt" : 5}} }
                    ]
                }
            }
        }
    }
}'
```

查询的路径指向了被嵌套的对象路径，查询包括查询将运行在匹配上的嵌套的文档和连接跟文档的直接路径上。注意任何在查询内部引用的字段必须使用全路径。

score\_mode允许我们设置可以内部子嵌套对匹配到的父嵌套的文档的影响。默认是 avg，但是也可以是 sum,min,max,以及 none。

多层级的嵌套自动被支持，检测，如果一个嵌套查询存在另一个嵌套查询中，将产生一个内部嵌套查询去自动匹配有联系的嵌套(不是根)。
