Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
802eaa7
Initial draft test with index version setup
Samiul-TheSoccerFan Mar 17, 2025
c538fe9
Adding test in phases
Samiul-TheSoccerFan Mar 18, 2025
8dbde3b
[CI] Auto commit changes from spotless
Mar 18, 2025
1b9c4b6
Adding test for search functionality
Samiul-TheSoccerFan Mar 21, 2025
630a36d
Adding test for highlighting
Samiul-TheSoccerFan Mar 24, 2025
f505a7e
Adding randomization during selection process
Samiul-TheSoccerFan Mar 24, 2025
34dd779
Fix code styles by running spotlessApply
Samiul-TheSoccerFan Mar 24, 2025
8332e8d
Fix code styles by running spotlessApply
Samiul-TheSoccerFan Mar 24, 2025
fac1c30
Merge branch 'main' into index-version-compatibility-IT
elasticmachine Mar 25, 2025
af7454b
Merge branch 'main' into index-version-compatibility-IT
elasticmachine Mar 25, 2025
84f7ae5
Fixing forbiddenAPIcall issue
Samiul-TheSoccerFan Mar 25, 2025
d7f2ee3
Decoupled namedWritables to use separate fake plugin and simplified o…
Samiul-TheSoccerFan Mar 26, 2025
922081d
Merge branch 'main' into index-version-compatibility-IT
elasticmachine Mar 27, 2025
0dbca60
Updating settings string to variable and removed unused code
Samiul-TheSoccerFan Mar 27, 2025
4dcb691
Fix SemanticQueryBuilder dependencies
Samiul-TheSoccerFan Mar 27, 2025
9739c28
fix setting maximum number of tests to run
Samiul-TheSoccerFan Mar 27, 2025
a1263a6
Merge branch 'main' into index-version-compatibility-IT
elasticmachine Mar 27, 2025
def0fbf
utilizing semantci_text index version param and removed unwanted over…
Samiul-TheSoccerFan Mar 28, 2025
c738358
Merge branch 'main' into index-version-compatibility-IT
elasticmachine Mar 28, 2025
469b97f
Merge branch 'main' into index-version-compatibility-IT
elasticmachine Mar 28, 2025
ec51d45
Merge branch 'main' into index-version-compatibility-IT
elasticmachine Mar 28, 2025
b218cde
Merge branch 'main' into index-version-compatibility-IT
elasticmachine Mar 30, 2025
e41fc78
Merge branch 'main' into index-version-compatibility-IT
elasticmachine Apr 1, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

package org.elasticsearch.xpack.inference.integration;

import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.action.admin.indices.refresh.RefreshRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.core.TimeValue;
import org.elasticsearch.index.IndexVersion;
import org.elasticsearch.index.mapper.InferenceMetadataFieldsMapper;
import org.elasticsearch.license.LicenseSettings;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.test.ESIntegTestCase;
import org.elasticsearch.test.index.IndexVersionUtils;
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xcontent.XContentFactory;
import org.elasticsearch.xpack.inference.LocalStateInferencePlugin;
import org.elasticsearch.xpack.inference.Utils;
import org.elasticsearch.xpack.inference.mock.TestSparseInferenceServiceExtension;
import org.elasticsearch.xpack.inference.queries.SemanticQueryBuilder;
import org.elasticsearch.xpack.inference.registry.ModelRegistry;
import org.junit.Before;

import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHighlight;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertResponse;
import static org.hamcrest.Matchers.equalTo;

public class SemanticTextIndexVersionIT extends ESIntegTestCase {
private static final IndexVersion SEMANTIC_TEXT_INTRODUCED_VERSION = IndexVersion.fromId(8512000);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This index version starts from 8.15. If we use the SEMANTIC_TEXT_FIELD_TYPE, the index version starts from 8.14.4/5 and fails to create the semantic_text field.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just tried starting from SEMANTIC_TEXT_FIELD_TYPE and the test passes. Perhaps the test failure was related to other problems that you've now fixed?

private static final double PERCENTAGE_TO_TEST = 0.5;
private Set<IndexVersion> availableVersions;

@Before
public void setup() throws Exception {
ModelRegistry modelRegistry = internalCluster().getCurrentMasterNodeInstance(ModelRegistry.class);
Utils.storeSparseModel(modelRegistry);
availableVersions = IndexVersionUtils.allReleasedVersions()
.stream()
.filter(indexVersion -> indexVersion.after(SEMANTIC_TEXT_INTRODUCED_VERSION))
.collect(Collectors.toSet());
}

@Override
protected boolean forbidPrivateIndexSettings() {
return false;
}

@Override
protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) {
return Settings.builder().put(otherSettings).put(LicenseSettings.SELF_GENERATED_LICENSE_TYPE.getKey(), "trial").build();
}

@Override
protected Collection<Class<? extends Plugin>> nodePlugins() {
return List.of(LocalStateInferencePlugin.class);
}

/**
* Generate settings for an index with a specific version.
*/
private Settings getIndexSettingsWithVersion(IndexVersion version) {
return Settings.builder().put(indexSettings()).put("index.version.created", version).build();
}

