-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Fix DenseVectorFieldIndexTypeUpdateIT test #132176
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
carlosdelest
merged 7 commits into
elastic:main
from
carlosdelest:tests/fix-dense-vector-update-tests
Jul 31, 2025
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0cfb0b3
Fix DenseVectorFieldIndexTypeUpdateIT test
carlosdelest 89e2a7c
[CI] Auto commit changes from spotless
3d96ea7
Merge branch 'main' into tests/fix-dense-vector-update-tests
carlosdelest 4b86db8
Merge remote-tracking branch 'origin/main' into tests/fix-dense-vecto…
carlosdelest f794def
Remove more muted tests
carlosdelest a65331f
Merge remote-tracking branch 'carlosdelest/tests/fix-dense-vector-upd…
carlosdelest 4dc8b4b
Fix bad merge
carlosdelest 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,10 +12,14 @@ | |
| import com.carrotsearch.randomizedtesting.annotations.Name; | ||
| import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; | ||
|
|
||
| import org.elasticsearch.action.DocWriteResponse; | ||
| import org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsResponse; | ||
| import org.elasticsearch.action.index.IndexRequest; | ||
| import org.elasticsearch.cluster.metadata.IndexMetadata; | ||
| import org.elasticsearch.common.settings.Settings; | ||
| import org.elasticsearch.rest.RestStatus; | ||
| import org.elasticsearch.search.vectors.KnnVectorQueryBuilder; | ||
| import org.elasticsearch.search.vectors.VectorData; | ||
| import org.elasticsearch.test.ESIntegTestCase; | ||
| import org.elasticsearch.xcontent.XContentBuilder; | ||
| import org.elasticsearch.xcontent.XContentFactory; | ||
|
|
@@ -25,6 +29,7 @@ | |
| import java.util.Collection; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.concurrent.ExecutionException; | ||
|
|
||
| import static org.elasticsearch.index.IndexSettings.INDEX_MAPPER_SOURCE_MODE_SETTING; | ||
| import static org.elasticsearch.index.mapper.SourceFieldMapper.Mode.SYNTHETIC; | ||
|
|
@@ -75,7 +80,7 @@ public static Collection<Object[]> params() { | |
| @SuppressWarnings("unchecked") | ||
| public void testDenseVectorMappingUpdate() throws Exception { | ||
| dimensions = randomIntBetween(1, 10) * 64; | ||
| var client = client().admin().indices(); | ||
| var indicesClient = client().admin().indices(); | ||
|
|
||
| Settings.Builder settingsBuilder = Settings.builder() | ||
| .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) | ||
|
|
@@ -85,7 +90,7 @@ public void testDenseVectorMappingUpdate() throws Exception { | |
| } | ||
|
|
||
| // Create index with initial mapping | ||
| var createRequest = client.prepareCreate(INDEX_NAME) | ||
| var createRequest = indicesClient.prepareCreate(INDEX_NAME) | ||
| .setSettings(Settings.builder().put("index.number_of_shards", randomIntBetween(1, 5))) | ||
| .setMapping(updateMapping(dimensions, initialType)) | ||
| .setSettings(settingsBuilder.build()); | ||
|
|
@@ -97,16 +102,16 @@ public void testDenseVectorMappingUpdate() throws Exception { | |
| indexDoc(i); | ||
| } | ||
|
|
||
| client.prepareFlush(INDEX_NAME).get(); | ||
| client.prepareRefresh(INDEX_NAME).get(); | ||
| indicesClient.prepareFlush(INDEX_NAME).get(); | ||
| indicesClient.prepareRefresh(INDEX_NAME).get(); | ||
|
|
||
| // Update mapping to new type | ||
| var putMappingRequest = client.preparePutMapping(INDEX_NAME).setSource(updateMapping(dimensions, updateType)).request(); | ||
| assertAcked(client.putMapping(putMappingRequest)); | ||
| var putMappingRequest = indicesClient.preparePutMapping(INDEX_NAME).setSource(updateMapping(dimensions, updateType)).request(); | ||
| assertAcked(indicesClient.putMapping(putMappingRequest)); | ||
|
|
||
| // Validate mapping | ||
| GetFieldMappingsResponse fieldMapping = client.getFieldMappings( | ||
| client.prepareGetFieldMappings(INDEX_NAME).setFields(VECTOR_FIELD).request() | ||
| GetFieldMappingsResponse fieldMapping = indicesClient.getFieldMappings( | ||
| indicesClient.prepareGetFieldMappings(INDEX_NAME).setFields(VECTOR_FIELD).request() | ||
| ).get(); | ||
| var fieldMappingMetadata = fieldMapping.fieldMappings(INDEX_NAME, VECTOR_FIELD); | ||
| var fieldMap = (Map<String, Object>) fieldMappingMetadata.sourceAsMap().get(VECTOR_FIELD); | ||
|
|
@@ -119,28 +124,26 @@ public void testDenseVectorMappingUpdate() throws Exception { | |
| indexDoc(i); | ||
| } | ||
|
|
||
| client.prepareFlush(INDEX_NAME).get(); | ||
| client.prepareRefresh(INDEX_NAME).get(); | ||
| indicesClient.prepareFlush(INDEX_NAME).get(); | ||
| indicesClient.prepareRefresh(INDEX_NAME).get(); | ||
|
|
||
| // Search to ensure all documents are present | ||
| int expectedDocs = docsBefore + docsAfter; | ||
| assertNoFailuresAndResponse(client().prepareSearch(INDEX_NAME).setSize(expectedDocs + 10), response -> { | ||
|
|
||
| // Count query | ||
| assertNoFailuresAndResponse(client().prepareSearch(INDEX_NAME).setSize(0).setTrackTotalHits(true).setSize(expectedDocs), response -> { | ||
| assertHitCount(response, expectedDocs); | ||
| }); | ||
| } | ||
|
|
||
| private XContentBuilder initialMapping(int dimensions, String type) throws IOException { | ||
| XContentBuilder builder = XContentFactory.jsonBuilder(); | ||
| builder.startObject(); | ||
| { | ||
| builder.startObject("mappings"); | ||
| { | ||
| createFieldMapping(dimensions, type, builder); | ||
| } | ||
| builder.endObject(); | ||
| // KNN query | ||
| float[] queryVector = new float[dimensions]; | ||
| for (int i = 0; i < queryVector.length; i++) { | ||
| queryVector[i] = randomFloatBetween(-1, 1, true); | ||
| } | ||
| builder.endObject(); | ||
| return builder; | ||
| KnnVectorQueryBuilder queryBuilder = new KnnVectorQueryBuilder(VECTOR_FIELD, queryVector, null, null, null, null); | ||
| assertNoFailuresAndResponse(client().prepareSearch(INDEX_NAME).setQuery(queryBuilder).setTrackTotalHits(true).setSize(expectedDocs), response -> { | ||
| assertHitCount(response, expectedDocs); | ||
| }); | ||
| } | ||
|
|
||
| private XContentBuilder updateMapping(int dimensions, String type) throws IOException { | ||
|
|
@@ -168,9 +171,10 @@ private static void createFieldMapping(int dimensions, String type, XContentBuil | |
| builder.endObject(); | ||
| } | ||
|
|
||
| private void indexDoc(int id) throws IOException { | ||
| private void indexDoc(int id) throws ExecutionException, InterruptedException { | ||
| Float[] vector = randomArray(dimensions, dimensions, Float[]::new, () -> randomFloatBetween(-1, 1, true)); | ||
| IndexRequest req = prepareIndex(INDEX_NAME).setSource(VECTOR_FIELD, vector).setId(Integer.toString(id)).request(); | ||
| client().index(req); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤦 |
||
| IndexRequest indexRequest = prepareIndex(INDEX_NAME).setSource(VECTOR_FIELD, vector).setId(Integer.toString(id)).request(); | ||
| DocWriteResponse indexResponse = client().index(indexRequest).get(); | ||
| assertEquals(RestStatus.CREATED, indexResponse.status()); | ||
| } | ||
| } | ||
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.
Added a count query as well as a knn query to ensure results are consistent