Skip to content

Commit

Permalink
Rewrite newSlowRangeQuery to MatchNoDocsQuery when upper > lower (#13425
Browse files Browse the repository at this point in the history
)
  • Loading branch information
ioanatia authored and jpountz committed Jun 4, 2024
1 parent e8801bf commit 2ed1f2f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lucene/CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
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;
Expand Down Expand Up @@ -87,6 +88,9 @@ public Query rewrite(IndexSearcher indexSearcher) throws IOException {
if (lowerValue == Long.MIN_VALUE && upperValue == Long.MAX_VALUE) {
return new FieldExistsQuery(field);
}
if (lowerValue > upperValue) {
return new MatchNoDocsQuery();
}
return super.rewrite(indexSearcher);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,24 @@ public void testMissingField() throws IOException {
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);
Expand Down

0 comments on commit 2ed1f2f

Please sign in to comment.