| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 | [[query-dsl-nested-query]]=== Nested QueryNested query allows to query nested objects / docs (see<<nested,nested mapping>>). Thequery is executed against the nested objects / docs as if they wereindexed as separate docs (they are, internally) and resulting in theroot parent doc (or parent nested mapping). Here is a sample mapping wewill work with:[source,js]--------------------------------------------------PUT /my_index{    "mappings": {        "type1" : {            "properties" : {                "obj1" : {                    "type" : "nested"                }            }        }    }}--------------------------------------------------// CONSOLE// TESTSETUPAnd here is a sample nested query usage:[source,js]--------------------------------------------------GET /_search{    "query": {        "nested" : {            "path" : "obj1",            "score_mode" : "avg",            "query" : {                "bool" : {                    "must" : [                    { "match" : {"obj1.name" : "blue"} },                    { "range" : {"obj1.count" : {"gt" : 5}} }                    ]                }            }        }    }}--------------------------------------------------// CONSOLEThe query `path` points to the nested object path, and the `query`includes the query that will run on the nested docs matching thedirect path, and joining with the root parent docs. Note that anyfields referenced inside the query must use the complete path (fullyqualified).The `score_mode` allows to set how inner children matching affectsscoring of parent. It defaults to `avg`, but can be `sum`, `min`,`max` and `none`.There is also an `ignore_unmapped` option which, when set to `true` willignore an unmapped `path` and will not match any documents for this query.This can be useful when querying multiple indexes which might have differentmappings. When set to `false` (the default value) the query will throw anexception if the `path` is not mapped.Multi level nesting is automatically supported, and detected, resultingin an inner nested query to automatically match the relevant nestinglevel (and not root) if it exists within another nested query.
 |