6161import  org .elasticsearch .xcontent .XContentParser ;
6262import  org .elasticsearch .xcontent .XContentType ;
6363import  org .elasticsearch .xcontent .json .JsonXContent ;
64+ import  org .elasticsearch .xpack .inference .DefaultElserFeatureFlag ;
6465import  org .elasticsearch .xpack .inference .InferencePlugin ;
6566import  org .elasticsearch .xpack .inference .model .TestModel ;
6667import  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