2013-08-28 19:24:34 -04:00
|
|
|
[[query-dsl-nested-query]]
|
2015-06-03 19:59:22 -04:00
|
|
|
=== Nested Query
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
Nested query allows to query nested objects / docs (see
|
2015-08-06 11:24:29 -04:00
|
|
|
<<nested,nested mapping>>). The
|
2013-08-28 19:24:34 -04:00
|
|
|
query is executed against the nested objects / docs as if they were
|
|
|
|
indexed as separate docs (they are, internally) and resulting in the
|
|
|
|
root parent doc (or parent nested mapping). Here is a sample mapping we
|
|
|
|
will work with:
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
2016-05-24 05:58:43 -04:00
|
|
|
PUT /my_index
|
2013-08-28 19:24:34 -04:00
|
|
|
{
|
2016-05-24 05:58:43 -04:00
|
|
|
"mappings": {
|
|
|
|
"type1" : {
|
|
|
|
"properties" : {
|
|
|
|
"obj1" : {
|
|
|
|
"type" : "nested"
|
|
|
|
}
|
2013-08-28 19:24:34 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2016-05-24 05:58:43 -04:00
|
|
|
// CONSOLE
|
|
|
|
// TESTSETUP
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
And here is a sample nested query usage:
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
2016-05-24 05:58:43 -04:00
|
|
|
GET /_search
|
2013-08-28 19:24:34 -04:00
|
|
|
{
|
2016-05-24 05:58:43 -04:00
|
|
|
"query": {
|
|
|
|
"nested" : {
|
|
|
|
"path" : "obj1",
|
|
|
|
"score_mode" : "avg",
|
|
|
|
"query" : {
|
|
|
|
"bool" : {
|
|
|
|
"must" : [
|
|
|
|
{ "match" : {"obj1.name" : "blue"} },
|
|
|
|
{ "range" : {"obj1.count" : {"gt" : 5}} }
|
|
|
|
]
|
|
|
|
}
|
2016-05-12 06:29:18 -04:00
|
|
|
}
|
2013-08-28 19:24:34 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2016-05-24 05:58:43 -04:00
|
|
|
// CONSOLE
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2015-10-27 10:57:10 -04:00
|
|
|
The query `path` points to the nested object path, and the `query`
|
|
|
|
includes the query that will run on the nested docs matching the
|
|
|
|
direct path, and joining with the root parent docs. Note that any
|
2015-08-06 11:24:29 -04:00
|
|
|
fields referenced inside the query must use the complete path (fully
|
2014-05-13 13:05:54 -04:00
|
|
|
qualified).
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
The `score_mode` allows to set how inner children matching affects
|
2015-06-29 06:26:30 -04:00
|
|
|
scoring of parent. It defaults to `avg`, but can be `sum`, `min`,
|
|
|
|
`max` and `none`.
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2016-04-14 04:37:18 -04:00
|
|
|
There is also an `ignore_unmapped` option which, when set to `true` will
|
|
|
|
ignore an unmapped `path` and will not match any documents for this query.
|
|
|
|
This can be useful when querying multiple indexes which might have different
|
|
|
|
mappings. When set to `false` (the default value) the query will throw an
|
|
|
|
exception if the `path` is not mapped.
|
|
|
|
|
2013-08-28 19:24:34 -04:00
|
|
|
Multi level nesting is automatically supported, and detected, resulting
|
|
|
|
in an inner nested query to automatically match the relevant nesting
|
|
|
|
level (and not root) if it exists within another nested query.
|