Skip to content

Commit c538fe9

Browse files
Adding test in phases
1 parent 802eaa7 commit c538fe9

File tree

1 file changed

+97
-19
lines changed

1 file changed

+97
-19
lines changed

x-pack/plugin/inference/src/internalClusterTest/java/org/elasticsearch/xpack/inference/integration/SemanticTextIndexVersionIT.java

Lines changed: 97 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,49 @@
77

88
package org.elasticsearch.xpack.inference.integration;
99

10-
import org.elasticsearch.Version;
10+
import org.elasticsearch.action.DocWriteResponse;
11+
import org.elasticsearch.action.admin.indices.refresh.RefreshRequest;
12+
import org.elasticsearch.action.search.SearchRequest;
13+
import org.elasticsearch.action.search.SearchResponse;
1114
import org.elasticsearch.common.settings.Settings;
1215
import org.elasticsearch.core.TimeValue;
13-
import org.elasticsearch.index.Index;
1416
import org.elasticsearch.index.IndexVersion;
1517
import org.elasticsearch.index.IndexVersions;
18+
import org.elasticsearch.index.query.QueryBuilders;
19+
import org.elasticsearch.license.LicenseSettings;
20+
import org.elasticsearch.plugins.Plugin;
21+
import org.elasticsearch.search.builder.SearchSourceBuilder;
1622
import org.elasticsearch.test.ESIntegTestCase;
17-
import org.elasticsearch.test.VersionUtils;
1823
import org.elasticsearch.test.index.IndexVersionUtils;
24+
import org.elasticsearch.xcontent.XContentBuilder;
25+
import org.elasticsearch.xcontent.XContentFactory;
26+
import org.elasticsearch.xpack.inference.LocalStateInferencePlugin;
27+
import org.elasticsearch.xpack.inference.Utils;
28+
import org.elasticsearch.xpack.inference.mock.TestSparseInferenceServiceExtension;
29+
import org.elasticsearch.xpack.inference.queries.SemanticQueryBuilder;
1930
import org.junit.Before;
2031

2132
import java.io.IOException;
33+
import java.util.Collection;
2234
import java.util.HashMap;
2335
import java.util.Map;
2436
import java.util.List;
2537
import java.util.Set;
2638
import java.util.stream.Collectors;
2739

40+
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
41+
import static org.hamcrest.Matchers.equalTo;
42+
2843
public class SemanticTextIndexVersionIT extends ESIntegTestCase {
2944
private static final IndexVersion SEMANTIC_TEXT_INTRODUCED_VERSION = IndexVersions.SEMANTIC_TEXT_FIELD_TYPE;
45+
private static final IndexVersion SEMANTIC_TEXT_NEW_FORMAT = IndexVersions.INFERENCE_METADATA_FIELDS_BACKPORT;
3046

3147
private Set<IndexVersion> availableVersions;
3248
private static final int MIN_NUMBER_OF_TESTS_TO_RUN = 10;
3349

3450
@Before
35-
public void setupVersions() {
51+
public void setup() throws Exception {
52+
Utils.storeSparseModel(client());
3653
availableVersions = IndexVersionUtils.allReleasedVersions().stream()
3754
.filter((version -> version.onOrAfter(SEMANTIC_TEXT_INTRODUCED_VERSION)))
3855
.collect(Collectors.toSet());
@@ -45,18 +62,18 @@ protected boolean forbidPrivateIndexSettings() {
4562
return false;
4663
}
4764

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-
// }
65+
@Override
66+
protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) {
67+
return Settings.builder()
68+
.put(otherSettings)
69+
.put(LicenseSettings.SELF_GENERATED_LICENSE_TYPE.getKey(), "trial")
70+
.build();
71+
}
72+
73+
@Override
74+
protected Collection<Class<? extends Plugin>> nodePlugins() {
75+
return List.of(LocalStateInferencePlugin.class);
76+
}
6077

6178
/**
6279
* Generate settings for an index with a specific version.
@@ -93,16 +110,77 @@ public void test() throws Exception {
93110
for (String indexName : indices.keySet()) {
94111
IndexVersion version = indices.get(indexName);
95112
logger.info("Testing index [{}] with version [{}]", indexName, version);
113+
114+
// Test index creation
96115
assertTrue("Index " + indexName + " should exist", indexExists(indexName));
97116
assertEquals("Index version should match",
98117
version.id(),
99118
client().admin().indices().prepareGetSettings(TimeValue.THIRTY_SECONDS, indexName)
100119
.get().getIndexToSettings().get(indexName)
101120
.getAsVersionId("index.version.created", IndexVersion::fromId).id());
121+
122+
// Test update mapping
123+
XContentBuilder mapping = XContentFactory.jsonBuilder()
124+
.startObject()
125+
.startObject("properties")
126+
.startObject("semantic_field")
127+
.field("type", "semantic_text")
128+
.field("inference_id", TestSparseInferenceServiceExtension.TestInferenceService.NAME)
129+
.endObject()
130+
.endObject()
131+
.endObject();
132+
133+
assertAcked(client().admin().indices().preparePutMapping(indexName)
134+
.setSource(mapping)
135+
.get());
136+
137+
// Test data ingestion
138+
String[] text = new String[] {"inference test", "another inference test"};
139+
140+
DocWriteResponse response = client().prepareIndex(indexName).setSource(Map.of("semantic_field", text)).get();
141+
142+
assertEquals("Document should be created", "created", response.getResult().toString().toLowerCase());
143+
144+
client().admin().indices().refresh(new RefreshRequest(indexName)).get();
145+
146+
147+
// Simple search
148+
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().trackTotalHits(true);
149+
SearchResponse searchResponse = client().search(new SearchRequest(indexName).source(sourceBuilder)).get();
150+
try {
151+
assertThat(searchResponse.getHits().getTotalHits().value(), equalTo(1L));
152+
} finally {
153+
searchResponse.decRef();
154+
}
155+
156+
// Search with query
157+
SearchResponse searchWithQueryResponse = null;
158+
if (version.after(SEMANTIC_TEXT_NEW_FORMAT)) {
159+
searchWithQueryResponse = client().search(new SearchRequest(indexName)
160+
.source(sourceBuilder.query(QueryBuilders.matchQuery("semantic_field", "another inference test"))))
161+
.get();
162+
} else {
163+
String semanticQuery = """
164+
{
165+
"semantic": {
166+
"field": "semantic_field",
167+
"query": "inference"
168+
}
169+
}
170+
""";
171+
searchWithQueryResponse = client().search(new SearchRequest(indexName)
172+
.source(sourceBuilder.query(new SemanticQueryBuilder("semantic_field", "inference test"))))
173+
.get();
174+
}
175+
176+
try {
177+
assertThat(searchResponse.getHits().getTotalHits().value(), equalTo(1L));
178+
} finally {
179+
searchResponse.decRef();
180+
}
181+
182+
102183
}
103184
}
104185

105186
}
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

Comments
 (0)