From bcb93c3b7443a4841370957d577ed64c98b8f06e Mon Sep 17 00:00:00 2001 From: Kathleen DeRusso Date: Mon, 2 Dec 2024 13:42:00 -0500 Subject: [PATCH] Fix tests --- .../index/query/InferenceQueryBuilderService.java | 4 +++- .../index/query/InferenceQueryBuilderServiceBuilder.java | 7 ++++++- .../org/elasticsearch/index/query/MatchQueryBuilder.java | 7 +++++-- .../xpack/inference/mapper/SemanticTextFieldMapper.java | 6 +++--- 4 files changed, 17 insertions(+), 7 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/index/query/InferenceQueryBuilderService.java b/server/src/main/java/org/elasticsearch/index/query/InferenceQueryBuilderService.java index a8f0a7b28f01c..2d7ecd2aec178 100644 --- a/server/src/main/java/org/elasticsearch/index/query/InferenceQueryBuilderService.java +++ b/server/src/main/java/org/elasticsearch/index/query/InferenceQueryBuilderService.java @@ -18,6 +18,8 @@ public class InferenceQueryBuilderService { } public AbstractQueryBuilder getDefaultInferenceQueryBuilder(String fieldName, String query, boolean throwOnUnsupportedQueries) { - return defaultInferenceQueryBuilder.apply(fieldName, query, throwOnUnsupportedQueries); + return defaultInferenceQueryBuilder != null + ? defaultInferenceQueryBuilder.apply(fieldName, query, throwOnUnsupportedQueries) + : null; } } diff --git a/server/src/main/java/org/elasticsearch/index/query/InferenceQueryBuilderServiceBuilder.java b/server/src/main/java/org/elasticsearch/index/query/InferenceQueryBuilderServiceBuilder.java index 6bcd67dd51585..3a346cdb32ceb 100644 --- a/server/src/main/java/org/elasticsearch/index/query/InferenceQueryBuilderServiceBuilder.java +++ b/server/src/main/java/org/elasticsearch/index/query/InferenceQueryBuilderServiceBuilder.java @@ -37,9 +37,14 @@ public InferenceQueryBuilderService build() { } } + if (definedInferenceQueryBuilders.isEmpty()) { + // Backwards compatibility + return new InferenceQueryBuilderService(null); + } + if (definedInferenceQueryBuilders.size() != 1) { throw new IllegalStateException( - "Expected exactly one default inference query builder, but found " + definedInferenceQueryBuilders.size() + "Expected a single default inference query builder, but found " + definedInferenceQueryBuilders.size() ); } 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 ded6ad59fb206..414c6c4be92f8 100644 --- a/server/src/main/java/org/elasticsearch/index/query/MatchQueryBuilder.java +++ b/server/src/main/java/org/elasticsearch/index/query/MatchQueryBuilder.java @@ -418,9 +418,12 @@ protected QueryBuilder doRewrite(QueryRewriteContext queryRewriteContext) throws } private QueryBuilder createInferenceSubQuery(InferenceQueryBuilderService inferenceQueryBuilderService, String indexName) { + QueryBuilder inferenceClause = inferenceQueryBuilderService.getDefaultInferenceQueryBuilder(fieldName, value.toString(), false); BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder(); - boolQueryBuilder.must(inferenceQueryBuilderService.getDefaultInferenceQueryBuilder(fieldName, value.toString(), false)); - boolQueryBuilder.filter(new TermQueryBuilder(IndexFieldMapper.NAME, indexName)); + if (inferenceClause != null) { + boolQueryBuilder.must(inferenceClause); + boolQueryBuilder.filter(new TermQueryBuilder(IndexFieldMapper.NAME, indexName)); + } return boolQueryBuilder; } diff --git a/x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/mapper/SemanticTextFieldMapper.java b/x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/mapper/SemanticTextFieldMapper.java index 27643ddd21302..5cea0fd2ec0ef 100644 --- a/x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/mapper/SemanticTextFieldMapper.java +++ b/x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/mapper/SemanticTextFieldMapper.java @@ -387,7 +387,7 @@ public InferenceFieldMetadata getMetadata(Set sourcePaths) { String[] copyFields = sourcePaths.toArray(String[]::new); // ensure consistent order Arrays.sort(copyFields); - return new InferenceFieldMetadata(fullPath(), fieldType().inferenceId, fieldType().searchInferenceId, copyFields); + return new InferenceFieldMetadata(fullPath(), fieldType().getInferenceId(), fieldType().getSearchInferenceId(), copyFields); } @Override @@ -441,7 +441,7 @@ public SemanticTextFieldType( ) { super(name, true, false, false, TextSearchInfo.NONE, meta); this.inferenceId = inferenceId; - this.searchInferenceId = searchInferenceId == null ? inferenceId : searchInferenceId; + this.searchInferenceId = searchInferenceId; ; this.modelSettings = modelSettings; this.inferenceField = inferenceField; @@ -458,7 +458,7 @@ public String getInferenceId() { } public String getSearchInferenceId() { - return searchInferenceId; + return searchInferenceId == null ? inferenceId : searchInferenceId; } public SemanticTextField.ModelSettings getModelSettings() {