9
9
10
10
import org .apache .lucene .index .FieldInfo ;
11
11
import org .apache .lucene .index .FieldInfos ;
12
+ import org .elasticsearch .cluster .metadata .IndexMetadata ;
12
13
import org .elasticsearch .common .settings .Settings ;
14
+ import org .elasticsearch .index .IndexVersion ;
15
+ import org .elasticsearch .index .IndexVersions ;
16
+ import org .elasticsearch .index .mapper .InferenceMetadataFieldsMapper ;
13
17
import org .elasticsearch .index .mapper .MappedFieldType ;
14
18
import org .elasticsearch .index .mapper .MapperServiceTestCase ;
15
19
import org .elasticsearch .plugins .Plugin ;
20
+ import org .elasticsearch .test .index .IndexVersionUtils ;
16
21
import org .elasticsearch .xpack .inference .InferencePlugin ;
17
22
18
23
import java .util .Collection ;
@@ -24,6 +29,59 @@ protected Collection<? extends Plugin> getPlugins() {
24
29
return Collections .singletonList (new InferencePlugin (Settings .EMPTY ));
25
30
}
26
31
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
+
27
85
@ Override
28
86
public void testFieldHasValue () {
29
87
assertTrue (
@@ -42,4 +100,19 @@ public void testFieldHasValueWithEmptyFieldInfos() {
42
100
public MappedFieldType getMappedFieldType () {
43
101
return new SemanticInferenceMetadataFieldsMapper .FieldType ();
44
102
}
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
+
45
118
}
0 commit comments