[DOCS] Add multi-level nested query example to nested query docs (#46986)

This commit is contained in:
James Rodewig 2019-09-25 02:01:37 -04:00 committed by GitHub
parent 4faba9cbbf
commit 61eef2fd31
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 163 additions and 4 deletions

View File

@ -23,7 +23,7 @@ mapping. For example:
---- ----
PUT /my_index PUT /my_index
{ {
"mappings": { "mappings" : {
"properties" : { "properties" : {
"obj1" : { "obj1" : {
"type" : "nested" "type" : "nested"
@ -33,7 +33,6 @@ PUT /my_index
} }
---- ----
// TESTSETUP
[[nested-query-ex-query]] [[nested-query-ex-query]]
===== Example query ===== Example query
@ -42,7 +41,7 @@ PUT /my_index
---- ----
GET /my_index/_search GET /my_index/_search
{ {
"query": { "query": {
"nested" : { "nested" : {
"path" : "obj1", "path" : "obj1",
"query" : { "query" : {
@ -58,6 +57,7 @@ GET /my_index/_search
} }
} }
---- ----
// TEST[continued]
[[nested-top-level-params]] [[nested-top-level-params]]
==== Top-level parameters for `nested` ==== Top-level parameters for `nested`
@ -78,6 +78,8 @@ such as `obj1.name`.
Multi-level nesting is automatically supported, and detected, resulting in an Multi-level nesting is automatically supported, and detected, resulting in an
inner nested query to automatically match the relevant nesting level, rather inner nested query to automatically match the relevant nesting level, rather
than root, if it exists within another nested query. than root, if it exists within another nested query.
See <<multi-level-nested-query-ex>> for an example.
-- --
`score_mode`:: `score_mode`::
@ -114,4 +116,161 @@ 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 the You can use this parameter to query multiple indices that may not contain the
field `path`. field `path`.
-- --
[[nested-query-notes]]
==== Notes
[[multi-level-nested-query-ex]]
===== Multi-level nested queries
To see how multi-level nested queries work,
first you need an index that has nested fields.
The following request defines mappings for the `drivers` index
with 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 query
to 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",
"_type" : "_doc",
"_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/]