LUCENE-10435: Break loop early while checking whether DocValuesFieldExistsQuery can be rewrite to MatchAllDocsQuery (#701)

This commit is contained in:
Lu Xugang 2022-02-23 20:53:56 +08:00 committed by GitHub
parent ab47db4fee
commit 43e89d6a29
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 6 additions and 5 deletions

View File

@ -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);