diff --git a/server/src/main/java/org/opensearch/index/mapper/NumberFieldMapper.java b/server/src/main/java/org/opensearch/index/mapper/NumberFieldMapper.java index 0b62e17e6a020..9286b5c64b5f2 100644 --- a/server/src/main/java/org/opensearch/index/mapper/NumberFieldMapper.java +++ b/server/src/main/java/org/opensearch/index/mapper/NumberFieldMapper.java @@ -46,6 +46,7 @@ import org.apache.lucene.sandbox.document.HalfFloatPoint; import org.apache.lucene.search.BoostQuery; import org.apache.lucene.search.IndexOrDocValuesQuery; +import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.IndexSortSortedNumericDocValuesRangeQuery; import org.apache.lucene.search.MatchNoDocsQuery; import org.apache.lucene.search.PointInSetQuery; @@ -1463,6 +1464,14 @@ public BytesRef next() { return encoded; } }) { + @Override + public Query rewrite(IndexSearcher indexSearcher) throws IOException { + if (bitmap.isEmpty()) { + return new MatchNoDocsQuery(); + } + return super.rewrite(indexSearcher); + } + @Override protected String toString(byte[] value) { assert value.length == Integer.BYTES; diff --git a/server/src/main/java/org/opensearch/search/query/BitmapDocValuesQuery.java b/server/src/main/java/org/opensearch/search/query/BitmapDocValuesQuery.java index e09a724c8486a..dfa5fc4567f80 100644 --- a/server/src/main/java/org/opensearch/search/query/BitmapDocValuesQuery.java +++ b/server/src/main/java/org/opensearch/search/query/BitmapDocValuesQuery.java @@ -45,8 +45,13 @@ public class BitmapDocValuesQuery extends Query implements Accountable { public BitmapDocValuesQuery(String field, RoaringBitmap bitmap) { this.field = field; this.bitmap = bitmap; - min = bitmap.first(); - max = bitmap.last(); + if (!bitmap.isEmpty()) { + min = bitmap.first(); + max = bitmap.last(); + } else { + min = 0; // final field + max = 0; + } } @Override @@ -112,7 +117,7 @@ public String toString(String field) { @Override public Query rewrite(IndexSearcher indexSearcher) throws IOException { - if (bitmap.getLongCardinality() == 0) { + if (bitmap.isEmpty()) { return new MatchNoDocsQuery(); } return super.rewrite(indexSearcher);