diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt index b89aa865a81..ad7163b20d2 100644 --- a/lucene/CHANGES.txt +++ b/lucene/CHANGES.txt @@ -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) diff --git a/lucene/core/src/java/org/apache/lucene/document/SortedNumericDocValuesRangeQuery.java b/lucene/core/src/java/org/apache/lucene/document/SortedNumericDocValuesRangeQuery.java index 93232771698..424f9473fa5 100644 --- a/lucene/core/src/java/org/apache/lucene/document/SortedNumericDocValuesRangeQuery.java +++ b/lucene/core/src/java/org/apache/lucene/document/SortedNumericDocValuesRangeQuery.java @@ -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); } diff --git a/lucene/core/src/test/org/apache/lucene/search/TestDocValuesQueries.java b/lucene/core/src/test/org/apache/lucene/search/TestDocValuesQueries.java index a54be70360e..c864fa0d98c 100644 --- a/lucene/core/src/test/org/apache/lucene/search/TestDocValuesQueries.java +++ b/lucene/core/src/test/org/apache/lucene/search/TestDocValuesQueries.java @@ -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);