-
Notifications
You must be signed in to change notification settings - Fork 25.6k
[Semantic Text] Integration Test #125141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Samiul-TheSoccerFan
merged 23 commits into
elastic:main
from
Samiul-TheSoccerFan:index-version-compatibility-IT
Apr 1, 2025
Merged
[Semantic Text] Integration Test #125141
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 c538fe9
Adding test in phases
Samiul-TheSoccerFan 8dbde3b
[CI] Auto commit changes from spotless
1b9c4b6
Adding test for search functionality
Samiul-TheSoccerFan 630a36d
Adding test for highlighting
Samiul-TheSoccerFan f505a7e
Adding randomization during selection process
Samiul-TheSoccerFan 34dd779
Fix code styles by running spotlessApply
Samiul-TheSoccerFan 8332e8d
Fix code styles by running spotlessApply
Samiul-TheSoccerFan fac1c30
Merge branch 'main' into index-version-compatibility-IT
elasticmachine af7454b
Merge branch 'main' into index-version-compatibility-IT
elasticmachine 84f7ae5
Fixing forbiddenAPIcall issue
Samiul-TheSoccerFan d7f2ee3
Decoupled namedWritables to use separate fake plugin and simplified o…
Samiul-TheSoccerFan 922081d
Merge branch 'main' into index-version-compatibility-IT
elasticmachine 0dbca60
Updating settings string to variable and removed unused code
Samiul-TheSoccerFan 4dcb691
Fix SemanticQueryBuilder dependencies
Samiul-TheSoccerFan 9739c28
fix setting maximum number of tests to run
Samiul-TheSoccerFan a1263a6
Merge branch 'main' into index-version-compatibility-IT
elasticmachine def0fbf
utilizing semantci_text index version param and removed unwanted over…
Samiul-TheSoccerFan c738358
Merge branch 'main' into index-version-compatibility-IT
elasticmachine 469b97f
Merge branch 'main' into index-version-compatibility-IT
elasticmachine ec51d45
Merge branch 'main' into index-version-compatibility-IT
elasticmachine b218cde
Merge branch 'main' into index-version-compatibility-IT
elasticmachine e41fc78
Merge branch 'main' into index-version-compatibility-IT
elasticmachine File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
180 changes: 180 additions & 0 deletions
180
...erTest/java/org/elasticsearch/xpack/inference/integration/SemanticTextIndexVersionIT.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| 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); | ||
Samiul-TheSoccerFan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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(); | ||
Samiul-TheSoccerFan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /** | ||
| * 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); | ||
Mikep86 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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; | ||
| } | ||
Samiul-TheSoccerFan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * 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) | ||
Samiul-TheSoccerFan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| .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(); | ||
Samiul-TheSoccerFan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
Samiul-TheSoccerFan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| 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); | ||
Samiul-TheSoccerFan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // 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")); | ||
| }); | ||
| } | ||
Samiul-TheSoccerFan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 theSEMANTIC_TEXT_FIELD_TYPE, the index version starts from8.14.4/5and fails to create the semantic_text field.There was a problem hiding this comment.
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_TYPEand the test passes. Perhaps the test failure was related to other problems that you've now fixed?