@@ -37,25 +37,44 @@ public void testIsEnabled() {
3737 assertFalse (InferenceMetadataFieldsMapper .isEnabled (settings ));
3838
3939 settings = Settings .builder ()
40- .put (
41- IndexMetadata .SETTING_INDEX_VERSION_CREATED .getKey (),
42- getRandomCompatibleIndexVersion (true , IndexVersionUtils .getPreviousVersion (IndexVersions .INFERENCE_METADATA_FIELDS ))
43- )
44- .put (InferenceMetadataFieldsMapper .USE_LEGACY_SEMANTIC_TEXT_FORMAT .getKey (), false )
40+ .put (IndexMetadata .SETTING_INDEX_VERSION_CREATED .getKey (), getRandomCompatibleIndexVersion (false ))
41+ .put (InferenceMetadataFieldsMapper .USE_LEGACY_SEMANTIC_TEXT_FORMAT .getKey (), true )
4542 .build ();
4643 assertFalse (InferenceMetadataFieldsMapper .isEnabled (settings ));
4744
4845 settings = Settings .builder ()
4946 .put (IndexMetadata .SETTING_INDEX_VERSION_CREATED .getKey (), getRandomCompatibleIndexVersion (false ))
50- .put (InferenceMetadataFieldsMapper .USE_LEGACY_SEMANTIC_TEXT_FORMAT .getKey (), true )
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 )
5163 .build ();
5264 assertFalse (InferenceMetadataFieldsMapper .isEnabled (settings ));
5365
5466 settings = Settings .builder ()
55- .put (IndexMetadata .SETTING_INDEX_VERSION_CREATED .getKey (), getRandomCompatibleIndexVersion (false ))
67+ .put (
68+ IndexMetadata .SETTING_INDEX_VERSION_CREATED .getKey (),
69+ IndexVersionUtils .randomVersionBetween (
70+ random (),
71+ IndexVersions .UPGRADE_TO_LUCENE_10_0_0 ,
72+ IndexVersionUtils .getPreviousVersion (IndexVersions .INFERENCE_METADATA_FIELDS )
73+ ) // 9.x version range prior to the introduction of the new format
74+ )
5675 .put (InferenceMetadataFieldsMapper .USE_LEGACY_SEMANTIC_TEXT_FORMAT .getKey (), false )
5776 .build ();
58- assertTrue (InferenceMetadataFieldsMapper .isEnabled (settings ));
77+ assertFalse (InferenceMetadataFieldsMapper .isEnabled (settings ));
5978 }
6079
6180 public void testIsEnabledByDefault () {
@@ -117,19 +136,27 @@ public MappedFieldType getMappedFieldType() {
117136 }
118137
119138 static IndexVersion getRandomCompatibleIndexVersion (boolean useLegacyFormat ) {
120- return getRandomCompatibleIndexVersion (useLegacyFormat , IndexVersion .current ());
121- }
122-
123- static IndexVersion getRandomCompatibleIndexVersion (boolean useLegacyFormat , IndexVersion maxVersion ) {
124139 if (useLegacyFormat ) {
140+ // Randomly choose an index version compatible with the legacy semantic text format
125141 if (randomBoolean ()) {
126- return IndexVersionUtils .randomVersionBetween (random (), IndexVersions .UPGRADE_TO_LUCENE_10_0_0 , maxVersion );
142+ // 9.x+ version
143+ return IndexVersionUtils .randomVersionBetween (random (), IndexVersions .UPGRADE_TO_LUCENE_10_0_0 , IndexVersion .current ());
127144 }
128- return IndexVersionUtils .randomPreviousCompatibleVersion (random (), IndexVersions .INFERENCE_METADATA_FIELDS_BACKPORT );
145+
146+ // 8.x version
147+ return IndexVersionUtils .randomVersionBetween (
148+ random (),
149+ IndexVersions .SEMANTIC_TEXT_FIELD_TYPE ,
150+ IndexVersionUtils .getPreviousVersion (IndexVersions .UPGRADE_TO_LUCENE_10_0_0 )
151+ );
129152 } else {
153+ // Randomly choose an index version compatible with the new semantic text format
130154 if (randomBoolean ()) {
131- return IndexVersionUtils .randomVersionBetween (random (), IndexVersions .INFERENCE_METADATA_FIELDS , maxVersion );
155+ // 9.x+ version
156+ return IndexVersionUtils .randomVersionBetween (random (), IndexVersions .INFERENCE_METADATA_FIELDS , IndexVersion .current ());
132157 }
158+
159+ // 8.x version
133160 return IndexVersionUtils .randomVersionBetween (
134161 random (),
135162 IndexVersions .INFERENCE_METADATA_FIELDS_BACKPORT ,
0 commit comments