Skip to content

Commit

Permalink
Some more cleanup/renames
Browse files Browse the repository at this point in the history
  • Loading branch information
kderusso committed Dec 4, 2024
1 parent 11b3efa commit ec5d333
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,16 @@ public class MatchQueryBuilder extends AbstractQueryBuilder<MatchQueryBuilder> {

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");
}
Expand All @@ -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) {
Expand Down Expand Up @@ -125,7 +129,7 @@ public MatchQueryBuilder(StreamInput in) throws IOException {
in.readOptionalFloat();
}
autoGenerateSynonymsPhraseQuery = in.readBoolean();
inferenceFieldsChecked = false;
interceptedAndRewritten = false;
}

@Override
Expand Down Expand Up @@ -199,8 +203,8 @@ public Fuzziness fuzziness() {
return this.fuzziness;
}

public boolean getInferenceFieldsChecked() {
return inferenceFieldsChecked;
public boolean getInterceptedAndRewritten() {
return interceptedAndRewritten;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -224,7 +225,7 @@ private IndexService newIndexService(IndexModule module) throws IOException {
null,
indexDeletionListener,
emptyMap(),
null // TODO create interceptor
new MockQueryRewriteInterceptor()
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public Set<NodeFeature> 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
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}

Expand All @@ -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<IndexMetadata> indexMetadataCollection = resolvedIndices.getConcreteLocalIndicesMetadata().values();
Expand All @@ -61,15 +63,15 @@ 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()
)
);
}
boolQueryBuilder.should(
createNonInferenceSubQuery(nonInferenceIndices, matchQueryBuilder.fieldName(), matchQueryBuilder.value())
createMatchSubQuery(nonInferenceIndices, matchQueryBuilder.fieldName(), matchQueryBuilder.value())
);
rewritten = boolQueryBuilder;
} else if (inferenceIndices.isEmpty() == false) {
Expand All @@ -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<String> indices, String fieldName, Object value) {
private QueryBuilder createMatchSubQuery(List<String> indices, String fieldName, Object value) {
BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
boolQueryBuilder.must(new MatchQueryBuilder(fieldName, value, true));
boolQueryBuilder.filter(new TermsQueryBuilder(IndexFieldMapper.NAME, indices));
Expand Down
Original file line number Diff line number Diff line change
@@ -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:
Expand Down

0 comments on commit ec5d333

Please sign in to comment.