How does elasticsearch fetch AND operator query from its indexes
First some basics: ElasticSearch uses lucene behind the scenes. In lucene a query returns a scorer, and that scorer is responsible for returning the list of documents matching the query.
Your boolean query will internally be translated to lucene BooleanQuery
which in this case will return ConjunctionScorer
, as it has only must
clauses.
Each of the clauses is a TermQuery
that returns a TermScorer
which, when advanced, gives next matching document in increasing order of document id.
ConjunctionScorer
computes intersection of the matching documents returned by scorers for each clause by simply advancing each scorer in turns.
So you can think of TermScorer
as of one returning an ordered list of the documents, and of ConjunctionScorer
as of one simply intersecting two ordered lists.
There's not much you can do to optimize it. Maybe, since you're not really interested in scores, you could use a filter query instead and let ElasticSearch cache it.