From 98ae9cee4dc25367c138aaece7d92aac650b4eab 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 (cherry picked from commit d2c08b320b49920e142ec70fa66621305fd4ccf8) Signed-off-by: Andriy Redko --- CHANGELOG.md | 1 + .../org/opensearch/index/mapper/NumberFieldMapper.java | 3 +++ .../opensearch/index/mapper/NumberFieldTypeTests.java | 9 ++++++--- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d0a3a6dea671a..c3e9a29df8f5f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), - Bump `opentelemetry` from 1.36.0 to 1.39.0 ([#14457](https://github.com/opensearch-project/OpenSearch/pull/14457)) ### Changed +- unsignedLongRangeQuery now returns MatchNoDocsQuery if the lower bounds are greater than the upper bounds ([#14416](https://github.com/opensearch-project/OpenSearch/pull/14416)) - Make the class CommunityIdProcessor final ([#14448](https://github.com/opensearch-project/OpenSearch/pull/14448)) - Updated the `indices.query.bool.max_clause_count` setting from being static to dynamically updateable ([#13568](https://github.com/opensearch-project/OpenSearch/pull/13568)) 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();