From d8397c1089e40c30ade58e142fd67a9b5864635e Mon Sep 17 00:00:00 2001 From: Skyring100 <106502383+Skyring100@users.noreply.github.com> Date: Thu, 20 Jun 2024 07:29:16 -0700 Subject: [PATCH] Optimize UnsignedLong range queries to convert to MatchNoDocsQuery when lower > upper bounds (#14416) * Added check for lower > upper at end of function Signed-off-by: Skyring100 <106502383+Skyring100@users.noreply.github.com> * Fixed mistake of using < operator on BigInteger, now using compareTo Signed-off-by: Skyring100 <106502383+Skyring100@users.noreply.github.com> * Fixed simple mistake of flipping > operator Signed-off-by: Skyring100 <106502383+Skyring100@users.noreply.github.com> * Fixed space formatting Signed-off-by: Skyring100 <106502383+Skyring100@users.noreply.github.com> * Updated CHANGELOG.md Signed-off-by: Skyring100 <106502383+Skyring100@users.noreply.github.com> * Issue number linked in CHANGELOG.md Signed-off-by: Skyring100 <106502383+Skyring100@users.noreply.github.com> * doTestDocValueRangeQueries now accepts MatchNoDocsQuery alongside IndexOrDocValuesQuery Signed-off-by: Skyring100 <106502383+Skyring100@users.noreply.github.com> * dotestdoTestDocValueRangeQueries only checks indexQuery and randomAccessQuery only when query is type IndexIndexOrDocValuesQuery Signed-off-by: Skyring100 <106502383+Skyring100@users.noreply.github.com> * Ran gradlew spotlessApply to fix import formatting issues Signed-off-by: Skyring100 <106502383+Skyring100@users.noreply.github.com> * Imported Matchers.either method instead of entire Matchers class Signed-off-by: Skyring100 <106502383+Skyring100@users.noreply.github.com> --------- Signed-off-by: Skyring100 <106502383+Skyring100@users.noreply.github.com> Signed-off-by: Andriy Redko Co-authored-by: Andriy Redko --- .../org/opensearch/index/mapper/NumberFieldMapper.java | 3 +++ .../opensearch/index/mapper/NumberFieldTypeTests.java | 9 ++++++--- 2 files changed, 9 insertions(+), 3 deletions(-) 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 25e5f9970795f..27e62c3746a8e 100644 --- a/server/src/main/java/org/opensearch/index/mapper/NumberFieldMapper.java +++ b/server/src/main/java/org/opensearch/index/mapper/NumberFieldMapper.java @@ -1410,6 +1410,9 @@ public static Query unsignedLongRangeQuery( u = u.subtract(BigInteger.ONE); } } + if (l.compareTo(u) > 0) { + return new MatchNoDocsQuery(); + } return builder.apply(l, u); } } diff --git a/server/src/test/java/org/opensearch/index/mapper/NumberFieldTypeTests.java b/server/src/test/java/org/opensearch/index/mapper/NumberFieldTypeTests.java index af852b12e7a30..96487db6dd512 100644 --- a/server/src/test/java/org/opensearch/index/mapper/NumberFieldTypeTests.java +++ b/server/src/test/java/org/opensearch/index/mapper/NumberFieldTypeTests.java @@ -86,6 +86,7 @@ import java.util.function.Supplier; import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.either; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.instanceOf; @@ -673,9 +674,11 @@ public void doTestDocValueRangeQueries(NumberType type, Supplier valueSu true, MOCK_QSC ); - assertThat(query, instanceOf(IndexOrDocValuesQuery.class)); - IndexOrDocValuesQuery indexOrDvQuery = (IndexOrDocValuesQuery) query; - assertEquals(searcher.count(indexOrDvQuery.getIndexQuery()), searcher.count(indexOrDvQuery.getRandomAccessQuery())); + assertThat(query, either(instanceOf(IndexOrDocValuesQuery.class)).or(instanceOf(MatchNoDocsQuery.class))); + if (query instanceof IndexOrDocValuesQuery) { + IndexOrDocValuesQuery indexOrDvQuery = (IndexOrDocValuesQuery) query; + assertEquals(searcher.count(indexOrDvQuery.getIndexQuery()), searcher.count(indexOrDvQuery.getRandomAccessQuery())); + } } reader.close(); dir.close();