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

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

Merged
merged 8 commits into from
Sep 25, 2019
Merged
Changes from 6 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
124 changes: 120 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 @@ -33,7 +33,6 @@ PUT /my_index
}

----
// TESTSETUP

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

[[nested-top-level-params]]
==== Top-level parameters for `nested`
Expand All @@ -78,6 +78,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 @@ -114,4 +116,118 @@ 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

jrodewig marked this conversation as resolved.
Show resolved Hide resolved
[[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 the `model` field nested under `driver -> vehicle -> model`:

[source,console]
----
PUT /drivers
{
"mappings" : {
"properties" : {
"driver" : {
"type" : "nested",
"properties" : {
"vehicle" : {
"type" : "nested",
"properties" : {
"model" : {
"type" : "keyword"
}
}
}
}
}
}
}
}
----

Next, index a document to the `drivers` index.

[source,console]
----
PUT /drivers/_doc/1?refresh
{
"driver" : {
"vehicle" : {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think in order to demonstrate why nested field & query are needed here a more complex
example should be shown here. The document should contain multiple drivers and models and
two match queries on two different model fields. Otherwise an mapping with regular object fields
and just match queries would work too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your feedback @martijnvg.

I added some more detail to the example with 0eb74e5 and 2dedd58.

This should make the example a bit more realistic and add two match queries.

"model" : "Canyonero"
}
}
}
----
// TEST[continued]

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

[source,console]
----
GET /drivers/_search
{
"query" : {
"nested" : {
"path" : "driver",
"query" : {
"nested" : {
"path" : "driver.vehicle",
"query" : {
"match" : {
"driver.vehicle.model" : "Canyonero"
}
}
}
}
}
}
}
----
// TEST[continued]

The search request returns the following response:

[source,console-result]
----
{
"took" : 75,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.2876821,
"hits" : [
{
"_index" : "drivers",
"_id" : "1",
"_score" : 0.2876821,
"_source" : {
"driver" : {
"vehicle" : {
"model" : "Canyonero"
}
}
},
}
]
}
}
----
// TESTRESPONSE[s/"took" : 75/"took": $body.took/]