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