/**
* Creates a subset of indices with different versions for testing.
*
* @return Map of created indices with their versions
*/
protected Map<String, IndexVersion> createRandomVersionIndices() throws IOException {
int versionsCount = (int) Math.ceil(availableVersions.size() * PERCENTAGE_TO_TEST);
List<IndexVersion> selectedVersions = randomSubsetOf(versionsCount, availableVersions);
Map<String, IndexVersion> result = new HashMap<>();

for (int i = 0; i < selectedVersions.size(); i++) {
String indexName = "test_semantic" + "_" + i;
IndexVersion version = selectedVersions.get(i);
createIndex(indexName, getIndexSettingsWithVersion(version));
result.put(indexName, version);
}

return result;
}

/**
* This test creates an index, ingests data, and performs searches (including highlighting when applicable)
* for a selected subset of index versions.
*/
public void testSemanticText() throws Exception {
Map<String, IndexVersion> indices = createRandomVersionIndices();
for (String indexName : indices.keySet()) {
IndexVersion version = indices.get(indexName);

// Test index creation
assertTrue("Index " + indexName + " should exist", indexExists(indexName));
assertEquals(
"Index version should match",
version.id(),
client().admin()
.indices()
.prepareGetSettings(TimeValue.THIRTY_SECONDS, indexName)
.get()
.getIndexToSettings()
.get(indexName)
.getAsVersionId("index.version.created", IndexVersion::fromId)
.id()
);

// Test update mapping
XContentBuilder mapping = XContentFactory.jsonBuilder()
.startObject()
.startObject("properties")
.startObject("semantic_field")
.field("type", "semantic_text")
.field("inference_id", TestSparseInferenceServiceExtension.TestInferenceService.NAME)
.endObject()
.endObject()
.endObject();

assertAcked(client().admin().indices().preparePutMapping(indexName).setSource(mapping).get());

// Test data ingestion
String[] text = new String[] { "inference test", "another inference test" };
DocWriteResponse docWriteResponse = client().prepareIndex(indexName).setSource(Map.of("semantic_field", text)).get();

assertEquals("Document should be created", "created", docWriteResponse.getResult().toString().toLowerCase(Locale.ROOT));

// Ensure index is ready
client().admin().indices().refresh(new RefreshRequest(indexName)).get();
ensureGreen(indexName);

// Semantic Search
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().query(new SemanticQueryBuilder("semantic_field", "inference"))
.trackTotalHits(true);

assertResponse(
client().search(new SearchRequest(indexName).source(sourceBuilder)),
response -> { assertHitCount(response, 1L); }
);

Settings settings = client().admin()
.indices()
.prepareGetSettings(TimeValue.THIRTY_SECONDS, indexName)
.get()
.getIndexToSettings()
.get(indexName);

// Semantic Search with highlighter only available from 8.18 and 9.0
if (InferenceMetadataFieldsMapper.isEnabled(settings)) {
SearchSourceBuilder sourceHighlighterBuilder = new SearchSourceBuilder().query(
new SemanticQueryBuilder("semantic_field", "inference")
).highlighter(new HighlightBuilder().field("semantic_field")).trackTotalHits(true);

assertResponse(client().search(new SearchRequest(indexName).source(sourceHighlighterBuilder)), response -> {
assertHighlight(response, 0, "semantic_field", 0, 2, equalTo("inference test"));
assertHighlight(response, 0, "semantic_field", 1, 2, equalTo("another inference test"));
});
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,24 @@
package org.elasticsearch.xpack.inference;

import org.elasticsearch.action.support.MappedActionFilter;
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.mapper.Mapper;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.inference.InferenceServiceExtension;
import org.elasticsearch.license.XPackLicenseState;
import org.elasticsearch.plugins.SearchPlugin;
import org.elasticsearch.search.fetch.subphase.highlight.Highlighter;
import org.elasticsearch.xpack.core.LocalStateCompositeXPackPlugin;
import org.elasticsearch.xpack.core.ml.inference.MlInferenceNamedXContentProvider;
import org.elasticsearch.xpack.core.ml.search.SparseVectorQueryBuilder;
import org.elasticsearch.xpack.core.ssl.SSLService;
import org.elasticsearch.xpack.inference.highlight.SemanticTextHighlighter;
import org.elasticsearch.xpack.inference.mock.TestDenseInferenceServiceExtension;
import org.elasticsearch.xpack.inference.mock.TestSparseInferenceServiceExtension;

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -63,8 +70,29 @@ public Map<String, Mapper.TypeParser> getMappers() {
return inferencePlugin.getMappers();
}

@Override
public Map<String, Highlighter> getHighlighters() {
return Map.of(SemanticTextHighlighter.NAME, new SemanticTextHighlighter());
}

@Override
public Collection<MappedActionFilter> getMappedActionFilters() {
return inferencePlugin.getMappedActionFilters();
}

@Override
public List<QuerySpec<?>> getQueries() {
return inferencePlugin.getQueries();
}

@Override
public List<NamedWriteableRegistry.Entry> getNamedWriteables() {
List<NamedWriteableRegistry.Entry> namedWriteables = new ArrayList<>(super.getNamedWriteables());
namedWriteables.addAll(new MlInferenceNamedXContentProvider().getNamedWriteables());
namedWriteables.add(
new NamedWriteableRegistry.Entry(QueryBuilder.class, SparseVectorQueryBuilder.NAME, SparseVectorQueryBuilder::new)
);

return namedWriteables;
}
}