Skip to content

Commit 4aefdd4

Browse files
committed
Add new index version for inference metadata field
This commit adds the backported index version in elastic#119339 and updates the test to support the new inference metadata field mapper in 8.x.
1 parent dee3c6e commit 4aefdd4

File tree

4 files changed

+65
-2
lines changed

4 files changed

+65
-2
lines changed

server/src/main/java/org/elasticsearch/index/IndexVersions.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ private static Version parseUnchecked(String version) {
133133
public static final IndexVersion V8_DEPRECATE_SOURCE_MODE_MAPPER = def(8_521_00_0, Version.LUCENE_9_12_0);
134134
public static final IndexVersion USE_SYNTHETIC_SOURCE_FOR_RECOVERY_BACKPORT = def(8_522_00_0, Version.LUCENE_9_12_0);
135135
public static final IndexVersion UPGRADE_TO_LUCENE_9_12_1 = def(8_523_00_0, parseUnchecked("9.12.1"));
136+
public static final IndexVersion INFERENCE_METADATA_FIELDS_BACKPORT = def(8_524_00_0, parseUnchecked("9.12.1"));
136137
public static final IndexVersion UPGRADE_TO_LUCENE_10_0_0 = def(9_000_00_0, Version.LUCENE_10_0_0);
137138
public static final IndexVersion LOGSDB_DEFAULT_IGNORE_DYNAMIC_BEYOND_LIMIT = def(9_001_00_0, Version.LUCENE_10_0_0);
138139
public static final IndexVersion TIME_BASED_K_ORDERED_DOC_ID = def(9_002_00_0, Version.LUCENE_10_0_0);

server/src/main/java/org/elasticsearch/index/mapper/InferenceMetadataFieldsMapper.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,12 @@ public abstract ValueFetcher valueFetcher(
8686
* @return {@code true} if the new format is enabled; {@code false} otherwise
8787
*/
8888
public static boolean isEnabled(Settings settings) {
89-
return IndexMetadata.SETTING_INDEX_VERSION_CREATED.get(settings).onOrAfter(IndexVersions.INFERENCE_METADATA_FIELDS)
90-
&& USE_LEGACY_SEMANTIC_TEXT_FORMAT.get(settings) == false;
89+
var version = IndexMetadata.SETTING_INDEX_VERSION_CREATED.get(settings);
90+
if (version.before(IndexVersions.INFERENCE_METADATA_FIELDS)
91+
&& version.between(IndexVersions.INFERENCE_METADATA_FIELDS_BACKPORT, IndexVersions.UPGRADE_TO_LUCENE_10_0_0) == false) {
92+
return false;
93+
}
94+
return USE_LEGACY_SEMANTIC_TEXT_FORMAT.get(settings) == false;
9195
}
9296

9397
/**

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

Lines changed: 53 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,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
}

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

Lines changed: 5 additions & 0 deletions
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;
@@ -112,6 +113,10 @@ protected Collection<? extends Plugin> getPlugins() {
112113

113114
private MapperService createMapperService(XContentBuilder mappings, boolean useLegacyFormat) throws IOException {
114115
var settings = Settings.builder()
116+
.put(
117+
IndexMetadata.SETTING_INDEX_VERSION_CREATED.getKey(),
118+
SemanticInferenceMetadataFieldMapperTests.getRandomCompatibleIndexVersion(useLegacyFormat)
119+
)
115120
.put(InferenceMetadataFieldsMapper.USE_LEGACY_SEMANTIC_TEXT_FORMAT.getKey(), useLegacyFormat)
116121
.build();
117122
return createMapperService(settings, mappings);

0 commit comments

Comments
 (0)