-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Add query rewriting infrastructure to reduce query complexity #19060
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
+3,012
−208
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
f58ba21
Add query rewriting infrastructure to reduce query complexity
atris 478e22a
Fix forbidden api issues
atris 847ef04
Update writers and get tests to pass
atris 2cfefbe
Update per CI
atris d498e7d
Fix term merging threshold and update comments
atris f1e1ea4
Expose setting and update per comments
atris da6a580
Update CHANGELOG
atris 014c001
Fix tests and ensure scoring MATCH ALL query is preserved
atris e75dd9d
Migrate must to filter and must not to should optimizations to query …
atris 03bf50f
Handle fields with missing fields
atris 0524afd
Update per comments
atris 7b72846
Merge branch 'main' into really_new_que
atris 2d0f234
Add missing package info
atris 87352b7
Spotless checks
atris 4155cfe
Remove redundant tests
atris 89c8998
Update to use singleton pattern
atris 1ebf7a1
Update per comments
atris 43d3b2e
More comments
atris ef766f7
Move to singleton instance for rewriters
atris d161640
Remove singleton pattern breakage and mark as experimental
atris 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
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
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
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
50 changes: 50 additions & 0 deletions
50
server/src/main/java/org/opensearch/search/query/QueryRewriter.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,50 @@ | ||
| /* | ||
| * 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 org.opensearch.common.annotation.ExperimentalApi; | ||
| import org.opensearch.index.query.QueryBuilder; | ||
| import org.opensearch.index.query.QueryShardContext; | ||
|
|
||
| /** | ||
| * Interface for query rewriting implementations that optimize query structure | ||
| * before conversion to Lucene queries. | ||
| * | ||
| * @opensearch.experimental | ||
| */ | ||
| @ExperimentalApi | ||
| public interface QueryRewriter { | ||
rishabhmaurya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Rewrites the given query builder to a more optimal form. | ||
| * | ||
| * @param query The query to rewrite | ||
| * @param context The search execution context | ||
| * @return The rewritten query (may be the same instance if no rewrite needed) | ||
| */ | ||
| QueryBuilder rewrite(QueryBuilder query, QueryShardContext context); | ||
|
|
||
| /** | ||
| * Returns the priority of this rewriter. Lower values execute first. | ||
| * This allows control over rewrite ordering when multiple rewriters | ||
| * may interact. | ||
| * | ||
| * @return The priority value | ||
| */ | ||
| default int priority() { | ||
| return 1000; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the name of this rewriter for debugging and profiling. | ||
| * | ||
| * @return The rewriter name | ||
| */ | ||
| String name(); | ||
| } | ||
113 changes: 113 additions & 0 deletions
113
server/src/main/java/org/opensearch/search/query/QueryRewriterRegistry.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,113 @@ | ||
| /* | ||
| * 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 org.apache.logging.log4j.LogManager; | ||
| import org.apache.logging.log4j.Logger; | ||
| import org.opensearch.common.settings.ClusterSettings; | ||
| import org.opensearch.common.settings.Settings; | ||
| import org.opensearch.index.query.QueryBuilder; | ||
| import org.opensearch.index.query.QueryShardContext; | ||
| import org.opensearch.search.SearchService; | ||
| import org.opensearch.search.query.rewriters.BooleanFlatteningRewriter; | ||
| import org.opensearch.search.query.rewriters.MatchAllRemovalRewriter; | ||
| import org.opensearch.search.query.rewriters.MustNotToShouldRewriter; | ||
| import org.opensearch.search.query.rewriters.MustToFilterRewriter; | ||
| import org.opensearch.search.query.rewriters.TermsMergingRewriter; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Comparator; | ||
| import java.util.List; | ||
| import java.util.concurrent.CopyOnWriteArrayList; | ||
|
|
||
| /** | ||
| * Registry for query rewriters | ||
| * | ||
| * @opensearch.internal | ||
| */ | ||
| public final class QueryRewriterRegistry { | ||
|
|
||
| private static final Logger logger = LogManager.getLogger(QueryRewriterRegistry.class); | ||
|
|
||
| public static final QueryRewriterRegistry INSTANCE = new QueryRewriterRegistry(); | ||
|
|
||
| /** | ||
| * Default rewriters. | ||
| * CopyOnWriteArrayList is used for thread-safety during registration. | ||
| */ | ||
| private final CopyOnWriteArrayList<QueryRewriter> rewriters; | ||
|
|
||
| /** | ||
| * Whether query rewriting is enabled. | ||
| */ | ||
| private volatile boolean enabled; | ||
|
|
||
| private QueryRewriterRegistry() { | ||
| this.rewriters = new CopyOnWriteArrayList<>(); | ||
|
|
||
| // Register default rewriters using singletons | ||
| registerRewriter(BooleanFlatteningRewriter.INSTANCE); | ||
| registerRewriter(MustToFilterRewriter.INSTANCE); | ||
| registerRewriter(MustNotToShouldRewriter.INSTANCE); | ||
| registerRewriter(MatchAllRemovalRewriter.INSTANCE); | ||
| registerRewriter(TermsMergingRewriter.INSTANCE); | ||
| } | ||
|
|
||
| /** | ||
| * Register a custom query rewriter. | ||
| * | ||
| * @param rewriter The rewriter to register | ||
| */ | ||
| public void registerRewriter(QueryRewriter rewriter) { | ||
| if (rewriter != null) { | ||
| rewriters.add(rewriter); | ||
| logger.info("Registered query rewriter: {}", rewriter.name()); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Initialize the registry with cluster settings. | ||
| * This must be called once during system startup to properly configure | ||
| * the TermsMergingRewriter with settings and update consumers. | ||
| * | ||
| * @param settings Initial cluster settings | ||
| * @param clusterSettings Cluster settings for registering update consumers | ||
| */ | ||
| public void initialize(Settings settings, ClusterSettings clusterSettings) { | ||
| TermsMergingRewriter.INSTANCE.initialize(settings, clusterSettings); | ||
| this.enabled = SearchService.QUERY_REWRITING_ENABLED_SETTING.get(settings); | ||
| clusterSettings.addSettingsUpdateConsumer( | ||
| SearchService.QUERY_REWRITING_ENABLED_SETTING, | ||
| (Boolean enabled) -> this.enabled = enabled | ||
| ); | ||
| } | ||
|
|
||
| public QueryBuilder rewrite(QueryBuilder query, QueryShardContext context) { | ||
| if (!enabled || query == null) { | ||
| return query; | ||
| } | ||
|
|
||
| List<QueryRewriter> sortedRewriters = new ArrayList<>(rewriters); | ||
| sortedRewriters.sort(Comparator.comparingInt(QueryRewriter::priority)); | ||
|
|
||
| QueryBuilder current = query; | ||
| for (QueryRewriter rewriter : sortedRewriters) { | ||
| try { | ||
| QueryBuilder rewritten = rewriter.rewrite(current, context); | ||
| if (rewritten != current) { | ||
rishabhmaurya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| current = rewritten; | ||
| } | ||
| } catch (Exception e) { | ||
| logger.warn("Query rewriter {} failed: {}", rewriter.name(), e.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| return current; | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.