From ec5d3336d5a6d79214b321cd3207d51a0f688793 Mon Sep 17 00:00:00 2001 From: Kathleen DeRusso Date: Wed, 4 Dec 2024 15:36:59 -0500 Subject: [PATCH] Some more cleanup/renames --- .../index/query/MatchQueryBuilder.java | 16 ++++++++++------ .../org/elasticsearch/search/SearchFeatures.java | 1 - .../elasticsearch/index/IndexModuleTests.java | 3 ++- .../xpack/inference/InferenceFeatures.java | 2 +- .../queries/SemanticQueryRewriteInterceptor.java | 14 ++++++++------ .../test/inference/45_semantic_text_match.yml | 2 +- 6 files changed, 22 insertions(+), 16 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/index/query/MatchQueryBuilder.java b/server/src/main/java/org/elasticsearch/index/query/MatchQueryBuilder.java index b53035243d886..08c88ce97fe87 100644 --- a/server/src/main/java/org/elasticsearch/index/query/MatchQueryBuilder.java +++ b/server/src/main/java/org/elasticsearch/index/query/MatchQueryBuilder.java @@ -81,12 +81,16 @@ public class MatchQueryBuilder extends AbstractQueryBuilder { private boolean autoGenerateSynonymsPhraseQuery = true; - private final boolean inferenceFieldsChecked; + /** + * Indicates that this MatchQueryBuilder has already been intercepted and rewritten, + * so subsequent rewrite rounds can short-circuit interception. + */ + private final boolean interceptedAndRewritten; /** * Constructs a new match query. */ - public MatchQueryBuilder(String fieldName, Object value, boolean inferenceFieldsChecked) { + public MatchQueryBuilder(String fieldName, Object value, boolean interceptedAndRewritten) { if (fieldName == null) { throw new IllegalArgumentException("[" + NAME + "] requires fieldName"); } @@ -95,7 +99,7 @@ public MatchQueryBuilder(String fieldName, Object value, boolean inferenceFields } this.fieldName = fieldName; this.value = value; - this.inferenceFieldsChecked = inferenceFieldsChecked; + this.interceptedAndRewritten = interceptedAndRewritten; } public MatchQueryBuilder(String fieldName, Object value) { @@ -125,7 +129,7 @@ public MatchQueryBuilder(StreamInput in) throws IOException { in.readOptionalFloat(); } autoGenerateSynonymsPhraseQuery = in.readBoolean(); - inferenceFieldsChecked = false; + interceptedAndRewritten = false; } @Override @@ -199,8 +203,8 @@ public Fuzziness fuzziness() { return this.fuzziness; } - public boolean getInferenceFieldsChecked() { - return inferenceFieldsChecked; + public boolean getInterceptedAndRewritten() { + return interceptedAndRewritten; } /** diff --git a/server/src/main/java/org/elasticsearch/search/SearchFeatures.java b/server/src/main/java/org/elasticsearch/search/SearchFeatures.java index 8ecc647bfc60e..beac39c2de304 100644 --- a/server/src/main/java/org/elasticsearch/search/SearchFeatures.java +++ b/server/src/main/java/org/elasticsearch/search/SearchFeatures.java @@ -11,7 +11,6 @@ import org.elasticsearch.features.FeatureSpecification; import org.elasticsearch.features.NodeFeature; -import org.elasticsearch.index.query.QueryRewriteInterceptor; import org.elasticsearch.search.vectors.KnnVectorQueryBuilder; import java.util.Set; diff --git a/server/src/test/java/org/elasticsearch/index/IndexModuleTests.java b/server/src/test/java/org/elasticsearch/index/IndexModuleTests.java index 6100d1eb7b687..1f708ae868b70 100644 --- a/server/src/test/java/org/elasticsearch/index/IndexModuleTests.java +++ b/server/src/test/java/org/elasticsearch/index/IndexModuleTests.java @@ -92,6 +92,7 @@ import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.IndexSettingsModule; import org.elasticsearch.test.engine.MockEngineFactory; +import org.elasticsearch.test.index.query.MockQueryRewriteInterceptor; import org.elasticsearch.threadpool.TestThreadPool; import org.elasticsearch.threadpool.ThreadPool; import org.hamcrest.Matchers; @@ -224,7 +225,7 @@ private IndexService newIndexService(IndexModule module) throws IOException { null, indexDeletionListener, emptyMap(), - null // TODO create interceptor + new MockQueryRewriteInterceptor() ); } diff --git a/x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/InferenceFeatures.java b/x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/InferenceFeatures.java index c1e00a0ed697e..b1e1c809b6269 100644 --- a/x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/InferenceFeatures.java +++ b/x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/InferenceFeatures.java @@ -31,7 +31,7 @@ public Set getFeatures() { SemanticQueryBuilder.SEMANTIC_TEXT_INNER_HITS, SemanticTextFieldMapper.SEMANTIC_TEXT_DEFAULT_ELSER_2, TextSimilarityRankRetrieverBuilder.TEXT_SIMILARITY_RERANKER_COMPOSITION_SUPPORTED, - SemanticQueryRewriteInterceptor.QUERY_REWRITE_INTERCEPTION_SUPPORTED + SemanticQueryRewriteInterceptor.SEMANTIC_MATCH_QUERY_REWRITE_INTERCEPTION_SUPPORTED ); } diff --git a/x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/queries/SemanticQueryRewriteInterceptor.java b/x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/queries/SemanticQueryRewriteInterceptor.java index 3c8badbf9f7f6..0a69d55194128 100644 --- a/x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/queries/SemanticQueryRewriteInterceptor.java +++ b/x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/queries/SemanticQueryRewriteInterceptor.java @@ -26,7 +26,9 @@ public class SemanticQueryRewriteInterceptor implements QueryRewriteInterceptor { - public static final NodeFeature QUERY_REWRITE_INTERCEPTION_SUPPORTED = new NodeFeature("search.query_rewrite_interception_supported"); + public static final NodeFeature SEMANTIC_MATCH_QUERY_REWRITE_INTERCEPTION_SUPPORTED = new NodeFeature( + "search.semantic_match_query_rewrite_interception_supported" + ); public SemanticQueryRewriteInterceptor() {} @@ -39,7 +41,7 @@ public QueryBuilder rewrite(QueryRewriteContext context, QueryBuilder queryBuild if (queryBuilder instanceof MatchQueryBuilder matchQueryBuilder) { QueryBuilder rewritten = queryBuilder; - if (context.convertToQueryRewriteContext() != null && matchQueryBuilder.getInferenceFieldsChecked() == false) { + if (context.convertToQueryRewriteContext() != null && matchQueryBuilder.getInterceptedAndRewritten() == false) { ResolvedIndices resolvedIndices = context.getResolvedIndices(); if (resolvedIndices != null) { Collection indexMetadataCollection = resolvedIndices.getConcreteLocalIndicesMetadata().values(); @@ -61,7 +63,7 @@ public QueryBuilder rewrite(QueryRewriteContext context, QueryBuilder queryBuild for (String inferenceIndexName : inferenceIndices) { // Add a separate clause for each inference query, because they may be using different inference endpoints boolQueryBuilder.should( - createInferenceSubQuery( + createSemanticSubQuery( inferenceIndexName, matchQueryBuilder.fieldName(), (String) matchQueryBuilder.value() @@ -69,7 +71,7 @@ public QueryBuilder rewrite(QueryRewriteContext context, QueryBuilder queryBuild ); } boolQueryBuilder.should( - createNonInferenceSubQuery(nonInferenceIndices, matchQueryBuilder.fieldName(), matchQueryBuilder.value()) + createMatchSubQuery(nonInferenceIndices, matchQueryBuilder.fieldName(), matchQueryBuilder.value()) ); rewritten = boolQueryBuilder; } else if (inferenceIndices.isEmpty() == false) { @@ -83,14 +85,14 @@ public QueryBuilder rewrite(QueryRewriteContext context, QueryBuilder queryBuild return queryBuilder; } - private QueryBuilder createInferenceSubQuery(String indexName, String fieldName, String value) { + private QueryBuilder createSemanticSubQuery(String indexName, String fieldName, String value) { BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder(); boolQueryBuilder.must(new SemanticQueryBuilder(fieldName, value, false)); boolQueryBuilder.filter(new TermQueryBuilder(IndexFieldMapper.NAME, indexName)); return boolQueryBuilder; } - private QueryBuilder createNonInferenceSubQuery(List indices, String fieldName, Object value) { + private QueryBuilder createMatchSubQuery(List indices, String fieldName, Object value) { BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder(); boolQueryBuilder.must(new MatchQueryBuilder(fieldName, value, true)); boolQueryBuilder.filter(new TermsQueryBuilder(IndexFieldMapper.NAME, indices)); diff --git a/x-pack/plugin/inference/src/yamlRestTest/resources/rest-api-spec/test/inference/45_semantic_text_match.yml b/x-pack/plugin/inference/src/yamlRestTest/resources/rest-api-spec/test/inference/45_semantic_text_match.yml index c5f0bcef9417c..395ca348501fb 100644 --- a/x-pack/plugin/inference/src/yamlRestTest/resources/rest-api-spec/test/inference/45_semantic_text_match.yml +++ b/x-pack/plugin/inference/src/yamlRestTest/resources/rest-api-spec/test/inference/45_semantic_text_match.yml @@ -1,6 +1,6 @@ setup: - requires: - cluster_features: "search.query_rewrite_interception_supported" + cluster_features: "search.semantic_match_query_rewrite_interception_supported" reason: semantic_text match support introduced in 8.18.0 - do: