I want to know the execution order of bool query
Does filter first then must ?
OST _search
{
"query": {
"bool" : {
"must" : {
"term" : { "user.id" : "kimchy" }
},
"filter": {
"term" : { "tags" : "production" }
},
"must_not" : {
"range" : {
"age" : { "gte" : 10, "lte" : 20 }
}
},
"should" : [
{ "term" : { "tags" : "env1" } },
{ "term" : { "tags" : "deployed" } }
],
"minimum_should_match" : 1,
"boost" : 1.0
}
}
}
>Solution :
In your case, the term query on user.id should go into the filter array, because it’s an exact match query which doesn’t require scoring, hence must doesn’t make sense. must should be used for free-text queries that require scoring, not exact yes/no queries which don’t.
But to answer your question, yes, filter and must_not (what we call the filter context) are executed first in order to reduce the document set to the smallest possible set, and then must and should queries (what we call the query context) are run on that document set.