Issues With Filter Clause Inside Bool Query
See original GitHub issueAs far a I can see, since ElasticSearch 2.x and above - there shouldn’t be any difference between the structure of “Must” and “Filter” clauses inside bool queries. However, there is a difference in bodybuilder. I have added an explanation below.
I believe the issues are leftovers from previous bodybuilder versions supporting ElasticSearch 1.x. What do you think?
MUST
Single must:
bodybuilder()
.query('term','user','Idan')
.build()
result as expected by bodybuilder:
{
"query": {
"term": {
"user": "Idan"
}
}
}
Multiple musts:
bodybuilder()
.query('term','user','Idan')
.query('term','level','INFO')
.build()
result as expected by bodybuilder. An ordinary array.
{
"query": {
"bool": {
"must": [
{
"term": {
"user": "Idan"
}
},
{
"term": {
"level": "INFO"
}
}
]
}
}
}
FILTER
Single filter:
bodybuilder()
.filter('term','user','Idan')
.build()
Result as expected by bodybuilder. It might look unnecessary to wrap the clause with bool since it is only a single clause, however this way will make sure no score will be calculated (“Filter context is in effect whenever a query clause is passed to a filter parameter, such as the filter or must_not parameters in the bool query, the filter parameter in the constant_score query, or the filter aggregation”).
{
"query": {
"bool": {
"filter": {
"term": {
"user": "Idan"
}
}
}
}
}
Multiple filters:
bodybuilder()
.filter('term','user','Idan')
.filter('term','level','INFO')
.build()
Unexpected result by bodybuilder. Why wrapping the clauses with bool and must, instead of an ordinary array?
{
"query": {
"bool": {
"filter": {
"bool": {
"must": [
{
"term": {
"user": "Idan"
}
},
{
"term": {
"level": "INFO"
}
}
]
}
}
}
}
}
Issue Analytics
- State:
- Created 7 years ago
- Comments:16 (2 by maintainers)
Top GitHub Comments
To elaborate, consider this:
Which produces this:
I’m not wondering why that JS turns two filter calls into a separate
bool
query – that makes sense if we need to chain it with other clauses at that first level – I’m wondering why that newbool
query uses amust
instead of afilter
clause. Why doesn’t the above JS produce this instead?I understand the elasticsearch docs so that the query is executed in filter context once filter context is initiated https://www.elastic.co/guide/en/elasticsearch/reference/current/query-filter-context.html
In other words: once you’re in filter context, you can’t execute a a query in query context anymore.
Do you have any example or docs that show that those two are different? In that case is be happy to reopen and address this asap