Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Sep 25, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
170 changes: 166 additions & 4 deletions docs/reference/query-dsl/nested-query.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ mapping. For example:
----
PUT /my_index
{
"mappings": {
"mappings" : {
"properties" : {
"obj1" : {
"type" : "nested"
Expand All @@ -34,7 +34,6 @@ PUT /my_index

----
// CONSOLE
// TESTSETUP

[[nested-query-ex-query]]
===== Example query
Expand All @@ -43,7 +42,7 @@ PUT /my_index
----
GET /my_index/_search
{
"query": {
"query": {
"nested" : {
"path" : "obj1",
"query" : {
Expand All @@ -60,6 +59,7 @@ GET /my_index/_search
}
----
// CONSOLE
// TEST[continued]

[[nested-top-level-params]]
==== Top-level parameters for `nested`
Expand All @@ -80,6 +80,8 @@ such as `obj1.name`.
Multi-level nesting is automatically supported, and detected, resulting in an
inner nested query to automatically match the relevant nesting level, rather
than root, if it exists within another nested query.

See <<multi-level-nested-query-ex>> for an example.
--

`score_mode`::
Expand Down Expand Up @@ -116,4 +118,164 @@ 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
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,js]
----
PUT /drivers
{
"mappings" : {
"properties" : {
"driver" : {
"type" : "nested",
"properties" : {
"last_name" : {
"type" : "text"
},
"vehicle" : {
"type" : "nested",
"properties" : {
"make" : {
"type" : "text"
},
"model" : {
"type" : "text"
}
}
}
}
}
}
}
}
----
// CONSOLE

Next, index some documents to the `drivers` index.

[source,js]
----
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"
}
]
}
}
----
// CONSOLE
// TEST[continued]

You can now use a multi-level nested query
to match documents based on the `make` and `model` fields.

[source,js]
----
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" } }
]
}
}
}
}
}
}
}
----
// CONSOLE
// TEST[continued]

The search request returns the following response:

[source,js]
----
{
"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/]