99
1010import org .apache .lucene .index .FieldInfo ;
1111import org .apache .lucene .index .FieldInfos ;
12+ import org .elasticsearch .cluster .metadata .IndexMetadata ;
1213import org .elasticsearch .common .settings .Settings ;
14+ import org .elasticsearch .index .IndexVersion ;
15+ import org .elasticsearch .index .IndexVersions ;
16+ import org .elasticsearch .index .mapper .InferenceMetadataFieldsMapper ;
1317import org .elasticsearch .index .mapper .MappedFieldType ;
1418import org .elasticsearch .index .mapper .MapperServiceTestCase ;
1519import org .elasticsearch .plugins .Plugin ;
20+ import org .elasticsearch .test .index .IndexVersionUtils ;
1621import org .elasticsearch .xpack .inference .InferencePlugin ;
1722
1823import java .util .Collection ;
@@ -24,6 +29,59 @@ protected Collection<? extends Plugin> getPlugins() {
2429 return Collections .singletonList (new InferencePlugin (Settings .EMPTY ));
2530 }
2631
32+ public void testIsEnabled () {
33+ var settings = Settings .builder ()
34+ .put (IndexMetadata .SETTING_INDEX_VERSION_CREATED .getKey (), getRandomCompatibleIndexVersion (true ))
35+ .put (InferenceMetadataFieldsMapper .USE_LEGACY_SEMANTIC_TEXT_FORMAT .getKey (), true )
36+ .build ();
37+ assertFalse (InferenceMetadataFieldsMapper .isEnabled (settings ));
38+
39+ settings = Settings .builder ()
40+ .put (IndexMetadata .SETTING_INDEX_VERSION_CREATED .getKey (), getRandomCompatibleIndexVersion (false ))
41+ .put (InferenceMetadataFieldsMapper .USE_LEGACY_SEMANTIC_TEXT_FORMAT .getKey (), true )
42+ .build ();
43+ assertFalse (InferenceMetadataFieldsMapper .isEnabled (settings ));
44+
45+ settings = Settings .builder ()
46+ .put (IndexMetadata .SETTING_INDEX_VERSION_CREATED .getKey (), getRandomCompatibleIndexVersion (false ))
47+ .put (InferenceMetadataFieldsMapper .USE_LEGACY_SEMANTIC_TEXT_FORMAT .getKey (), false )
48+ .build ();
49+ assertTrue (InferenceMetadataFieldsMapper .isEnabled (settings ));
50+
51+ // Test that index.mapping.semantic_text.use_legacy_format == false is ignored when the index version is too old to support the new
52+ // format
53+ settings = Settings .builder ()
54+ .put (
55+ IndexMetadata .SETTING_INDEX_VERSION_CREATED .getKey (),
56+ IndexVersionUtils .randomVersionBetween (
57+ random (),
58+ IndexVersions .SEMANTIC_TEXT_FIELD_TYPE ,
59+ IndexVersionUtils .getPreviousVersion (IndexVersions .INFERENCE_METADATA_FIELDS_BACKPORT )
60+ ) // 8.x version range prior to the introduction of the new format
61+ )
62+ .put (InferenceMetadataFieldsMapper .USE_LEGACY_SEMANTIC_TEXT_FORMAT .getKey (), false )
63+ .build ();
64+ assertFalse (InferenceMetadataFieldsMapper .isEnabled (settings ));
65+ }
66+
67+ public void testIsEnabledByDefault () {
68+ var settings = Settings .builder ()
69+ .put (
70+ IndexMetadata .SETTING_INDEX_VERSION_CREATED .getKey (),
71+ IndexVersionUtils .randomPreviousCompatibleVersion (random (), IndexVersions .INFERENCE_METADATA_FIELDS_BACKPORT )
72+ )
73+ .build ();
74+ assertFalse (InferenceMetadataFieldsMapper .isEnabled (settings ));
75+
76+ settings = Settings .builder ()
77+ .put (
78+ IndexMetadata .SETTING_INDEX_VERSION_CREATED .getKey (),
79+ IndexVersionUtils .randomVersionBetween (random (), IndexVersions .INFERENCE_METADATA_FIELDS_BACKPORT , IndexVersion .current ())
80+ )
81+ .build ();
82+ assertTrue (InferenceMetadataFieldsMapper .isEnabled (settings ));
83+ }
84+
2785 @ Override
2886 public void testFieldHasValue () {
2987 assertTrue (
@@ -42,4 +100,19 @@ public void testFieldHasValueWithEmptyFieldInfos() {
42100 public MappedFieldType getMappedFieldType () {
43101 return new SemanticInferenceMetadataFieldsMapper .FieldType ();
44102 }
103+
104+ static IndexVersion getRandomCompatibleIndexVersion (boolean useLegacyFormat ) {
105+ if (useLegacyFormat ) {
106+ // Randomly choose an index version compatible with the legacy semantic text format
107+ return IndexVersionUtils .randomVersionBetween (random (), IndexVersions .SEMANTIC_TEXT_FIELD_TYPE , IndexVersion .current ());
108+ } else {
109+ // Randomly choose an index version compatible with the new semantic text format
110+ return IndexVersionUtils .randomVersionBetween (
111+ random (),
112+ IndexVersions .INFERENCE_METADATA_FIELDS_BACKPORT ,
113+ IndexVersion .current ()
114+ );
115+ }
116+ }
117+
45118}
0 commit comments