@@ -23,7 +23,7 @@ mapping. For example:
23
23
----
24
24
PUT /my_index
25
25
{
26
- "mappings": {
26
+ "mappings" : {
27
27
"properties" : {
28
28
"obj1" : {
29
29
"type" : "nested"
@@ -34,7 +34,6 @@ PUT /my_index
34
34
35
35
----
36
36
// CONSOLE
37
- // TESTSETUP
38
37
39
38
[[nested-query-ex-query]]
40
39
===== Example query
@@ -43,7 +42,7 @@ PUT /my_index
43
42
----
44
43
GET /my_index/_search
45
44
{
46
- "query": {
45
+ "query": {
47
46
"nested" : {
48
47
"path" : "obj1",
49
48
"query" : {
@@ -60,6 +59,7 @@ GET /my_index/_search
60
59
}
61
60
----
62
61
// CONSOLE
62
+ // TEST[continued]
63
63
64
64
[[nested-top-level-params]]
65
65
==== Top-level parameters for `nested`
@@ -80,6 +80,8 @@ such as `obj1.name`.
80
80
Multi-level nesting is automatically supported, and detected, resulting in an
81
81
inner nested query to automatically match the relevant nesting level, rather
82
82
than root, if it exists within another nested query.
83
+
84
+ See <<multi-level-nested-query-ex>> for an example.
83
85
--
84
86
85
87
`score_mode`::
@@ -116,4 +118,164 @@ If `false`, {es} returns an error if the `path` is an unmapped field.
116
118
117
119
You can use this parameter to query multiple indices that may not contain the
118
120
field `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