Closed
Description
It's probably more easily explained with an example: TermQueryBuilber#doToQuery
looks like this today
protected Query doToQuery(QueryShardContext context) throws IOException {
Query query = null;
MappedFieldType mapper = context.fieldMapper(this.fieldName);
if (mapper != null) {
query = mapper.termQuery(this.value, context);
}
if (query == null) {
query = new TermQuery(new Term(this.fieldName, BytesRefs.toBytesRef(this.value)));
}
return query;
}
So if the field is unmapped, we create a TermQuery on a field that doesn't exist, which is no different from returning a MatchNoDocsQuery.
It's becoming more and more common that multiple indices that have different mappings get queried together, e.g. as part of an alias or an index pattern, so a more efficient way to handle these cases would be to rewrite to a MatchNoDocsQueryBuilder
in the rewrite()
method. This would help figure out that the shard cannot possibly match in the can_match
phase and skip the query
phase completely.