Why IDs query expands to terms query?

So i was poking around sources and looks like ids query doesn't do much by itself:

    protected Query doToQuery(SearchExecutionContext context) throws IOException {
        MappedFieldType idField = context.getFieldType(IdFieldMapper.NAME);
        if (idField == null || ids.isEmpty()) {
            throw new IllegalStateException("Rewrite first");
        return idField.termsQuery(new ArrayList<>(ids), context);

The thing is - looking form a veeeeery high level - the termsQuery itself will expand into N term queries, every one will look through whole the index to find only one specific document (well, segments may have several versions, but that's just even more work for collapsing), then merge it all together, nasty stuff. At the same time, ES surely knows document id -> lucene id mapping, which allows to create necessary bitsets on the fly just by looking at this mapping and adding necessary bits (which should be really cheap on roaring bitmaps that i've heard being used in Lucene), completely eradicating necessity to look into index at all (at the expense of possibly reading the mapping from disk). The latter approach seems less cost-wisely to me, so here's the question: why it's implemented this way?

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.