@@ -23,7 +23,7 @@ mapping. For example:
2323----
2424PUT /my_index
2525{
26- "mappings": {
26+ "mappings" : {
2727 "properties" : {
2828 "obj1" : {
2929 "type" : "nested"
@@ -34,7 +34,6 @@ PUT /my_index
3434
3535----
3636// CONSOLE
37- // TESTSETUP
3837
3938[[nested-query-ex-query]]
4039===== Example query
@@ -43,7 +42,7 @@ PUT /my_index
4342----
4443GET /my_index/_search
4544{
46- "query": {
45+ "query": {
4746 "nested" : {
4847 "path" : "obj1",
4948 "query" : {
@@ -60,6 +59,7 @@ GET /my_index/_search
6059}
6160----
6261// CONSOLE
62+ // TEST[continued]
6363
6464[[nested-top-level-params]]
6565==== Top-level parameters for `nested`
@@ -80,6 +80,8 @@ such as `obj1.name`.
8080Multi-level nesting is automatically supported, and detected, resulting in an
8181inner nested query to automatically match the relevant nesting level, rather
8282than root, if it exists within another nested query.
83+
84+ See <<multi-level-nested-query-ex>> for an example.
8385--
8486
8587`score_mode`::
@@ -116,4 +118,164 @@ If `false`, {es} returns an error if the `path` is an unmapped field.
116118
117119You can use this parameter to query multiple indices that may not contain the
118120field `path`.
119- --
121+ --
122+
123+ [[nested-query-notes]]
124+ ==== Notes
125+
126+ [[multi-level-nested-query-ex]]
127+ ===== Multi-level nested queries
128+
129+ To see how multi-level nested queries work,
130+ first you need an index that has nested fields.
131+ The following request defines mappings for the `drivers` index
132+ with nested `make` and `model` fields.
133+
134+ [source,js]
135+ ----
136+ PUT /drivers
137+ {
138+ "mappings" : {
139+ "properties" : {
140+ "driver" : {
141+ "type" : "nested",
142+ "properties" : {
143+ "last_name" : {
144+ "type" : "text"
145+ },
146+ "vehicle" : {
147+ "type" : "nested",
148+ "properties" : {
149+ "make" : {
150+ "type" : "text"
151+ },
152+ "model" : {
153+ "type" : "text"
154+ }
155+ }
156+ }
157+ }
158+ }
159+ }
160+ }
161+ }
162+ ----
163+ // CONSOLE
164+
165+ Next, index some documents to the `drivers` index.
166+
167+ [source,js]
168+ ----
169+ PUT /drivers/_doc/1
170+ {
171+ "driver" : {
172+ "last_name" : "McQueen",
173+ "vehicle" : [
174+ {
175+ "make" : "Powell Motors",
176+ "model" : "Canyonero"
177+ },
178+ {
179+ "make" : "Miller-Meteor",
180+ "model" : "Ecto-1"
181+ }
182+ ]
183+ }
184+ }
185+
186+ PUT /drivers/_doc/2?refresh
187+ {
188+ "driver" : {
189+ "last_name" : "Hudson",
190+ "vehicle" : [
191+ {
192+ "make" : "Mifune",
193+ "model" : "Mach Five"
194+ },
195+ {
196+ "make" : "Miller-Meteor",
197+ "model" : "Ecto-1"
198+ }
199+ ]
200+ }
201+ }
202+ ----
203+ // CONSOLE
204+ // TEST[continued]
205+
206+ You can now use a multi-level nested query
207+ to match documents based on the `make` and `model` fields.
208+
209+ [source,js]
210+ ----
211+ GET /drivers/_search
212+ {
213+ "query" : {
214+ "nested" : {
215+ "path" : "driver",
216+ "query" : {
217+ "nested" : {
218+ "path" : "driver.vehicle",
219+ "query" : {
220+ "bool" : {
221+ "must" : [
222+ { "match" : { "driver.vehicle.make" : "Powell Motors" } },
223+ { "match" : { "driver.vehicle.model" : "Canyonero" } }
224+ ]
225+ }
226+ }
227+ }
228+ }
229+ }
230+ }
231+ }
232+ ----
233+ // CONSOLE
234+ // TEST[continued]
235+
236+ The search request returns the following response:
237+
238+ [source,js]
239+ ----
240+ {
241+ "took" : 5,
242+ "timed_out" : false,
243+ "_shards" : {
244+ "total" : 1,
245+ "successful" : 1,
246+ "skipped" : 0,
247+ "failed" : 0
248+ },
249+ "hits" : {
250+ "total" : {
251+ "value" : 1,
252+ "relation" : "eq"
253+ },
254+ "max_score" : 3.7349272,
255+ "hits" : [
256+ {
257+ "_index" : "drivers",
258+ "_type" : "_doc",
259+ "_id" : "1",
260+ "_score" : 3.7349272,
261+ "_source" : {
262+ "driver" : {
263+ "last_name" : "McQueen",
264+ "vehicle" : [
265+ {
266+ "make" : "Powell Motors",
267+ "model" : "Canyonero"
268+ },
269+ {
270+ "make" : "Miller-Meteor",
271+ "model" : "Ecto-1"
272+ }
273+ ]
274+ }
275+ }
276+ }
277+ ]
278+ }
279+ }
280+ ----
281+ // TESTRESPONSE[s/"took" : 5/"took": $body.took/]
0 commit comments