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

[8.0] [DOCS] Clarify nested query behavior for must_not clauses (#82727) #82734

Merged
merged 1 commit into from
Jan 18, 2022
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
183 changes: 183 additions & 0 deletions docs/reference/query-dsl/nested-query.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -279,3 +279,186 @@ The search request returns the following response:
}
----
// TESTRESPONSE[s/"took" : 5/"took": $body.took/]

[[must-not-clauses-and-nested-queries]]
===== `must_not` clauses and `nested` queries

If a `nested` query matches one or more nested objects in a document, it returns
the document as a hit. This applies even if other nested objects in the document
don't match the query. Keep this in mind when using a `nested` query that
contains an inner <<query-dsl-bool-query,`must_not` clause>>.

TIP: Use the <<inner-hits,`inner_hits`>> parameter to see which nested objects
matched a `nested` query.

For example, the following search uses an outer `nested` query with an inner
`must_not` clause.

[source,console]
----
PUT my-index
{
"mappings": {
"properties": {
"comments": {
"type": "nested"
}
}
}
}

PUT my-index/_doc/1?refresh
{
"comments": [
{
"author": "kimchy"
}
]
}

PUT my-index/_doc/2?refresh
{
"comments": [
{
"author": "kimchy"
},
{
"author": "nik9000"
}
]
}

PUT my-index/_doc/3?refresh
{
"comments": [
{
"author": "nik9000"
}
]
}

POST my-index/_search
{
"query": {
"nested": {
"path": "comments",
"query": {
"bool": {
"must_not": [
{
"term": {
"comments.author": "nik9000"
}
}
]
}
}
}
}
}
----
// TEST[s/_search/_search?filter_path=hits.hits/]

The search returns:

[source,console]
----
{
...
"hits" : {
...
"hits" : [
{
"_index" : "my-index",
"_id" : "1",
"_score" : 0.0,
"_source" : {
"comments" : [
{
"author" : "kimchy"
}
]
}
},
{
"_index" : "my-index",
"_id" : "2",
"_score" : 0.0,
"_source" : {
"comments" : [
{
"author" : "kimchy" <1>
},
{
"author" : "nik9000" <2>
}
]
}
}
]
}
}
----
// TESTRESPONSE[s/\.\.\.//]

<1> This nested object matches the query. As a result, the search returns the
object's parent document as a hit.

<2> This nested object doesn't match the query. Since another nested object in
the same document does match the query, the search still returns the parent
document as a hit.

To exclude documents with any nested objects that match the `nested` query,
use an outer `must_not` clause.

[source,console]
----
POST my-index/_search
{
"query": {
"bool": {
"must_not": [
{
"nested": {
"path": "comments",
"query": {
"term": {
"comments.author": "nik9000"
}
}
}
}
]
}
}
}
----
// TEST[continued]
// TEST[s/_search/_search?filter_path=hits.hits/]

The search returns:

[source,console]
----
{
...
"hits" : {
...
"hits" : [
{
"_index" : "my-index",
"_id" : "1",
"_score" : 0.0,
"_source" : {
"comments" : [
{
"author" : "kimchy"
}
]
}
}
]
}
}
----
// TESTRESPONSE[s/\.\.\.//]