|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +package org.elasticsearch.xpack.inference.integration; |
| 9 | + |
| 10 | +import org.elasticsearch.Version; |
| 11 | +import org.elasticsearch.common.settings.Settings; |
| 12 | +import org.elasticsearch.core.TimeValue; |
| 13 | +import org.elasticsearch.index.Index; |
| 14 | +import org.elasticsearch.index.IndexVersion; |
| 15 | +import org.elasticsearch.index.IndexVersions; |
| 16 | +import org.elasticsearch.test.ESIntegTestCase; |
| 17 | +import org.elasticsearch.test.VersionUtils; |
| 18 | +import org.elasticsearch.test.index.IndexVersionUtils; |
| 19 | +import org.junit.Before; |
| 20 | + |
| 21 | +import java.io.IOException; |
| 22 | +import java.util.HashMap; |
| 23 | +import java.util.Map; |
| 24 | +import java.util.List; |
| 25 | +import java.util.Set; |
| 26 | +import java.util.stream.Collectors; |
| 27 | + |
| 28 | +public class SemanticTextIndexVersionIT extends ESIntegTestCase { |
| 29 | + private static final IndexVersion SEMANTIC_TEXT_INTRODUCED_VERSION = IndexVersions.SEMANTIC_TEXT_FIELD_TYPE; |
| 30 | + |
| 31 | + private Set<IndexVersion> availableVersions; |
| 32 | + private static final int MIN_NUMBER_OF_TESTS_TO_RUN = 10; |
| 33 | + |
| 34 | + @Before |
| 35 | + public void setupVersions() { |
| 36 | + availableVersions = IndexVersionUtils.allReleasedVersions().stream() |
| 37 | + .filter((version -> version.onOrAfter(SEMANTIC_TEXT_INTRODUCED_VERSION))) |
| 38 | + .collect(Collectors.toSet()); |
| 39 | + |
| 40 | + logger.info("Available versions for testing: {}", availableVersions); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + protected boolean forbidPrivateIndexSettings() { |
| 45 | + return false; |
| 46 | + } |
| 47 | + |
| 48 | +// /** |
| 49 | +// * Creates an index with a random version from the filtered versions list. |
| 50 | +// * @param indexName The name of the index to create |
| 51 | +// * @return The selected version |
| 52 | +// */ |
| 53 | +// protected Version createRandomVersionIndex(String indexName) throws IOException { |
| 54 | +// Version indexVersion = randomFrom(availableVersions); |
| 55 | +// logger.info("Creating index [{}] with version [{}]", indexName, indexVersion); |
| 56 | +// |
| 57 | +// createIndex(indexName, getIndexSettingsWithVersion(indexVersion)); |
| 58 | +// return indexVersion; |
| 59 | +// } |
| 60 | + |
| 61 | + /** |
| 62 | + * Generate settings for an index with a specific version. |
| 63 | + */ |
| 64 | + private Settings getIndexSettingsWithVersion(IndexVersion version) { |
| 65 | + return Settings.builder() |
| 66 | + .put(indexSettings()) |
| 67 | + .put("index.version.created", version) |
| 68 | + .build(); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Creates a subset of indices with different versions for testing. |
| 73 | + * |
| 74 | + * @return Map of created indices with their versions |
| 75 | + */ |
| 76 | + protected Map<String, IndexVersion> createRandomVersionIndices() throws IOException { |
| 77 | + int versionsCount = Math.min(MIN_NUMBER_OF_TESTS_TO_RUN, availableVersions.size()); |
| 78 | + List<IndexVersion> selectedVersions = randomSubsetOf(versionsCount, availableVersions); |
| 79 | + Map<String, IndexVersion> result = new HashMap<>(); |
| 80 | + |
| 81 | + for (int i = 0; i < selectedVersions.size(); i++) { |
| 82 | + String indexName = "test_semantic" + "_" + i; |
| 83 | + IndexVersion version = selectedVersions.get(i); |
| 84 | + createIndex(indexName, getIndexSettingsWithVersion(version)); |
| 85 | + result.put(indexName, version); |
| 86 | + } |
| 87 | + |
| 88 | + return result; |
| 89 | + } |
| 90 | + |
| 91 | + public void test() throws Exception { |
| 92 | + Map<String, IndexVersion> indices = createRandomVersionIndices(); |
| 93 | + for (String indexName : indices.keySet()) { |
| 94 | + IndexVersion version = indices.get(indexName); |
| 95 | + logger.info("Testing index [{}] with version [{}]", indexName, version); |
| 96 | + assertTrue("Index " + indexName + " should exist", indexExists(indexName)); |
| 97 | + assertEquals("Index version should match", |
| 98 | + version.id(), |
| 99 | + client().admin().indices().prepareGetSettings(TimeValue.THIRTY_SECONDS, indexName) |
| 100 | + .get().getIndexToSettings().get(indexName) |
| 101 | + .getAsVersionId("index.version.created", IndexVersion::fromId).id()); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | +} |
| 106 | + |
| 107 | +//[8.15.3, 8.17.3, 8.15.2, 8.16.6, 8.17.2, 9.0.0, 8.15.1, 8.16.5, 8.17.1, 8.15.0, 8.16.4, 8.17.0, 8.19.0, 8.16.3, 8.16.2, 9.1.0, 8.15.5, 8.16.1, 8.15.4, 8.16.0, 8.17.4, 8.18.0] |
| 108 | +// Available versions for testing: [8512000, 9005000, 9013000, 8520000, 8509000, 8517000, 9000000, 9008000, 8525000, 8514000, 9007000, 9015000, 8522000, 8511000, 8519000, 9002000, 9010000, 8527000, 8508000, 8516000, 9001000, 9009000, 8524000, 8513000, 9004000, 9012000, 8521000, 8510000, 8518000, 9003000, 9011000, 8526000, 8507000, 8515000, 9006000, 9014000, 8523000] |
0 commit comments