Home > Enterprise >  Does bool query exist execution order?
Does bool query exist execution order?

Time:11-27

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
    }
  }
}

CodePudding user response:

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.

CodePudding user response:

There is a blog post on Elasticsearch official website exactly about this topic. I don't know if it still holds true since the post was made in 2017. Of course, you can read it to have the better understanding, but for a quick summary, it says

there is no simple answer to "which query runs first"!

We also think that filter should be executed before queries to reduce the document set, but the blog claims that

Everything is interleaved, regardless of whether they are queries of filters.

It is really difficult to know which queries/filters are executed first. This internal information is not exposed. There is one way which might help is to look into the Profile API, as suggested by the blog.

  • Related