-
Notifications
You must be signed in to change notification settings - Fork 2.5k
BooleanQuery rewrite for must_not RangeQuery clauses #17655
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
msfroh
merged 14 commits into
opensearch-project:main
from
peteralfonsi:must-not-range-rewrite
Jun 4, 2025
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
bf77918
Add query rewrite for boolean queries with must_not range queries
6b67b4a
add ITs
6dcb6e0
Merge branch 'main' into must-not-range-rewrite
d9eee10
changelog
25367bb
Fix forbiddenApis
24a6561
Adds case for fields that have != 1 values per doc
6887d9e
Merge branch 'main' into must-not-range-rewrite
bb3243d
fix UTs
fc49ddd
fix >1 value IT
4b00d36
Address LeafReaderContext comment
feed2e8
Merge branch 'main' into must-not-range-rewrite
peteralfonsi c273159
spotless apply
c4d29f2
Merge branch 'main' into must-not-range-rewrite
peteralfonsi 58d9a45
rerun gradle
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
244 changes: 244 additions & 0 deletions
244
server/src/internalClusterTest/java/org/opensearch/search/query/BooleanQueryIT.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,244 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * The OpenSearch Contributors require contributions made to | ||
| * this file be licensed under the Apache-2.0 license or a | ||
| * compatible open source license. | ||
| */ | ||
|
|
||
| package org.opensearch.search.query; | ||
|
|
||
| import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; | ||
|
|
||
| import org.opensearch.common.settings.Settings; | ||
| import org.opensearch.test.ParameterizedStaticSettingsOpenSearchIntegTestCase; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.Collection; | ||
|
|
||
| import static org.opensearch.index.query.QueryBuilders.boolQuery; | ||
| import static org.opensearch.index.query.QueryBuilders.matchAllQuery; | ||
| import static org.opensearch.index.query.QueryBuilders.rangeQuery; | ||
| import static org.opensearch.index.query.QueryBuilders.termQuery; | ||
| import static org.opensearch.search.SearchService.CLUSTER_CONCURRENT_SEGMENT_SEARCH_SETTING; | ||
| import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertHitCount; | ||
|
|
||
| public class BooleanQueryIT extends ParameterizedStaticSettingsOpenSearchIntegTestCase { | ||
|
|
||
| public BooleanQueryIT(Settings staticSettings) { | ||
| super(staticSettings); | ||
| } | ||
|
|
||
| @ParametersFactory | ||
| public static Collection<Object[]> parameters() { | ||
| return Arrays.asList( | ||
| new Object[] { Settings.builder().put(CLUSTER_CONCURRENT_SEGMENT_SEARCH_SETTING.getKey(), false).build() }, | ||
| new Object[] { Settings.builder().put(CLUSTER_CONCURRENT_SEGMENT_SEARCH_SETTING.getKey(), true).build() } | ||
| ); | ||
| } | ||
|
|
||
| public void testMustNotRangeRewrite() throws Exception { | ||
| String intField = "int_field"; | ||
| String termField1 = "term_field_1"; | ||
| String termField2 = "term_field_2"; | ||
| // If we don't explicitly set this mapping, term_field_2 is not correctly mapped as a keyword field and queries don't work as | ||
| // expected | ||
| createIndex( | ||
| "test", | ||
| Settings.EMPTY, | ||
| "{\"properties\":{\"int_field\":{\"type\": \"integer\"},\"term_field_1\":{\"type\": \"keyword\"},\"term_field_2\":{\"type\": \"keyword\"}}}}" | ||
| ); | ||
| int numDocs = 100; | ||
|
|
||
| for (int i = 0; i < numDocs; i++) { | ||
| String termValue1 = "odd"; | ||
| String termValue2 = "A"; | ||
| if (i % 2 == 0) { | ||
| termValue1 = "even"; | ||
| } | ||
| if (i >= numDocs / 2) { | ||
| termValue2 = "B"; | ||
| } | ||
| client().prepareIndex("test") | ||
| .setId(Integer.toString(i)) | ||
| .setSource(intField, i, termField1, termValue1, termField2, termValue2) | ||
| .get(); | ||
| } | ||
| ensureGreen(); | ||
| waitForRelocation(); | ||
| forceMerge(); | ||
| refresh(); | ||
|
|
||
| int lt = 80; | ||
| int gte = 20; | ||
| int expectedHitCount = numDocs - (lt - gte); | ||
|
|
||
| assertHitCount( | ||
| client().prepareSearch().setQuery(boolQuery().mustNot(rangeQuery(intField).lt(lt).gte(gte))).get(), | ||
| expectedHitCount | ||
| ); | ||
|
|
||
| assertHitCount( | ||
| client().prepareSearch() | ||
| .setQuery(boolQuery().must(termQuery(termField1, "even")).mustNot(rangeQuery(intField).lt(lt).gte(gte))) | ||
| .get(), | ||
| expectedHitCount / 2 | ||
| ); | ||
|
|
||
| // Test with preexisting should fields, to ensure the original default minimumShouldMatch is respected | ||
| // once we add a must clause during rewrite, even if minimumShouldMatch is not explicitly set | ||
| assertHitCount( | ||
| client().prepareSearch().setQuery(boolQuery().should(termQuery(termField1, "even")).should(termQuery(termField2, "B"))).get(), | ||
| 3 * numDocs / 4 | ||
| ); | ||
| assertHitCount( | ||
| client().prepareSearch() | ||
| .setQuery( | ||
| boolQuery().should(termQuery(termField1, "even")) | ||
| .should(termQuery(termField2, "A")) | ||
| .mustNot(rangeQuery(intField).lt(lt).gte(gte)) | ||
| ) | ||
| .get(), | ||
| 3 * expectedHitCount / 4 | ||
| ); | ||
|
|
||
| assertHitCount(client().prepareSearch().setQuery(boolQuery().mustNot(rangeQuery(intField).lt(numDocs * 2))).get(), 0); | ||
| } | ||
|
|
||
| public void testMustNotDateRangeRewrite() throws Exception { | ||
| int minDay = 10; | ||
| int maxDay = 31; | ||
| String dateField = "date_field"; | ||
| createIndex("test"); | ||
| for (int day = minDay; day <= maxDay; day++) { | ||
| client().prepareIndex("test").setSource(dateField, getDate(day, 0)).get(); | ||
| } | ||
| ensureGreen(); | ||
| waitForRelocation(); | ||
| forceMerge(); | ||
| refresh(); | ||
|
|
||
| int minExcludedDay = 15; | ||
| int maxExcludedDay = 25; | ||
| int expectedHitCount = maxDay + 1 - minDay - (maxExcludedDay - minExcludedDay + 1); | ||
|
|
||
| assertHitCount( | ||
| client().prepareSearch("test") | ||
| .setQuery(boolQuery().mustNot(rangeQuery(dateField).gte(getDate(minExcludedDay, 0)).lte(getDate(maxExcludedDay, 0)))) | ||
| .get(), | ||
| expectedHitCount | ||
| ); | ||
| } | ||
|
|
||
| public void testMustNotDateRangeWithFormatAndTimeZoneRewrite() throws Exception { | ||
| // Index a document for each hour of 1/1 in UTC. Then, do a boolean query excluding 1/1 for UTC+3. | ||
| // If the time zone is respected by the rewrite, we should see 3 matching documents, | ||
| // as there will be 3 hours that are 1/1 in UTC but not UTC+3. | ||
|
|
||
| String dateField = "date_field"; | ||
| createIndex("test"); | ||
| String format = "strict_date_hour_minute_second_fraction"; | ||
|
|
||
| for (int hour = 0; hour < 24; hour++) { | ||
| client().prepareIndex("test").setSource(dateField, getDate(1, hour)).get(); | ||
| } | ||
| ensureGreen(); | ||
| waitForRelocation(); | ||
| forceMerge(); | ||
| refresh(); | ||
|
|
||
| int zoneOffset = 3; | ||
| assertHitCount( | ||
| client().prepareSearch("test") | ||
| .setQuery( | ||
| boolQuery().mustNot( | ||
| rangeQuery(dateField).gte(getDate(1, 0)).lte(getDate(1, 23)).timeZone("+" + zoneOffset).format(format) | ||
| ) | ||
| ) | ||
| .get(), | ||
| zoneOffset | ||
| ); | ||
| } | ||
|
|
||
| public void testMustNotRangeRewriteWithMissingValues() throws Exception { | ||
| // This test passes because the rewrite is skipped when not all docs have exactly 1 value | ||
| String intField = "int_field"; | ||
| String termField = "term_field"; | ||
| createIndex("test"); | ||
| int numDocs = 100; | ||
| int numDocsMissingIntValues = 20; | ||
|
|
||
| for (int i = 0; i < numDocs; i++) { | ||
| String termValue = "odd"; | ||
| if (i % 2 == 0) { | ||
| termValue = "even"; | ||
| } | ||
|
|
||
| if (i >= numDocsMissingIntValues) { | ||
| client().prepareIndex("test").setId(Integer.toString(i)).setSource(intField, i, termField, termValue).get(); | ||
| } else { | ||
| client().prepareIndex("test").setId(Integer.toString(i)).setSource(termField, termValue).get(); | ||
| } | ||
| } | ||
| ensureGreen(); | ||
| waitForRelocation(); | ||
| forceMerge(); | ||
| refresh(); | ||
|
|
||
| // Search excluding range 30 to 50 | ||
|
|
||
| int lt = 50; | ||
| int gte = 30; | ||
| int expectedHitCount = numDocs - (lt - gte); // Expected hit count includes documents missing this value | ||
|
|
||
| assertHitCount( | ||
| client().prepareSearch().setQuery(boolQuery().mustNot(rangeQuery(intField).lt(lt).gte(gte))).get(), | ||
| expectedHitCount | ||
| ); | ||
| } | ||
|
|
||
| public void testMustNotRangeRewriteWithMoreThanOneValue() throws Exception { | ||
| // This test passes because the rewrite is skipped when not all docs have exactly 1 value | ||
| String intField = "int_field"; | ||
| createIndex("test"); | ||
| int numDocs = 100; | ||
| int numDocsWithExtraValue = 20; | ||
| int extraValue = -1; | ||
|
|
||
| for (int i = 0; i < numDocs; i++) { | ||
| if (i < numDocsWithExtraValue) { | ||
| client().prepareIndex("test").setId(Integer.toString(i)).setSource(intField, new int[] { extraValue, i }).get(); | ||
| } else { | ||
| client().prepareIndex("test").setId(Integer.toString(i)).setSource(intField, i).get(); | ||
| } | ||
| } | ||
| ensureGreen(); | ||
| waitForRelocation(); | ||
| forceMerge(); | ||
| refresh(); | ||
|
|
||
| // Range queries will match if ANY of the doc's values are within the range. | ||
| // So if we exclude the range 0 to 20, we shouldn't see any of those documents returned, | ||
| // even though they also have a value -1 which is not excluded. | ||
|
|
||
| int expectedHitCount = numDocs - numDocsWithExtraValue; | ||
| assertHitCount( | ||
| client().prepareSearch().setQuery(boolQuery().mustNot(rangeQuery(intField).lt(numDocsWithExtraValue).gte(0))).get(), | ||
| expectedHitCount | ||
| ); | ||
| assertHitCount(client().prepareSearch().setQuery(matchAllQuery()).get(), numDocs); | ||
| } | ||
|
|
||
| private String padZeros(int value, int length) { | ||
| // String.format() not allowed | ||
| String ret = Integer.toString(value); | ||
| if (ret.length() < length) { | ||
| ret = "0".repeat(length - ret.length()) + ret; | ||
| } | ||
| return ret; | ||
| } | ||
|
|
||
| private String getDate(int day, int hour) { | ||
| return "2016-01-" + padZeros(day, 2) + "T" + padZeros(hour, 2) + ":00:00.000"; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The json mapping passed here seems incorrect. The json string has an extra closing braces.