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,32 @@ 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 (true ))
41+ .put (InferenceMetadataFieldsMapper .USE_LEGACY_SEMANTIC_TEXT_FORMAT .getKey (), false )
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 (), true )
48+ .build ();
49+ assertFalse (InferenceMetadataFieldsMapper .isEnabled (settings ));
50+
51+ settings = Settings .builder ()
52+ .put (IndexMetadata .SETTING_INDEX_VERSION_CREATED .getKey (), getRandomCompatibleIndexVersion (false ))
53+ .put (InferenceMetadataFieldsMapper .USE_LEGACY_SEMANTIC_TEXT_FORMAT .getKey (), false )
54+ .build ();
55+ assertTrue (InferenceMetadataFieldsMapper .isEnabled (settings ));
56+ }
57+
2758 @ Override
2859 public void testFieldHasValue () {
2960 assertTrue (
@@ -42,4 +73,26 @@ public void testFieldHasValueWithEmptyFieldInfos() {
4273 public MappedFieldType getMappedFieldType () {
4374 return new SemanticInferenceMetadataFieldsMapper .FieldType ();
4475 }
76+
77+ static IndexVersion getRandomCompatibleIndexVersion (boolean useLegacyFormat ) {
78+ if (useLegacyFormat ) {
79+ if (randomBoolean ()) {
80+ return IndexVersionUtils .randomVersionBetween (
81+ random (),
82+ IndexVersions .UPGRADE_TO_LUCENE_10_0_0 ,
83+ IndexVersionUtils .getPreviousVersion (IndexVersions .INFERENCE_METADATA_FIELDS )
84+ );
85+ }
86+ return IndexVersionUtils .randomPreviousCompatibleVersion (random (), IndexVersions .INFERENCE_METADATA_FIELDS_BACKPORT );
87+ } else {
88+ if (randomBoolean ()) {
89+ return IndexVersionUtils .randomVersionBetween (random (), IndexVersions .INFERENCE_METADATA_FIELDS , IndexVersion .current ());
90+ }
91+ return IndexVersionUtils .randomVersionBetween (
92+ random (),
93+ IndexVersions .INFERENCE_METADATA_FIELDS_BACKPORT ,
94+ IndexVersions .UPGRADE_TO_LUCENE_10_0_0
95+ );
96+ }
97+ }
4598}
0 commit comments