5454import org .elasticsearch .xcontent .XContentParserConfiguration ;
5555import org .elasticsearch .xpack .core .ml .inference .results .MlTextEmbeddingResults ;
5656import org .elasticsearch .xpack .core .ml .inference .results .TextExpansionResults ;
57+ import org .elasticsearch .xpack .inference .DefaultElserFeatureFlag ;
5758
5859import java .io .IOException ;
5960import java .util .ArrayList ;
7172import static org .elasticsearch .xpack .inference .mapper .SemanticTextField .CHUNKS_FIELD ;
7273import static org .elasticsearch .xpack .inference .mapper .SemanticTextField .INFERENCE_FIELD ;
7374import static org .elasticsearch .xpack .inference .mapper .SemanticTextField .INFERENCE_ID_FIELD ;
75+ import static org .elasticsearch .xpack .inference .mapper .SemanticTextField .MODEL_SETTINGS_FIELD ;
76+ import static org .elasticsearch .xpack .inference .mapper .SemanticTextField .SEARCH_INFERENCE_ID_FIELD ;
7477import static org .elasticsearch .xpack .inference .mapper .SemanticTextField .TEXT_FIELD ;
7578import static org .elasticsearch .xpack .inference .mapper .SemanticTextField .getChunksFieldName ;
7679import static org .elasticsearch .xpack .inference .mapper .SemanticTextField .getEmbeddingsFieldName ;
7780import static org .elasticsearch .xpack .inference .mapper .SemanticTextField .getOriginalTextFieldName ;
81+ import static org .elasticsearch .xpack .inference .services .elasticsearch .ElasticsearchInternalService .DEFAULT_ELSER_ID ;
7882
7983/**
8084 * A {@link FieldMapper} for semantic text fields.
8185 */
8286public class SemanticTextFieldMapper extends FieldMapper implements InferenceFieldMapper {
8387 public static final NodeFeature SEMANTIC_TEXT_SEARCH_INFERENCE_ID = new NodeFeature ("semantic_text.search_inference_id" );
88+ public static final NodeFeature SEMANTIC_TEXT_DEFAULT_ELSER_2 = new NodeFeature ("semantic_text.default_elser_2" );
8489
8590 public static final String CONTENT_TYPE = "semantic_text" ;
91+ public static final String DEFAULT_ELSER_2_INFERENCE_ID = DEFAULT_ELSER_ID ;
8692
8793 private final IndexSettings indexSettings ;
8894
@@ -96,25 +102,37 @@ public static class Builder extends FieldMapper.Builder {
96102 private final IndexSettings indexSettings ;
97103
98104 private final Parameter <String > inferenceId = Parameter .stringParam (
99- "inference_id" ,
105+ INFERENCE_ID_FIELD ,
100106 false ,
101107 mapper -> ((SemanticTextFieldType ) mapper .fieldType ()).inferenceId ,
102- null
108+ DefaultElserFeatureFlag . isEnabled () ? DEFAULT_ELSER_2_INFERENCE_ID : null
103109 ).addValidator (v -> {
104110 if (Strings .isEmpty (v )) {
105- throw new IllegalArgumentException ("field [inference_id] must be specified" );
111+ // If the default ELSER feature flag is enabled, the only way we get here is if the user explicitly sets the param to an
112+ // empty value. However, if the feature flag is disabled, we can get here if the user didn't set the param.
113+ // Adjust the error message appropriately.
114+ String message = DefaultElserFeatureFlag .isEnabled ()
115+ ? "[" + INFERENCE_ID_FIELD + "] on mapper [" + leafName () + "] of type [" + CONTENT_TYPE + "] must not be empty"
116+ : "[" + INFERENCE_ID_FIELD + "] on mapper [" + leafName () + "] of type [" + CONTENT_TYPE + "] must be specified" ;
117+ throw new IllegalArgumentException (message );
106118 }
107119 });
108120
109121 private final Parameter <String > searchInferenceId = Parameter .stringParam (
110- "search_inference_id" ,
122+ SEARCH_INFERENCE_ID_FIELD ,
111123 true ,
112124 mapper -> ((SemanticTextFieldType ) mapper .fieldType ()).searchInferenceId ,
113125 null
114- ).acceptsNull ();
126+ ).acceptsNull ().addValidator (v -> {
127+ if (v != null && Strings .isEmpty (v )) {
128+ throw new IllegalArgumentException (
129+ "[" + SEARCH_INFERENCE_ID_FIELD + "] on mapper [" + leafName () + "] of type [" + CONTENT_TYPE + "] must not be empty"
130+ );
131+ }
132+ });
115133
116134 private final Parameter <SemanticTextField .ModelSettings > modelSettings = new Parameter <>(
117- "model_settings" ,
135+ MODEL_SETTINGS_FIELD ,
118136 true ,
119137 () -> null ,
120138 (n , c , o ) -> SemanticTextField .parseModelSettingsFromMap (o ),
@@ -204,6 +222,7 @@ public SemanticTextFieldMapper build(MapperBuilderContext context) {
204222 }
205223 var childContext = context .createChildContext (leafName (), ObjectMapper .Dynamic .FALSE );
206224 final ObjectMapper inferenceField = inferenceFieldBuilder .apply (childContext );
225+
207226 return new SemanticTextFieldMapper (
208227 leafName (),
209228 new SemanticTextFieldType (
0 commit comments