Skip to content

Commit f84716a

Browse files
committed
Backport getRandomCompatibleIndexVersion changes
1 parent 18eef3f commit f84716a

File tree

1 file changed

+41
-11
lines changed

1 file changed

+41
-11
lines changed

x-pack/plugin/inference/src/test/java/org/elasticsearch/xpack/inference/mapper/SemanticInferenceMetadataFieldsMapperTests.java

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,44 @@ public void testIsEnabled() {
3737
assertFalse(InferenceMetadataFieldsMapper.isEnabled(settings));
3838

3939
settings = Settings.builder()
40-
.put(IndexMetadata.SETTING_INDEX_VERSION_CREATED.getKey(), getRandomCompatibleIndexVersion(true))
41-
.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)
4242
.build();
4343
assertFalse(InferenceMetadataFieldsMapper.isEnabled(settings));
4444

4545
settings = Settings.builder()
4646
.put(IndexMetadata.SETTING_INDEX_VERSION_CREATED.getKey(), getRandomCompatibleIndexVersion(false))
47-
.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)
4863
.build();
4964
assertFalse(InferenceMetadataFieldsMapper.isEnabled(settings));
5065

5166
settings = Settings.builder()
52-
.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+
)
5375
.put(InferenceMetadataFieldsMapper.USE_LEGACY_SEMANTIC_TEXT_FORMAT.getKey(), false)
5476
.build();
55-
assertTrue(InferenceMetadataFieldsMapper.isEnabled(settings));
77+
assertFalse(InferenceMetadataFieldsMapper.isEnabled(settings));
5678
}
5779

5880
public void testIsEnabledByDefault() {
@@ -115,18 +137,26 @@ public MappedFieldType getMappedFieldType() {
115137

116138
static IndexVersion getRandomCompatibleIndexVersion(boolean useLegacyFormat) {
117139
if (useLegacyFormat) {
140+
// Randomly choose an index version compatible with the legacy semantic text format
118141
if (randomBoolean()) {
119-
return IndexVersionUtils.randomVersionBetween(
120-
random(),
121-
IndexVersions.UPGRADE_TO_LUCENE_10_0_0,
122-
IndexVersionUtils.getPreviousVersion(IndexVersions.INFERENCE_METADATA_FIELDS)
123-
);
142+
// 9.x+ version
143+
return IndexVersionUtils.randomVersionBetween(random(), IndexVersions.UPGRADE_TO_LUCENE_10_0_0, IndexVersion.current());
124144
}
125-
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+
);
126152
} else {
153+
// Randomly choose an index version compatible with the new semantic text format
127154
if (randomBoolean()) {
155+
// 9.x+ version
128156
return IndexVersionUtils.randomVersionBetween(random(), IndexVersions.INFERENCE_METADATA_FIELDS, IndexVersion.current());
129157
}
158+
159+
// 8.x version
130160
return IndexVersionUtils.randomVersionBetween(
131161
random(),
132162
IndexVersions.INFERENCE_METADATA_FIELDS_BACKPORT,

0 commit comments

Comments
 (0)