Skip to content

Commit d4accf3

Browse files
authored
[8.18] Backport Semantic Text Mapper Test Fixes (#127943)
1 parent d501e68 commit d4accf3

File tree

3 files changed

+102
-1
lines changed

3 files changed

+102
-1
lines changed

test/framework/src/main/java/org/elasticsearch/index/mapper/MapperServiceTestCase.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,12 @@ protected final MapperService createMapperService(Settings settings, String mapp
207207
return mapperService;
208208
}
209209

210+
protected final MapperService createMapperService(IndexVersion indexVersion, Settings settings, XContentBuilder mappings)
211+
throws IOException {
212+
MapperService mapperService = createMapperService(indexVersion, settings, () -> true, mappings);
213+
return mapperService;
214+
}
215+
210216
protected final MapperService createMapperService(IndexVersion version, XContentBuilder mapping) throws IOException {
211217
return createMapperService(version, getIndexSettings(), () -> true, mapping);
212218
}

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

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,15 @@
99

1010
import org.apache.lucene.index.FieldInfo;
1111
import org.apache.lucene.index.FieldInfos;
12+
import org.elasticsearch.cluster.metadata.IndexMetadata;
1213
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;
1317
import org.elasticsearch.index.mapper.MappedFieldType;
1418
import org.elasticsearch.index.mapper.MapperServiceTestCase;
1519
import org.elasticsearch.plugins.Plugin;
20+
import org.elasticsearch.test.index.IndexVersionUtils;
1621
import org.elasticsearch.xpack.inference.InferencePlugin;
1722

1823
import 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
}

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.apache.lucene.search.join.QueryBitSetProducer;
2525
import org.apache.lucene.search.join.ScoreMode;
2626
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
27+
import org.elasticsearch.cluster.metadata.IndexMetadata;
2728
import org.elasticsearch.common.CheckedBiConsumer;
2829
import org.elasticsearch.common.CheckedBiFunction;
2930
import org.elasticsearch.common.Strings;
@@ -33,6 +34,7 @@
3334
import org.elasticsearch.common.settings.Settings;
3435
import org.elasticsearch.core.CheckedConsumer;
3536
import org.elasticsearch.index.IndexVersion;
37+
import org.elasticsearch.index.IndexVersions;
3638
import org.elasticsearch.index.mapper.DocumentMapper;
3739
import org.elasticsearch.index.mapper.DocumentParsingException;
3840
import org.elasticsearch.index.mapper.FieldMapper;
@@ -61,6 +63,7 @@
6163
import org.elasticsearch.search.LeafNestedDocuments;
6264
import org.elasticsearch.search.NestedDocuments;
6365
import org.elasticsearch.search.SearchHit;
66+
import org.elasticsearch.test.index.IndexVersionUtils;
6467
import org.elasticsearch.xcontent.XContentBuilder;
6568
import org.elasticsearch.xcontent.XContentType;
6669
import org.elasticsearch.xcontent.json.JsonXContent;
@@ -112,10 +115,29 @@ protected Collection<? extends Plugin> getPlugins() {
112115
}
113116

114117
private MapperService createMapperService(XContentBuilder mappings, boolean useLegacyFormat) throws IOException {
118+
IndexVersion indexVersion = SemanticInferenceMetadataFieldsMapperTests.getRandomCompatibleIndexVersion(useLegacyFormat);
119+
return createMapperService(mappings, useLegacyFormat, indexVersion, indexVersion);
120+
}
121+
122+
private MapperService createMapperService(
123+
XContentBuilder mappings,
124+
boolean useLegacyFormat,
125+
IndexVersion minIndexVersion,
126+
IndexVersion maxIndexVersion
127+
) throws IOException {
128+
validateIndexVersion(minIndexVersion, useLegacyFormat);
129+
IndexVersion indexVersion = IndexVersionUtils.randomVersionBetween(random(), minIndexVersion, maxIndexVersion);
115130
var settings = Settings.builder()
131+
.put(IndexMetadata.SETTING_INDEX_VERSION_CREATED.getKey(), indexVersion)
116132
.put(InferenceMetadataFieldsMapper.USE_LEGACY_SEMANTIC_TEXT_FORMAT.getKey(), useLegacyFormat)
117133
.build();
118-
return createMapperService(settings, mappings);
134+
return createMapperService(indexVersion, settings, mappings);
135+
}
136+
137+
private static void validateIndexVersion(IndexVersion indexVersion, boolean useLegacyFormat) {
138+
if (useLegacyFormat == false && indexVersion.before(IndexVersions.INFERENCE_METADATA_FIELDS_BACKPORT)) {
139+
throw new IllegalArgumentException("Index version " + indexVersion + " does not support new semantic text format");
140+
}
119141
}
120142

121143
@Override

0 commit comments

Comments
 (0)