What is the rough underlying implementation for Bool MUST in ElasticSearch?

Viewed 30

In ElasticSearch, what's the rough implementation for an AND-style boolean query where the fields are term types? Does ElasticSearch run filter queries separately on each of the field, and then find their intersections?

For example, if I have something like

{
  "query": {
    "bool" : {
      "must" : {
        "term" : { "first_name": "John" },
        "term" : { "dob": "01-01-1990"}
    }
  }
}

does it first get all documents with "first_name": John", all documents with "dob": "01-01-1990", and then find the intersecting documents from each of the results? If that's the case does it imply that these kinds of queries can potentially be extremely slow, since the matching results for each terms individually can be extremely large and thus finding the intersection would be slow?

1 Answers

That kind of queries are extremely fast. Moreover, you should use bool/filter instead of bool/must as that will leverage filter caches and reuse existing filters to run the subsequent queries even faster.

You should go through this article which explains all about how filter bitsets are working. The first article has been posted a few years ago, but the logic underneath is still pretty much the same in recent versions.

Also here is another article worth looking at.

Related