Following this question: bool query - must and should on same level, what does it means?
I wonder how can I achieve a simple logical query in elasticsearch using the bool
query:
(A & B) | C
where A
, B
, and C
are all match queries needed to be combined with bool
.
If I understand correctly, the should
clause in the bool
query only makes those results' scores be higher. If that's the case, how can I achieve an "OR" relation?
Let's say A, B, and C are all number ranges so something like (pseudo code):
(x >= 4 & x<=6) | x=2
From this, I expect to get the potential result set of 2,4,5,6
(="give me all numbers between 4 and 6 inclusive, but 2 is also fine") - but this is not the case since if "&" means "must" - then the "and" and the "or" parts contradict each other (that's what I understood from people who know elasticsearch more than I do).
So maybe I do not understand how the elasticsearch boolean query works, I don't know. A working example that can achieve the above case will be highly appreciated as well.
For example, the following does not work and yields the result set of 4,5,6
only:
{'query': {'bool': {'should': [{'bool': {'must': [{'range': {'x': {'gte': 4}}}, {'range': {'x': {'lte': 6}}}]}}, {'match': {'x': 2}}]}}}