Skip to content

Commit 856e7bd

Browse files
[DOCS] Opster Migration: Nested bool query addition (#124455) (#125177)
added section related to nested bool queries to provide a more clear example and clean up surrounding language and grammatical issues Co-authored-by: Kofi B <[email protected]>
1 parent e1afe1d commit 856e7bd

File tree

1 file changed

+48
-2
lines changed

1 file changed

+48
-2
lines changed

docs/reference/query-languages/query-dsl/query-dsl-bool-query.md

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ A query that matches documents matching boolean combinations of other queries. T
1111

1212
| Occur | Description |
1313
| --- | --- |
14-
| `must` | The clause (query) must appear in matching documents and willcontribute to the score. Each query defined under a `must` acts as a logical "AND", returning only documents that match *all* the specified queries. |
14+
| `must` | The clause (query) must appear in matching documents and will contribute to the score. Each query defined under a `must` acts as a logical "AND", returning only documents that match *all* the specified queries. |
1515
| `should` | The clause (query) should appear in the matching document. Each query defined under a `should` acts as a logical "OR", returning documents that match *any* of the specified queries. |
1616
| `filter` | The clause (query) must appear in matching documents. However unlike`must` the score of the query will be ignored. Filter clauses are executedin [filter context](/reference/query-languages/query-dsl/query-filter-context.md), meaning that scoring is ignoredand clauses are considered for caching. Each query defined under a `filter` acts as a logical "AND", returning only documents that match *all* the specified queries. |
1717
| `must_not` | The clause (query) must not appear in the matchingdocuments. Clauses are executed in [filter context](/reference/query-languages/query-dsl/query-filter-context.md) meaningthat scoring is ignored and clauses are considered for caching. Because scoring isignored, a score of `0` for all documents is returned. Each query defined under a `must_not` acts as a logical "NOT", returning only documents that do not match any of the specified queries. |
1818

19-
The `must` and `should` clauses function as logical AND, OR operators, contributing to the scoring of results. However, these results will not be cached for faster retrieval. In contrast, the `filter` and `must_not` clauses are used to include or exclude results without impacting the score, unless used within a `constant_score` query.
19+
The `must` and `should` clauses function as logical AND, OR operators, contributing to the scoring of results. However, these results are not cached, which means repeated queries won't benefit from faster retrieval. In contrast, the `filter` and `must_not` clauses are used to include or exclude results without impacting the score, unless used within a `constant_score` query.
2020

2121
The `bool` query takes a *more-matches-is-better* approach, so the score from each matching `must` or `should` clause will be added together to provide the final `_score` for each document.
2222

@@ -55,6 +55,52 @@ If the `bool` query includes at least one `should` clause and no `must` or `filt
5555

5656
For other valid values, see the [`minimum_should_match` parameter](/reference/query-languages/query-dsl/query-dsl-minimum-should-match.md).
5757

58+
## Nested bool queries [nested-bool-queries]
59+
60+
You can nest `bool` queries within other `bool` queries to create complex logical constructs. This allows you to build sophisticated search conditions by combining multiple levels of boolean logic.
61+
62+
For example:
63+
64+
```console
65+
GET /_search
66+
{
67+
"query": {
68+
"bool": {
69+
"must": [ <1>
70+
{
71+
"bool": {
72+
"should": [
73+
{ "match": { "user.id": "kimchy" }},
74+
{ "match": { "user.id": "banon" }}
75+
]
76+
}
77+
},
78+
{ "match": { "tags": "production" }}
79+
],
80+
"should": [ <2>
81+
{
82+
"bool": {
83+
"must": [
84+
{ "match": { "status": "active" }},
85+
{ "match": { "title": "quick brown fox" }}
86+
]
87+
}
88+
}
89+
]
90+
}
91+
}
92+
}
93+
```
94+
1. Only documents that match all conditions in the must section will be returned in the results. This means documents must match either "kimchy" OR "banon" in the user.id field AND "production" in the tags field. It is semantically equivalent to (user.id="kimchy" OR user.id="banon") AND tags="production"
95+
96+
2. Matches in the `should` clauses are optional. They will only boost the relevance scores of documents that already match the required `must` criteria and don't add new documents to the result set. It is semantically equivalent to (status="active" AND title="quick brown fox")
97+
98+
You can use the `minimum_should_match` parameter to require matches from the `should` clauses.
99+
100+
::::{note}
101+
While nesting `bool` queries can be powerful, it can also lead to complex and slow queries. Try to keep your queries as flat as possible for the best performance.
102+
::::
103+
58104

59105
## Scoring with `bool.filter` [score-bool-filter]
60106

0 commit comments

Comments
 (0)