-
Notifications
You must be signed in to change notification settings - Fork 2k
Add missing integration tests for delete by ID API in vector store im… #2209
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
Closed
sobychacko
wants to merge
3
commits into
spring-projects:main
from
sobychacko:vec-store-delete-integ-tests
Closed
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
164 changes: 164 additions & 0 deletions
164
...g-ai-test/src/main/java/org/springframework/ai/test/vectorstore/BaseVectorStoreTests.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,164 @@ | ||
| /* | ||
| * Copyright 2023-2025 the original author or authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.springframework.ai.test.vectorstore; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.awaitility.Awaitility.await; | ||
|
|
||
| import java.time.Duration; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.function.Consumer; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| import org.springframework.ai.document.Document; | ||
| import org.springframework.ai.vectorstore.SearchRequest; | ||
| import org.springframework.ai.vectorstore.VectorStore; | ||
| import org.springframework.ai.vectorstore.filter.Filter; | ||
|
|
||
| /** | ||
| * Base test class for VectorStore implementations. Provides common test scenarios for | ||
| * delete operations. | ||
| * | ||
| * @author Soby Chacko | ||
| */ | ||
| public abstract class BaseVectorStoreTests { | ||
|
|
||
| protected abstract void executeTest(Consumer<VectorStore> testFunction); | ||
|
|
||
| protected Document createDocument(String country, Integer year) { | ||
| Map<String, Object> metadata = new HashMap<>(); | ||
| metadata.put("country", country); | ||
| if (year != null) { | ||
| metadata.put("year", year); | ||
| } | ||
| return new Document("The World is Big and Salvation Lurks Around the Corner", metadata); | ||
| } | ||
|
|
||
| protected List<Document> setupTestDocuments(VectorStore vectorStore) { | ||
| var doc1 = createDocument("BG", 2020); | ||
| var doc2 = createDocument("NL", null); | ||
| var doc3 = createDocument("BG", 2023); | ||
|
|
||
| List<Document> documents = List.of(doc1, doc2, doc3); | ||
| vectorStore.add(documents); | ||
|
|
||
| return documents; | ||
| } | ||
|
|
||
| private String normalizeValue(Object value) { | ||
| return value.toString().replaceAll("^\"|\"$", "").trim(); | ||
| } | ||
|
|
||
| private void verifyDocumentsExist(VectorStore vectorStore, List<Document> documents) { | ||
| await().atMost(5, TimeUnit.SECONDS).pollInterval(Duration.ofMillis(500)).untilAsserted(() -> { | ||
| List<Document> results = vectorStore.similaritySearch( | ||
| SearchRequest.builder().query("The World").topK(documents.size()).similarityThresholdAll().build()); | ||
| assertThat(results).hasSize(documents.size()); | ||
| }); | ||
| } | ||
|
|
||
| private void verifyDocumentsDeleted(VectorStore vectorStore, List<String> deletedIds) { | ||
| await().atMost(5, TimeUnit.SECONDS).pollInterval(Duration.ofMillis(500)).untilAsserted(() -> { | ||
| List<Document> results = vectorStore | ||
| .similaritySearch(SearchRequest.builder().query("The World").topK(10).similarityThresholdAll().build()); | ||
|
|
||
| List<String> foundIds = results.stream().map(Document::getId).collect(Collectors.toList()); | ||
|
|
||
| assertThat(foundIds).doesNotContainAnyElementsOf(deletedIds); | ||
| }); | ||
| } | ||
|
|
||
| @Test | ||
| protected void deleteById() { | ||
| executeTest(vectorStore -> { | ||
| List<Document> documents = setupTestDocuments(vectorStore); | ||
| verifyDocumentsExist(vectorStore, documents); | ||
|
|
||
| List<String> idsToDelete = List.of(documents.get(0).getId(), documents.get(1).getId()); | ||
| vectorStore.delete(idsToDelete); | ||
| verifyDocumentsDeleted(vectorStore, idsToDelete); | ||
|
|
||
| List<Document> results = vectorStore | ||
| .similaritySearch(SearchRequest.builder().query("The World").topK(5).similarityThresholdAll().build()); | ||
|
|
||
| assertThat(results).hasSize(1); | ||
| assertThat(results.get(0).getId()).isEqualTo(documents.get(2).getId()); | ||
| Map<String, Object> metadata = results.get(0).getMetadata(); | ||
| assertThat(normalizeValue(metadata.get("country"))).isEqualTo("BG"); | ||
| assertThat(normalizeValue(metadata.get("year"))).isEqualTo("2023"); | ||
|
|
||
| vectorStore.delete(List.of(documents.get(2).getId())); | ||
| }); | ||
| } | ||
|
|
||
| @Test | ||
| protected void deleteWithStringFilterExpression() { | ||
| executeTest(vectorStore -> { | ||
| List<Document> documents = setupTestDocuments(vectorStore); | ||
| verifyDocumentsExist(vectorStore, documents); | ||
|
|
||
| List<String> bgDocIds = documents.stream() | ||
| .filter(d -> "BG".equals(d.getMetadata().get("country"))) | ||
| .map(Document::getId) | ||
| .collect(Collectors.toList()); | ||
|
|
||
| vectorStore.delete("country == 'BG'"); | ||
| verifyDocumentsDeleted(vectorStore, bgDocIds); | ||
|
|
||
| List<Document> results = vectorStore | ||
| .similaritySearch(SearchRequest.builder().query("The World").topK(5).similarityThresholdAll().build()); | ||
|
|
||
| assertThat(results).hasSize(1); | ||
| assertThat(normalizeValue(results.get(0).getMetadata().get("country"))).isEqualTo("NL"); | ||
|
|
||
| vectorStore.delete(List.of(documents.get(1).getId())); | ||
| }); | ||
| } | ||
|
|
||
| @Test | ||
| protected void deleteByFilter() { | ||
| executeTest(vectorStore -> { | ||
| List<Document> documents = setupTestDocuments(vectorStore); | ||
| verifyDocumentsExist(vectorStore, documents); | ||
|
|
||
| List<String> bgDocIds = documents.stream() | ||
| .filter(d -> "BG".equals(d.getMetadata().get("country"))) | ||
| .map(Document::getId) | ||
| .collect(Collectors.toList()); | ||
|
|
||
| Filter.Expression filterExpression = new Filter.Expression(Filter.ExpressionType.EQ, | ||
| new Filter.Key("country"), new Filter.Value("BG")); | ||
|
|
||
| vectorStore.delete(filterExpression); | ||
| verifyDocumentsDeleted(vectorStore, bgDocIds); | ||
|
|
||
| List<Document> results = vectorStore | ||
| .similaritySearch(SearchRequest.builder().query("The World").topK(5).similarityThresholdAll().build()); | ||
|
|
||
| assertThat(results).hasSize(1); | ||
| assertThat(normalizeValue(results.get(0).getMetadata().get("country"))).isEqualTo("NL"); | ||
|
|
||
| vectorStore.delete(List.of(documents.get(1).getId())); | ||
| }); | ||
| } | ||
|
|
||
| } | ||
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
Oops, something went wrong.
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.
A javadoc here on what to expect for the implementations would be helpful