Repository navigation
[JAY-796] Add intersection support to QueryBuilder - #83
Open
sergio-bobillier wants to merge 1 commit into
Open
sergio-bobillier wants to merge 1 commit into
sergio-bobillier wants to merge 1 commit into
Conversation
sergio-bobillier
force-pushed
the
sb-JAY-796-query-intersect
branch
from
October 9, 2026 15:29
1758c58 to
41e143f
Compare
sergio-bobillier
force-pushed
the
sb-JAY-796-query-intersect
branch
from
October 27, 2026 11:59
41e143f to
08df482
Compare
Zauze
reviewed
Oct 27, 2026
| end | ||
| end | ||
|
|
||
| describe '#intersection' do |
Collaborator
There was a problem hiding this comment.
Would be great to have some test cases to test union and intersection operations combined in a more sophisticated AND/OR clause
Collaborator
Author
There was a problem hiding this comment.
Thanks. I have added an integration test with such a case, it also servers as an illustration of one of those cases when one might want an explicit intersection instead of a regular merge.
Zauze
reviewed
Oct 27, 2026
Zauze
requested changes
Oct 27, 2026
Add QueryBuilder#intersection to combine two queries with a boolean
'must' clause, equivalent to AND, while retaining the existing merge
behavior for other builder options. Cover empty and non-empty query
combinations, and document the method.
This behavior is desired when combining query clauses that involve one
or more 'should' subclauses. Combining the following two queries with
the #merge method:
{
"bool": {
"must": [
{ "range": { "timestamp": { "gte": "now-1d/d" } } }
]
}
}
{
"bool": {
"should": [
{ "term": { "field": "value" } },
{ "term": { "another_field": "another_value" } }
]
}
}
The resulting query is:
{
"bool": {
"must": [
{ "range": { "timestamp": { "gte": "now-1d/d" } } }
],
"should": [
{ "term": { "field": "value" } },
{ "term": { "another_field": "another_value" } }
]
}
}
Because of the way 'should' behaves when paired with 'must', the results
produced by this query might not be what one expects. From the
Elasticsearch documentation:
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.
So, in the above example, the query will basically match all documents
whose timestamp is within the last day. If they happen to match the
'should' clauses, it will boost their relevance scores. However, one
might actually want to get only the documents that are within the last
day and also match either of the clauses inside 'should'.
For that, the QueryBuilder#intersection method can be used to combine
the queries with a 'must' clause, ensuring that only documents matching
both conditions are returned. The resulting query from doing that would
be:
{
"bool": {
"must": [
{
"bool": {
"must": [
{ "range": { "timestamp": { "gte": "now-1d/d" } } }
]
}
},
{
"bool": {
"should": [
{ "term": { "field": "value" } },
{ "term": { "another_field": "another_value" } }
]
}
}
]
}
}
Which produces the intended result set.
sergio-bobillier
force-pushed
the
sb-JAY-796-query-intersect
branch
from
October 27, 2026 15:21
08df482 to
2a4bb8a
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add
QueryBuilder#intersectionto combine two queries with a booleanmustclause, equivalent toAND, while retaining the existing merge behavior for other builder options. Cover empty and non-empty query combinations, and document the method.This behavior is desired when combining query clauses that involve one or more
shouldsubclauses. Combining the following two queries with the#mergemethod:{ "bool": { "must": [ { "range": { "timestamp": { "gte": "now-1d/d" } } } ] } }{ "bool": { "should": [ { "term": { "field": "value" } }, { "term": { "another_field": "another_value" } } ] } }The resulting query is:
{ "bool": { "must": [ { "range": { "timestamp": { "gte": "now-1d/d" } } } ], "should": [ { "term": { "field": "value" } }, { "term": { "another_field": "another_value" } } ] } }Because of the way
shouldbehaves when paired withmust, the results produced by this query might not be what one expects. From the Elasticsearch documentation:So, in the above example, the query will basically match all documents whose timestamp is within the last day. If they happen to match the
shouldclauses, it will boost their relevance scores. However, one might actually want to get only the documents that are within the last day and also match either of the clauses insideshould.For that, the
QueryBuilder#intersectionmethod can be used to combine the queries with amustclause, ensuring that only documents matching both conditions are returned. The resulting query from doing that would be:{ "bool": { "must": [ { "bool": { "must": [ { "range": { "timestamp": { "gte": "now-1d/d" } } } ] } }, { "bool": { "should": [ { "term": { "field": "value" } }, { "term": { "another_field": "another_value" } } ] } } ] } }Which produces the intended result set.