Rewrite newSlowRangeQuery to MatchNoDocsQuery when upper > lower (#13425)

This commit is contained in:
Ioana Tagirta 2024-06-04 18:38:50 +02:00 committed by GitHub
parent 801b822972
commit e868b82045
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 25 additions and 0 deletions

View File

@ -252,6 +252,9 @@ Optimizations
* GITHUB#13439: Avoid unnecessary memory allocation in PackedLongValues#Iterator. (Zhang Chao)
* GITHUB##13425: Rewrite SortedNumericDocValuesRangeQuery to MatchNoDocsQuery when the upper bound is smaller than the
lower bound. (Ioana Tagirta)
Bug Fixes
---------------------
(No changes)

View File

@ -26,6 +26,7 @@ import org.apache.lucene.search.ConstantScoreScorer;
import org.apache.lucene.search.ConstantScoreWeight;
import org.apache.lucene.search.FieldExistsQuery;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.MatchNoDocsQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.QueryVisitor;
import org.apache.lucene.search.ScoreMode;
@ -87,6 +88,9 @@ final class SortedNumericDocValuesRangeQuery extends Query {
if (lowerValue == Long.MIN_VALUE && upperValue == Long.MAX_VALUE) {
return new FieldExistsQuery(field);
}
if (lowerValue > upperValue) {
return new MatchNoDocsQuery();
}
return super.rewrite(indexSearcher);
}

View File

@ -288,6 +288,24 @@ public class TestDocValuesQueries extends LuceneTestCase {
dir.close();
}
public void testSlowRangeQueryRewrite() throws IOException {
Directory dir = newDirectory();
RandomIndexWriter iw = new RandomIndexWriter(random(), dir);
IndexReader reader = iw.getReader();
iw.close();
IndexSearcher searcher = newSearcher(reader);
QueryUtils.checkEqual(
NumericDocValuesField.newSlowRangeQuery("foo", 10, 1).rewrite(searcher),
new MatchNoDocsQuery());
QueryUtils.checkEqual(
NumericDocValuesField.newSlowRangeQuery("foo", Long.MIN_VALUE, Long.MAX_VALUE)
.rewrite(searcher),
new FieldExistsQuery("foo"));
reader.close();
dir.close();
}
public void testSortedNumericNPE() throws IOException {
Directory dir = newDirectory();
RandomIndexWriter iw = new RandomIndexWriter(random(), dir);