diff --git a/lucene/core/src/java/org/apache/lucene/search/DocValuesFieldExistsQuery.java b/lucene/core/src/java/org/apache/lucene/search/DocValuesFieldExistsQuery.java index c2939ea1708..9c39a33b4ff 100644 --- a/lucene/core/src/java/org/apache/lucene/search/DocValuesFieldExistsQuery.java +++ b/lucene/core/src/java/org/apache/lucene/search/DocValuesFieldExistsQuery.java @@ -69,17 +69,18 @@ public final class DocValuesFieldExistsQuery extends Query { @Override public Query rewrite(IndexReader reader) throws IOException { - int rewritableReaders = 0; + boolean allReadersRewritable = true; for (LeafReaderContext context : reader.leaves()) { LeafReader leaf = context.reader(); Terms terms = leaf.terms(field); PointValues pointValues = leaf.getPointValues(field); - if ((terms != null && terms.getDocCount() == leaf.maxDoc()) - || (pointValues != null && pointValues.getDocCount() == leaf.maxDoc())) { - rewritableReaders++; + if ((terms == null || terms.getDocCount() != leaf.maxDoc()) + && (pointValues == null || pointValues.getDocCount() != leaf.maxDoc())) { + allReadersRewritable = false; + break; } } - if (rewritableReaders == reader.leaves().size()) { + if (allReadersRewritable) { return new MatchAllDocsQuery(); } return super.rewrite(reader);