Skip to content

Commit ddf9faf

Browse files
authored
Condition Default ELSER Endpoint For Semantic Text on Feature Flag (#115684) (#115690)
1 parent 239038b commit ddf9faf

File tree

4 files changed

+49
-30
lines changed

4 files changed

+49
-30
lines changed

docs/changelog/113563.yaml

Lines changed: 0 additions & 5 deletions
This file was deleted.

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/InferenceFeatures.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.elasticsearch.xpack.inference.rank.random.RandomRankRetrieverBuilder;
1414
import org.elasticsearch.xpack.inference.rank.textsimilarity.TextSimilarityRankRetrieverBuilder;
1515

16+
import java.util.HashSet;
1617
import java.util.Set;
1718

1819
/**
@@ -22,13 +23,15 @@ public class InferenceFeatures implements FeatureSpecification {
2223

2324
@Override
2425
public Set<NodeFeature> getFeatures() {
25-
return Set.of(
26-
TextSimilarityRankRetrieverBuilder.TEXT_SIMILARITY_RERANKER_RETRIEVER_SUPPORTED,
27-
RandomRankRetrieverBuilder.RANDOM_RERANKER_RETRIEVER_SUPPORTED,
28-
SemanticTextFieldMapper.SEMANTIC_TEXT_SEARCH_INFERENCE_ID,
29-
SemanticTextFieldMapper.SEMANTIC_TEXT_DEFAULT_ELSER_2,
30-
TextSimilarityRankRetrieverBuilder.TEXT_SIMILARITY_RERANKER_COMPOSITION_SUPPORTED
31-
);
26+
var features = new HashSet<NodeFeature>();
27+
features.add(TextSimilarityRankRetrieverBuilder.TEXT_SIMILARITY_RERANKER_RETRIEVER_SUPPORTED);
28+
features.add(RandomRankRetrieverBuilder.RANDOM_RERANKER_RETRIEVER_SUPPORTED);
29+
features.add(SemanticTextFieldMapper.SEMANTIC_TEXT_SEARCH_INFERENCE_ID);
30+
features.add(TextSimilarityRankRetrieverBuilder.TEXT_SIMILARITY_RERANKER_COMPOSITION_SUPPORTED);
31+
if (DefaultElserFeatureFlag.isEnabled()) {
32+
features.add(SemanticTextFieldMapper.SEMANTIC_TEXT_DEFAULT_ELSER_2);
33+
}
34+
return Set.copyOf(features);
3235
}
3336

3437
@Override

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/mapper/SemanticTextFieldMapper.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import org.elasticsearch.xcontent.XContentParserConfiguration;
5656
import org.elasticsearch.xpack.core.ml.inference.results.MlTextEmbeddingResults;
5757
import org.elasticsearch.xpack.core.ml.inference.results.TextExpansionResults;
58+
import org.elasticsearch.xpack.inference.DefaultElserFeatureFlag;
5859

5960
import java.io.IOException;
6061
import java.util.ArrayList;
@@ -106,12 +107,16 @@ public static class Builder extends FieldMapper.Builder {
106107
INFERENCE_ID_FIELD,
107108
false,
108109
mapper -> ((SemanticTextFieldType) mapper.fieldType()).inferenceId,
109-
DEFAULT_ELSER_2_INFERENCE_ID
110+
DefaultElserFeatureFlag.isEnabled() ? DEFAULT_ELSER_2_INFERENCE_ID : null
110111
).addValidator(v -> {
111112
if (Strings.isEmpty(v)) {
112-
throw new IllegalArgumentException(
113-
"[" + INFERENCE_ID_FIELD + "] on mapper [" + leafName() + "] of type [" + CONTENT_TYPE + "] must not be empty"
114-
);
113+
// If the default ELSER feature flag is enabled, the only way we get here is if the user explicitly sets the param to an
114+
// empty value. However, if the feature flag is disabled, we can get here if the user didn't set the param.
115+
// Adjust the error message appropriately.
116+
String message = DefaultElserFeatureFlag.isEnabled()
117+
? "[" + INFERENCE_ID_FIELD + "] on mapper [" + leafName() + "] of type [" + CONTENT_TYPE + "] must not be empty"
118+
: "[" + INFERENCE_ID_FIELD + "] on mapper [" + leafName() + "] of type [" + CONTENT_TYPE + "] must be specified";
119+
throw new IllegalArgumentException(message);
115120
}
116121
});
117122

x-pack/plugin/inference/src/test/java/org/elasticsearch/xpack/inference/mapper/SemanticTextFieldMapperTests.java

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
import org.elasticsearch.xcontent.XContentParser;
6262
import org.elasticsearch.xcontent.XContentType;
6363
import org.elasticsearch.xcontent.json.JsonXContent;
64+
import org.elasticsearch.xpack.inference.DefaultElserFeatureFlag;
6465
import org.elasticsearch.xpack.inference.InferencePlugin;
6566
import org.elasticsearch.xpack.inference.model.TestModel;
6667
import org.junit.AssumptionViolatedException;
@@ -102,6 +103,9 @@ protected Collection<? extends Plugin> getPlugins() {
102103
@Override
103104
protected void minimalMapping(XContentBuilder b) throws IOException {
104105
b.field("type", "semantic_text");
106+
if (DefaultElserFeatureFlag.isEnabled() == false) {
107+
b.field("inference_id", "test_model");
108+
}
105109
}
106110

107111
@Override
@@ -171,7 +175,9 @@ public void testDefaults() throws Exception {
171175
DocumentMapper mapper = mapperService.documentMapper();
172176
assertEquals(Strings.toString(fieldMapping), mapper.mappingSource().toString());
173177
assertSemanticTextField(mapperService, fieldName, false);
174-
assertInferenceEndpoints(mapperService, fieldName, DEFAULT_ELSER_2_INFERENCE_ID, DEFAULT_ELSER_2_INFERENCE_ID);
178+
if (DefaultElserFeatureFlag.isEnabled()) {
179+
assertInferenceEndpoints(mapperService, fieldName, DEFAULT_ELSER_2_INFERENCE_ID, DEFAULT_ELSER_2_INFERENCE_ID);
180+
}
175181

176182
ParsedDocument doc1 = mapper.parse(source(this::writeField));
177183
List<IndexableField> fields = doc1.rootDoc().getFields("field");
@@ -205,13 +211,15 @@ public void testSetInferenceEndpoints() throws IOException {
205211
assertSerialization.accept(fieldMapping, mapperService);
206212
}
207213
{
208-
final XContentBuilder fieldMapping = fieldMapping(
209-
b -> b.field("type", "semantic_text").field(SEARCH_INFERENCE_ID_FIELD, searchInferenceId)
210-
);
211-
final MapperService mapperService = createMapperService(fieldMapping);
212-
assertSemanticTextField(mapperService, fieldName, false);
213-
assertInferenceEndpoints(mapperService, fieldName, DEFAULT_ELSER_2_INFERENCE_ID, searchInferenceId);
214-
assertSerialization.accept(fieldMapping, mapperService);
214+
if (DefaultElserFeatureFlag.isEnabled()) {
215+
final XContentBuilder fieldMapping = fieldMapping(
216+
b -> b.field("type", "semantic_text").field(SEARCH_INFERENCE_ID_FIELD, searchInferenceId)
217+
);
218+
final MapperService mapperService = createMapperService(fieldMapping);
219+
assertSemanticTextField(mapperService, fieldName, false);
220+
assertInferenceEndpoints(mapperService, fieldName, DEFAULT_ELSER_2_INFERENCE_ID, searchInferenceId);
221+
assertSerialization.accept(fieldMapping, mapperService);
222+
}
215223
}
216224
{
217225
final XContentBuilder fieldMapping = fieldMapping(
@@ -238,18 +246,26 @@ public void testInvalidInferenceEndpoints() {
238246
);
239247
}
240248
{
249+
final String expectedMessage = DefaultElserFeatureFlag.isEnabled()
250+
? "[inference_id] on mapper [field] of type [semantic_text] must not be empty"
251+
: "[inference_id] on mapper [field] of type [semantic_text] must be specified";
241252
Exception e = expectThrows(
242253
MapperParsingException.class,
243254
() -> createMapperService(fieldMapping(b -> b.field("type", "semantic_text").field(INFERENCE_ID_FIELD, "")))
244255
);
245-
assertThat(e.getMessage(), containsString("[inference_id] on mapper [field] of type [semantic_text] must not be empty"));
256+
assertThat(e.getMessage(), containsString(expectedMessage));
246257
}
247258
{
248-
Exception e = expectThrows(
249-
MapperParsingException.class,
250-
() -> createMapperService(fieldMapping(b -> b.field("type", "semantic_text").field(SEARCH_INFERENCE_ID_FIELD, "")))
251-
);
252-
assertThat(e.getMessage(), containsString("[search_inference_id] on mapper [field] of type [semantic_text] must not be empty"));
259+
if (DefaultElserFeatureFlag.isEnabled()) {
260+
Exception e = expectThrows(
261+
MapperParsingException.class,
262+
() -> createMapperService(fieldMapping(b -> b.field("type", "semantic_text").field(SEARCH_INFERENCE_ID_FIELD, "")))
263+
);
264+
assertThat(
265+
e.getMessage(),
266+
containsString("[search_inference_id] on mapper [field] of type [semantic_text] must not be empty")
267+
);
268+
}
253269
}
254270
}
255271

0 commit comments

Comments
 (0)