-
Couldn't load subscription status.
- Fork 25.6k
Semantic Text Rolling Upgrade Tests #126548
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
Mikep86
merged 13 commits into
elastic:main
from
Mikep86:semantic-text_rolling-upgrade-tests
May 6, 2025
Merged
Changes from 5 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
5d7d270
Added SemanticTextUpgradeIT
Mikep86 ca9ba1e
Add doc to test index
Mikep86 296d240
Add semantic query tests
Mikep86 4c519dd
Perform highlighting and check query response
Mikep86 b34a73e
Remove forbidden API
Mikep86 1beb1da
Merge branch 'main' into semantic-text_rolling-upgrade-tests
Mikep86 334de43
Make max dimensions configurable
Mikep86 2eb61a8
Add semantic text field that uses dense model to test
Mikep86 b246cba
Query both sparse and dense semantic text fields
Mikep86 d176446
Doc value refactoring
Mikep86 f97dd2f
Clean up query response assertion
Mikep86 503c123
Merge branch 'main' into semantic-text_rolling-upgrade-tests
Mikep86 e623e03
Merge branch 'main' into semantic-text_rolling-upgrade-tests
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
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
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
206 changes: 206 additions & 0 deletions
206
...ck/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/SemanticTextUpgradeIT.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,206 @@ | ||
| /* | ||
| * 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.upgrades; | ||
|
|
||
| import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; | ||
|
|
||
| import org.apache.lucene.search.join.ScoreMode; | ||
| import org.elasticsearch.action.admin.indices.create.CreateIndexResponse; | ||
| import org.elasticsearch.client.Request; | ||
| import org.elasticsearch.client.RequestOptions; | ||
| import org.elasticsearch.client.Response; | ||
| import org.elasticsearch.common.Strings; | ||
| import org.elasticsearch.common.settings.Settings; | ||
| import org.elasticsearch.index.mapper.InferenceMetadataFieldsMapper; | ||
| import org.elasticsearch.index.query.NestedQueryBuilder; | ||
| import org.elasticsearch.inference.Model; | ||
| import org.elasticsearch.inference.TaskType; | ||
| import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder; | ||
| import org.elasticsearch.test.rest.ObjectPath; | ||
| import org.elasticsearch.xcontent.XContentBuilder; | ||
| import org.elasticsearch.xcontent.XContentFactory; | ||
| import org.elasticsearch.xcontent.XContentType; | ||
| import org.elasticsearch.xpack.core.ml.search.SparseVectorQueryBuilder; | ||
| import org.elasticsearch.xpack.core.ml.search.WeightedToken; | ||
| import org.elasticsearch.xpack.inference.mapper.SemanticTextField; | ||
| import org.elasticsearch.xpack.inference.model.TestModel; | ||
| import org.junit.BeforeClass; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Arrays; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
|
|
||
| import static org.elasticsearch.xpack.inference.mapper.SemanticTextFieldMapperTests.addSemanticTextInferenceResults; | ||
| import static org.elasticsearch.xpack.inference.mapper.SemanticTextFieldTests.randomSemanticText; | ||
| import static org.hamcrest.CoreMatchers.equalTo; | ||
| import static org.hamcrest.CoreMatchers.instanceOf; | ||
| import static org.hamcrest.CoreMatchers.notNullValue; | ||
|
|
||
| public class SemanticTextUpgradeIT extends AbstractUpgradeTestCase { | ||
| private static final String INDEX_BASE_NAME = "semantic_text_test_index"; | ||
| private static final String SEMANTIC_TEXT_FIELD = "semantic_field"; | ||
|
|
||
| private static Model SPARSE_MODEL; | ||
|
|
||
| private final boolean useLegacyFormat; | ||
|
|
||
| @BeforeClass | ||
| public static void beforeClass() { | ||
| SPARSE_MODEL = TestModel.createRandomInstance(TaskType.SPARSE_EMBEDDING); | ||
| } | ||
|
|
||
| public SemanticTextUpgradeIT(boolean useLegacyFormat) { | ||
| this.useLegacyFormat = useLegacyFormat; | ||
| } | ||
|
|
||
| @ParametersFactory | ||
| public static Iterable<Object[]> parameters() { | ||
| return List.of(new Object[] { true }, new Object[] { false }); | ||
| } | ||
|
|
||
| public void testSemanticTextOperations() throws Exception { | ||
| switch (CLUSTER_TYPE) { | ||
| case OLD -> createAndPopulateIndex(); | ||
| case MIXED, UPGRADED -> performIndexQueryHighlightOps(); | ||
| default -> throw new UnsupportedOperationException("Unknown cluster type [" + CLUSTER_TYPE + "]"); | ||
| } | ||
| } | ||
|
|
||
| private void createAndPopulateIndex() throws IOException { | ||
| final String indexName = getIndexName(); | ||
| final String mapping = Strings.format(""" | ||
| { | ||
| "properties": { | ||
| "%s": { | ||
| "type": "semantic_text", | ||
| "inference_id": "%s" | ||
| } | ||
| } | ||
| } | ||
| """, SEMANTIC_TEXT_FIELD, SPARSE_MODEL.getInferenceEntityId()); | ||
|
|
||
| CreateIndexResponse response = createIndex( | ||
| indexName, | ||
| Settings.builder().put(InferenceMetadataFieldsMapper.USE_LEGACY_SEMANTIC_TEXT_FORMAT.getKey(), useLegacyFormat).build(), | ||
| mapping | ||
| ); | ||
| assertThat(response.isAcknowledged(), equalTo(true)); | ||
|
|
||
| indexDoc("doc_1", List.of("a test value", "with multiple test values")); | ||
| } | ||
|
|
||
| private void performIndexQueryHighlightOps() throws IOException { | ||
| indexDoc("doc_2", List.of("another test value")); | ||
| ObjectPath queryObjectPath = semanticQuery("test value", 3); | ||
| assertQueryResponse(queryObjectPath); | ||
| } | ||
|
|
||
| private String getIndexName() { | ||
| return INDEX_BASE_NAME + (useLegacyFormat ? "_legacy" : "_new"); | ||
| } | ||
|
|
||
| private void indexDoc(String id, List<String> semanticTextFieldValue) throws IOException { | ||
| final String indexName = getIndexName(); | ||
| final SemanticTextField semanticTextField = randomSemanticText( | ||
| useLegacyFormat, | ||
| SEMANTIC_TEXT_FIELD, | ||
| SPARSE_MODEL, | ||
| null, | ||
| semanticTextFieldValue, | ||
| XContentType.JSON | ||
| ); | ||
|
|
||
| XContentBuilder builder = XContentFactory.jsonBuilder(); | ||
| builder.startObject(); | ||
| if (useLegacyFormat == false) { | ||
| builder.field(semanticTextField.fieldName(), semanticTextFieldValue); | ||
| } | ||
| addSemanticTextInferenceResults(useLegacyFormat, builder, List.of(semanticTextField)); | ||
| builder.endObject(); | ||
|
|
||
| RequestOptions requestOptions = RequestOptions.DEFAULT.toBuilder().addParameter("refresh", "true").build(); | ||
| Request request = new Request("POST", indexName + "/_doc/" + id); | ||
| request.setJsonEntity(Strings.toString(builder)); | ||
| request.setOptions(requestOptions); | ||
|
|
||
| Response response = client().performRequest(request); | ||
| assertOK(response); | ||
| } | ||
|
|
||
| private ObjectPath semanticQuery(String query, Integer numOfHighlightFragments) throws IOException { | ||
| // We can't perform a real semantic query because that requires performing inference, so instead we perform an equivalent nested | ||
| // query | ||
| List<WeightedToken> weightedTokens = Arrays.stream(query.split("\\s")).map(t -> new WeightedToken(t, 1.0f)).toList(); | ||
| SparseVectorQueryBuilder sparseVectorQueryBuilder = new SparseVectorQueryBuilder( | ||
| SemanticTextField.getEmbeddingsFieldName(SEMANTIC_TEXT_FIELD), | ||
| weightedTokens, | ||
| null, | ||
| null, | ||
| null, | ||
| null | ||
| ); | ||
| NestedQueryBuilder nestedQueryBuilder = new NestedQueryBuilder( | ||
| SemanticTextField.getChunksFieldName(SEMANTIC_TEXT_FIELD), | ||
| sparseVectorQueryBuilder, | ||
| ScoreMode.Max | ||
| ); | ||
|
|
||
| XContentBuilder builder = XContentFactory.jsonBuilder(); | ||
| builder.startObject(); | ||
| builder.field("query", nestedQueryBuilder); | ||
| if (numOfHighlightFragments != null) { | ||
| HighlightBuilder.Field highlightField = new HighlightBuilder.Field(SEMANTIC_TEXT_FIELD); | ||
| highlightField.numOfFragments(numOfHighlightFragments); | ||
|
|
||
| HighlightBuilder highlightBuilder = new HighlightBuilder(); | ||
| highlightBuilder.field(highlightField); | ||
|
|
||
| builder.field("highlight", highlightBuilder); | ||
| } | ||
| builder.endObject(); | ||
|
|
||
| Request request = new Request("GET", getIndexName() + "/_search"); | ||
| request.setJsonEntity(Strings.toString(builder)); | ||
|
|
||
| Response response = client().performRequest(request); | ||
| return assertOKAndCreateObjectPath(response); | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| private static void assertQueryResponse(ObjectPath queryObjectPath) throws IOException { | ||
Mikep86 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| final Map<String, List<String>> expectedHighlights = Map.of( | ||
| "doc_1", | ||
| List.of("a test value", "with multiple test values"), | ||
| "doc_2", | ||
| List.of("another test value") | ||
| ); | ||
|
|
||
| assertThat(queryObjectPath.evaluate("hits.total.value"), equalTo(2)); | ||
| assertThat(queryObjectPath.evaluateArraySize("hits.hits"), equalTo(2)); | ||
|
|
||
| Set<String> docIds = new HashSet<>(); | ||
| List<Object> hits = queryObjectPath.evaluate("hits.hits"); | ||
| for (Object hit : hits) { | ||
| assertThat(hit, instanceOf(Map.class)); | ||
| Map<String, Object> hitMap = (Map<String, Object>) hit; | ||
|
|
||
| String id = (String) hitMap.get("_id"); | ||
| assertThat(id, notNullValue()); | ||
| docIds.add(id); | ||
|
|
||
| List<String> expectedHighlight = expectedHighlights.get(id); | ||
| assertThat(expectedHighlight, notNullValue()); | ||
| assertThat(((Map<String, Object>) hitMap.get("highlight")).get(SEMANTIC_TEXT_FIELD), equalTo(expectedHighlight)); | ||
| } | ||
Mikep86 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| assertThat(docIds, equalTo(Set.of("doc_1", "doc_2"))); | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.