| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 | [[query-dsl-nested-query]]=== Nested query++++<titleabbrev>Nested</titleabbrev>++++Wraps another query to search <<nested,nested>> fields.The `nested` query searches nested field objects as if they were indexed asseparate documents. If an object matches the search, the `nested` query returnsthe root parent document.[[nested-query-ex-request]]==== Example request[[nested-query-index-setup]]===== Index setupTo use the `nested` query, your index must include a <<nested,nested>> fieldmapping. For example:[source,console]----PUT /my_index{    "mappings" : {        "properties" : {            "obj1" : {                "type" : "nested"            }        }    }}----[[nested-query-ex-query]]===== Example query[source,console]----GET /my_index/_search{    "query":  {        "nested" : {            "path" : "obj1",            "query" : {                "bool" : {                    "must" : [                    { "match" : {"obj1.name" : "blue"} },                    { "range" : {"obj1.count" : {"gt" : 5}} }                    ]                }            },            "score_mode" : "avg"        }    }}----// TEST[continued][[nested-top-level-params]]==== Top-level parameters for `nested``path`::(Required, string) Path to the nested object you wish to search.`query`::+--(Required, query object) Query you wish to run on nested objects in the `path`.If an object matches the search, the `nested` query returns the root parentdocument.You can search nested fields using dot notation that includes the complete path,such as `obj1.name`.Multi-level nesting is automatically supported, and detected, resulting in aninner nested query to automatically match the relevant nesting level, ratherthan root, if it exists within another nested query.See <<multi-level-nested-query-ex>> for an example.--`score_mode`::+--(Optional, string) Indicates how scores for matching child objects affect theroot parent document's <<relevance-scores,relevance score>>. Valid valuesare:`avg` (Default)::Use the mean relevance score of all matching child objects.`max`::Uses the highest relevance score of all matching child objects.`min`::Uses the lowest relevance score of all matching child objects.`none`::Do not use the relevance scores of matching child objects. The query assignsparent documents a score of `0`.`sum`::Add together the relevance scores of all matching child objects.--`ignore_unmapped`::+--(Optional, boolean) Indicates whether to ignore an unmapped `path` and notreturn any documents instead of an error. Defaults to `false`.If `false`, {es} returns an error if the `path` is an unmapped field.You can use this parameter to query multiple indices that may not contain thefield `path`.--[[nested-query-notes]]==== Notes[[multi-level-nested-query-ex]]===== Multi-level nested queriesTo see how multi-level nested queries work,first you need an index that has nested fields.The following request defines mappings for the `drivers` indexwith nested `make` and `model` fields.[source,console]----PUT /drivers{    "mappings" : {        "properties" : {            "driver" : {                "type" : "nested",                "properties" : {                    "last_name" : {                        "type" : "text"                    },                    "vehicle" : {                        "type" : "nested",                        "properties" : {                            "make" : {                                "type" : "text"                            },                            "model" : {                                "type" : "text"                            }                        }                    }                }            }        }    }}----Next, index some documents to the `drivers` index.[source,console]----PUT /drivers/_doc/1{  "driver" : {        "last_name" : "McQueen",        "vehicle" : [            {                "make" : "Powell Motors",                "model" : "Canyonero"            },            {                "make" : "Miller-Meteor",                "model" : "Ecto-1"            }        ]    }}PUT /drivers/_doc/2?refresh{  "driver" : {        "last_name" : "Hudson",        "vehicle" : [            {                "make" : "Mifune",                "model" : "Mach Five"            },            {                "make" : "Miller-Meteor",                "model" : "Ecto-1"            }        ]    }}----// TEST[continued]You can now use a multi-level nested queryto match documents based on the `make` and `model` fields.[source,console]----GET /drivers/_search{    "query" : {        "nested" : {            "path" : "driver",            "query" : {                "nested" : {                    "path" :  "driver.vehicle",                    "query" :  {                        "bool" : {                            "must" : [                                { "match" : { "driver.vehicle.make" : "Powell Motors" } },                                { "match" : { "driver.vehicle.model" : "Canyonero" } }                            ]                        }                    }                }            }        }    }}----// TEST[continued]The search request returns the following response:[source,console-result]----{  "took" : 5,  "timed_out" : false,  "_shards" : {    "total" : 1,    "successful" : 1,    "skipped" : 0,    "failed" : 0  },  "hits" : {    "total" : {      "value" : 1,      "relation" : "eq"    },    "max_score" : 3.7349272,    "hits" : [      {        "_index" : "drivers",        "_id" : "1",        "_score" : 3.7349272,        "_source" : {          "driver" : {            "last_name" : "McQueen",            "vehicle" : [              {                "make" : "Powell Motors",                "model" : "Canyonero"              },              {                "make" : "Miller-Meteor",                "model" : "Ecto-1"              }            ]          }        }      }    ]  }}----// TESTRESPONSE[s/"took" : 5/"took": $body.took/]
 |