|
34 | 34 | import org.elasticsearch.common.settings.Settings; |
35 | 35 | import org.elasticsearch.core.CheckedConsumer; |
36 | 36 | import org.elasticsearch.index.IndexVersion; |
| 37 | +import org.elasticsearch.index.IndexVersions; |
37 | 38 | import org.elasticsearch.index.mapper.DocumentMapper; |
38 | 39 | import org.elasticsearch.index.mapper.DocumentParsingException; |
39 | 40 | import org.elasticsearch.index.mapper.FieldMapper; |
|
62 | 63 | import org.elasticsearch.search.LeafNestedDocuments; |
63 | 64 | import org.elasticsearch.search.NestedDocuments; |
64 | 65 | import org.elasticsearch.search.SearchHit; |
| 66 | +import org.elasticsearch.test.index.IndexVersionUtils; |
65 | 67 | import org.elasticsearch.xcontent.XContentBuilder; |
66 | 68 | import org.elasticsearch.xcontent.XContentType; |
67 | 69 | import org.elasticsearch.xcontent.json.JsonXContent; |
@@ -113,14 +115,31 @@ protected Collection<? extends Plugin> getPlugins() { |
113 | 115 | } |
114 | 116 |
|
115 | 117 | 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); |
116 | 130 | var settings = Settings.builder() |
117 | | - .put( |
118 | | - IndexMetadata.SETTING_INDEX_VERSION_CREATED.getKey(), |
119 | | - SemanticInferenceMetadataFieldsMapperTests.getRandomCompatibleIndexVersion(useLegacyFormat) |
120 | | - ) |
| 131 | + .put(IndexMetadata.SETTING_INDEX_VERSION_CREATED.getKey(), indexVersion) |
121 | 132 | .put(InferenceMetadataFieldsMapper.USE_LEGACY_SEMANTIC_TEXT_FORMAT.getKey(), useLegacyFormat) |
122 | 133 | .build(); |
123 | | - 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 |
| 139 | + && indexVersion.before(IndexVersions.INFERENCE_METADATA_FIELDS) |
| 140 | + && indexVersion.between(IndexVersions.INFERENCE_METADATA_FIELDS_BACKPORT, IndexVersions.UPGRADE_TO_LUCENE_10_0_0) == false) { |
| 141 | + throw new IllegalArgumentException("Index version " + indexVersion + " does not support new semantic text format"); |
| 142 | + } |
124 | 143 | } |
125 | 144 |
|
126 | 145 | @Override |
|
0 commit comments