diff --git a/spring-ai-core/src/main/java/org/springframework/ai/vectorstore/VectorStore.java b/spring-ai-core/src/main/java/org/springframework/ai/vectorstore/VectorStore.java index d98bdfd94d3..2b1bc863d5d 100644 --- a/spring-ai-core/src/main/java/org/springframework/ai/vectorstore/VectorStore.java +++ b/spring-ai-core/src/main/java/org/springframework/ai/vectorstore/VectorStore.java @@ -1,5 +1,5 @@ /* - * Copyright 2023-2024 the original author or authors. + * 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. @@ -24,9 +24,11 @@ import org.springframework.ai.document.Document; import org.springframework.ai.document.DocumentWriter; import org.springframework.ai.embedding.BatchingStrategy; +import org.springframework.ai.vectorstore.filter.Filter; import org.springframework.ai.vectorstore.observation.DefaultVectorStoreObservationConvention; import org.springframework.ai.vectorstore.observation.VectorStoreObservationConvention; import org.springframework.lang.Nullable; +import org.springframework.util.Assert; /** * The {@code VectorStore} interface defines the operations for managing and querying @@ -62,6 +64,28 @@ default void accept(List documents) { @Nullable Optional delete(List idList); + /** + * Deletes documents from the vector store based on filter criteria. + * @param filterExpression Filter expression to identify documents to delete + * @throws IllegalStateException if the underlying delete causes an exception + */ + void delete(Filter.Expression filterExpression); + + /** + * Deletes documents from the vector store using a string filter expression. Converts + * the string filter to an Expression object and delegates to + * {@link #delete(Filter.Expression)}. + * @param filterExpression String representation of the filter criteria + * @throws IllegalArgumentException if the filter expression is null + * @throws IllegalStateException if the underlying delete causes an exception + */ + default void delete(String filterExpression) { + SearchRequest searchRequest = SearchRequest.builder().filterExpression(filterExpression).build(); + Filter.Expression textExpression = searchRequest.getFilterExpression(); + Assert.notNull(textExpression, "Filter expression must not be null"); + this.delete(textExpression); + } + /** * Retrieves documents by query embedding similarity and metadata filters to retrieve * exactly the number of nearest-neighbor results that match the request criteria. diff --git a/spring-ai-core/src/main/java/org/springframework/ai/vectorstore/observation/AbstractObservationVectorStore.java b/spring-ai-core/src/main/java/org/springframework/ai/vectorstore/observation/AbstractObservationVectorStore.java index da3f23058df..a2a4291e24b 100644 --- a/spring-ai-core/src/main/java/org/springframework/ai/vectorstore/observation/AbstractObservationVectorStore.java +++ b/spring-ai-core/src/main/java/org/springframework/ai/vectorstore/observation/AbstractObservationVectorStore.java @@ -1,5 +1,5 @@ /* - * Copyright 2023-2024 the original author or authors. + * 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. @@ -18,6 +18,7 @@ import java.util.List; import java.util.Optional; +import java.util.stream.Collectors; import io.micrometer.observation.ObservationRegistry; @@ -27,6 +28,7 @@ import org.springframework.ai.vectorstore.AbstractVectorStoreBuilder; import org.springframework.ai.vectorstore.SearchRequest; import org.springframework.ai.vectorstore.VectorStore; +import org.springframework.ai.vectorstore.filter.Filter; import org.springframework.lang.Nullable; /** @@ -99,6 +101,18 @@ public Optional delete(List deleteDocIds) { .observe(() -> this.doDelete(deleteDocIds)); } + @Override + public void delete(Filter.Expression filterExpression) { + VectorStoreObservationContext observationContext = this + .createObservationContextBuilder(VectorStoreObservationContext.Operation.DELETE.value()) + .build(); + + VectorStoreObservationDocumentation.AI_VECTOR_STORE + .observation(this.customObservationConvention, DEFAULT_OBSERVATION_CONVENTION, () -> observationContext, + this.observationRegistry) + .observe(() -> this.doDelete(filterExpression)); + } + @Override @Nullable public List similaritySearch(SearchRequest request) { @@ -131,6 +145,19 @@ public List similaritySearch(SearchRequest request) { */ public abstract Optional doDelete(List idList); + /** + * Template method for concrete implementations to provide filter-based deletion + * logic. + * @param filterExpression Filter expression to identify documents to delete + */ + @Nullable + protected void doDelete(Filter.Expression filterExpression) { + // this is temporary until we implement this method in all concrete vector stores, + // at which point + // this method will become an abstract method. + throw new UnsupportedOperationException(); + } + /** * Perform the actual similarity search operation. * @param request the search request diff --git a/vector-stores/spring-ai-chroma-store/src/main/java/org/springframework/ai/chroma/vectorstore/ChromaApi.java b/vector-stores/spring-ai-chroma-store/src/main/java/org/springframework/ai/chroma/vectorstore/ChromaApi.java index 04c12129dc6..a4d15249ab5 100644 --- a/vector-stores/spring-ai-chroma-store/src/main/java/org/springframework/ai/chroma/vectorstore/ChromaApi.java +++ b/vector-stores/spring-ai-chroma-store/src/main/java/org/springframework/ai/chroma/vectorstore/ChromaApi.java @@ -338,7 +338,7 @@ public AddEmbeddingsRequest(String id, float[] embedding, Map me */ @JsonInclude(JsonInclude.Include.NON_NULL) public record DeleteEmbeddingsRequest(// @formatter:off - @JsonProperty("ids") List ids, + @Nullable @JsonProperty("ids") List ids, @Nullable @JsonProperty("where") Map where) { // @formatter:on public DeleteEmbeddingsRequest(List ids) { diff --git a/vector-stores/spring-ai-chroma-store/src/main/java/org/springframework/ai/chroma/vectorstore/ChromaVectorStore.java b/vector-stores/spring-ai-chroma-store/src/main/java/org/springframework/ai/chroma/vectorstore/ChromaVectorStore.java index 14deaf40d8d..a8a600225ee 100644 --- a/vector-stores/spring-ai-chroma-store/src/main/java/org/springframework/ai/chroma/vectorstore/ChromaVectorStore.java +++ b/vector-stores/spring-ai-chroma-store/src/main/java/org/springframework/ai/chroma/vectorstore/ChromaVectorStore.java @@ -25,6 +25,8 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.json.JsonMapper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.ai.chroma.vectorstore.ChromaApi.AddEmbeddingsRequest; import org.springframework.ai.chroma.vectorstore.ChromaApi.DeleteEmbeddingsRequest; @@ -40,6 +42,7 @@ import org.springframework.ai.vectorstore.AbstractVectorStoreBuilder; import org.springframework.ai.vectorstore.SearchRequest; import org.springframework.ai.vectorstore.VectorStore; +import org.springframework.ai.vectorstore.filter.Filter; import org.springframework.ai.vectorstore.filter.FilterExpressionConverter; import org.springframework.ai.vectorstore.observation.AbstractObservationVectorStore; import org.springframework.ai.vectorstore.observation.VectorStoreObservationContext; @@ -81,6 +84,8 @@ public class ChromaVectorStore extends AbstractObservationVectorStore implements private boolean initialized = false; + private static final Logger logger = LoggerFactory.getLogger(ChromaVectorStore.class); + /** * @param builder {@link VectorStore.Builder} for chroma vector store */ @@ -162,7 +167,29 @@ public Optional doDelete(@NonNull List idList) { } @Override - public @NonNull List doSimilaritySearch(@NonNull SearchRequest request) { + protected void doDelete(Filter.Expression expression) { + Assert.notNull(expression, "Filter expression must not be null"); + + try { + ChromaFilterExpressionConverter converter = new ChromaFilterExpressionConverter(); + String whereClauseStr = converter.convertExpression(expression); + + Map whereClause = this.chromaApi.where(whereClauseStr); + + logger.debug("Deleting with where clause: {}", whereClause); + + DeleteEmbeddingsRequest deleteRequest = new DeleteEmbeddingsRequest(null, whereClause); + this.chromaApi.deleteEmbeddings(this.collectionId, deleteRequest); + } + catch (Exception e) { + logger.error("Failed to delete documents by filter: {}", e.getMessage(), e); + throw new IllegalStateException("Failed to delete documents by filter", e); + } + } + + @Override + @NonNull + public List doSimilaritySearch(@NonNull SearchRequest request) { String query = request.getQuery(); Assert.notNull(query, "Query string must not be null"); diff --git a/vector-stores/spring-ai-chroma-store/src/test/java/org/springframework/ai/chroma/vectorstore/ChromaVectorStoreIT.java b/vector-stores/spring-ai-chroma-store/src/test/java/org/springframework/ai/chroma/vectorstore/ChromaVectorStoreIT.java index 113903b797c..97802d034cb 100644 --- a/vector-stores/spring-ai-chroma-store/src/test/java/org/springframework/ai/chroma/vectorstore/ChromaVectorStoreIT.java +++ b/vector-stores/spring-ai-chroma-store/src/test/java/org/springframework/ai/chroma/vectorstore/ChromaVectorStoreIT.java @@ -1,5 +1,5 @@ /* - * Copyright 2023-2024 the original author or authors. + * 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. @@ -36,6 +36,7 @@ import org.springframework.ai.openai.api.OpenAiApi; import org.springframework.ai.vectorstore.SearchRequest; import org.springframework.ai.vectorstore.VectorStore; +import org.springframework.ai.vectorstore.filter.Filter; import org.springframework.boot.SpringBootConfiguration; import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.context.annotation.Bean; @@ -169,6 +170,69 @@ public void addAndSearchWithFilters() { }); } + @Test + public void deleteWithFilterExpression() { + this.contextRunner.run(context -> { + VectorStore vectorStore = context.getBean(VectorStore.class); + + // Create test documents with different metadata + var bgDocument = new Document("The World is Big and Salvation Lurks Around the Corner", + Map.of("country", "Bulgaria")); + var nlDocument = new Document("The World is Big and Salvation Lurks Around the Corner", + Map.of("country", "Netherlands")); + + // Add documents to the store + vectorStore.add(List.of(bgDocument, nlDocument)); + + // Verify initial state + var request = SearchRequest.builder().query("The World").topK(5).build(); + List results = vectorStore.similaritySearch(request); + assertThat(results).hasSize(2); + + // Delete document with country = Bulgaria + Filter.Expression filterExpression = new Filter.Expression(Filter.ExpressionType.EQ, + new Filter.Key("country"), new Filter.Value("Bulgaria")); + + vectorStore.delete(filterExpression); + + // Verify Bulgaria document was deleted + results = vectorStore + .similaritySearch(SearchRequest.from(request).filterExpression("country == 'Bulgaria'").build()); + assertThat(results).isEmpty(); + + // Verify Netherlands document still exists + results = vectorStore + .similaritySearch(SearchRequest.from(request).filterExpression("country == 'Netherlands'").build()); + assertThat(results).hasSize(1); + assertThat(results.get(0).getMetadata().get("country")).isEqualTo("Netherlands"); + + // Clean up + vectorStore.delete(List.of(nlDocument.getId())); + }); + } + + @Test + public void deleteWithStringFilterExpression() { + this.contextRunner.run(context -> { + VectorStore vectorStore = context.getBean(VectorStore.class); + + var bgDocument = new Document("The World is Big", Map.of("country", "Bulgaria")); + var nlDocument = new Document("The World is Big", Map.of("country", "Netherlands")); + vectorStore.add(List.of(bgDocument, nlDocument)); + + var request = SearchRequest.builder().query("World").topK(5).build(); + assertThat(vectorStore.similaritySearch(request)).hasSize(2); + + vectorStore.delete("country == 'Bulgaria'"); + + var results = vectorStore.similaritySearch(request); + assertThat(results).hasSize(1); + assertThat(results.get(0).getMetadata().get("country")).isEqualTo("Netherlands"); + + vectorStore.delete(List.of(nlDocument.getId())); + }); + } + @Test public void documentUpdateTest() { diff --git a/vector-stores/spring-ai-elasticsearch-store/src/main/java/org/springframework/ai/vectorstore/elasticsearch/ElasticsearchVectorStore.java b/vector-stores/spring-ai-elasticsearch-store/src/main/java/org/springframework/ai/vectorstore/elasticsearch/ElasticsearchVectorStore.java index f9b69f7463f..3e7514ea3bf 100644 --- a/vector-stores/spring-ai-elasticsearch-store/src/main/java/org/springframework/ai/vectorstore/elasticsearch/ElasticsearchVectorStore.java +++ b/vector-stores/spring-ai-elasticsearch-store/src/main/java/org/springframework/ai/vectorstore/elasticsearch/ElasticsearchVectorStore.java @@ -26,6 +26,7 @@ import co.elastic.clients.elasticsearch.ElasticsearchClient; import co.elastic.clients.elasticsearch.core.BulkRequest; import co.elastic.clients.elasticsearch.core.BulkResponse; +import co.elastic.clients.elasticsearch.core.DeleteByQueryResponse; import co.elastic.clients.elasticsearch.core.SearchResponse; import co.elastic.clients.elasticsearch.core.bulk.BulkResponseItem; import co.elastic.clients.elasticsearch.core.search.Hit; @@ -221,6 +222,23 @@ public Optional doDelete(List idList) { return Optional.of(bulkRequest(bulkRequestBuilder.build()).errors()); } + @Override + public void doDelete(Filter.Expression filterExpression) { + // For the index to be present, either it must be pre-created or set the + // initializeSchema to true. + if (!indexExists()) { + throw new IllegalArgumentException("Index not found"); + } + + try { + this.elasticsearchClient.deleteByQuery(d -> d.index(this.options.getIndexName()) + .query(q -> q.queryString(qs -> qs.query(getElasticsearchQueryString(filterExpression))))); + } + catch (Exception e) { + throw new IllegalStateException("Failed to delete documents by filter", e); + } + } + private BulkResponse bulkRequest(BulkRequest bulkRequest) { try { return this.elasticsearchClient.bulk(bulkRequest); diff --git a/vector-stores/spring-ai-elasticsearch-store/src/test/java/org/springframework/ai/vectorstore/elasticsearch/ElasticsearchVectorStoreIT.java b/vector-stores/spring-ai-elasticsearch-store/src/test/java/org/springframework/ai/vectorstore/elasticsearch/ElasticsearchVectorStoreIT.java index e35b98c16d4..5c8b74d6dd6 100644 --- a/vector-stores/spring-ai-elasticsearch-store/src/test/java/org/springframework/ai/vectorstore/elasticsearch/ElasticsearchVectorStoreIT.java +++ b/vector-stores/spring-ai-elasticsearch-store/src/test/java/org/springframework/ai/vectorstore/elasticsearch/ElasticsearchVectorStoreIT.java @@ -1,5 +1,5 @@ /* - * Copyright 2023-2024 the original author or authors. + * 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. @@ -53,6 +53,11 @@ import org.springframework.ai.openai.OpenAiEmbeddingModel; import org.springframework.ai.openai.api.OpenAiApi; import org.springframework.ai.vectorstore.SearchRequest; +import org.springframework.ai.vectorstore.filter.Filter; +import org.springframework.ai.vectorstore.filter.Filter.Expression; +import org.springframework.ai.vectorstore.filter.Filter.ExpressionType; +import org.springframework.ai.vectorstore.filter.Filter.Key; +import org.springframework.ai.vectorstore.filter.Filter.Value; import org.springframework.boot.SpringBootConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; @@ -143,6 +148,90 @@ public void addAndDeleteDocumentsTest() { }); } + @Test + public void deleteDocumentsByFilterExpressionTest() { + getContextRunner().run(context -> { + ElasticsearchVectorStore vectorStore = context.getBean("vectorStore_cosine", + ElasticsearchVectorStore.class); + ElasticsearchClient elasticsearchClient = context.getBean(ElasticsearchClient.class); + + IndicesStats stats = elasticsearchClient.indices() + .stats(s -> s.index("spring-ai-document-index")) + .indices() + .get("spring-ai-document-index"); + + assertThat(stats.total().docs().count()).isEqualTo(0L); + + // Add documents with metadata + List documents = List.of( + new Document("1", getText("classpath:/test/data/spring.ai.txt"), Map.of("meta1", "meta1")), + new Document("2", getText("classpath:/test/data/time.shelter.txt"), Map.of()), + new Document("3", getText("classpath:/test/data/great.depression.txt"), Map.of("meta2", "meta2"))); + + vectorStore.add(documents); + elasticsearchClient.indices().refresh(); + + stats = elasticsearchClient.indices() + .stats(s -> s.index("spring-ai-document-index")) + .indices() + .get("spring-ai-document-index"); + assertThat(stats.total().docs().count()).isEqualTo(3L); + + // Delete documents with meta1 using filter expression + Expression filterExpression = new Expression(ExpressionType.EQ, new Key("meta1"), new Value("meta1")); + + vectorStore.delete(filterExpression); + elasticsearchClient.indices().refresh(); + + stats = elasticsearchClient.indices() + .stats(s -> s.index("spring-ai-document-index")) + .indices() + .get("spring-ai-document-index"); + assertThat(stats.total().docs().count()).isEqualTo(2L); + + // Clean up remaining documents + vectorStore.delete(List.of("2", "3")); + elasticsearchClient.indices().refresh(); + + stats = elasticsearchClient.indices() + .stats(s -> s.index("spring-ai-document-index")) + .indices() + .get("spring-ai-document-index"); + assertThat(stats.total().docs().count()).isEqualTo(0L); + }); + } + + @Test + public void deleteWithStringFilterExpressionTest() { + getContextRunner().run(context -> { + ElasticsearchVectorStore vectorStore = context.getBean("vectorStore_cosine", + ElasticsearchVectorStore.class); + ElasticsearchClient elasticsearchClient = context.getBean(ElasticsearchClient.class); + + List documents = List.of( + new Document("1", getText("classpath:/test/data/spring.ai.txt"), Map.of("meta1", "meta1")), + new Document("2", getText("classpath:/test/data/time.shelter.txt"), Map.of()), + new Document("3", getText("classpath:/test/data/great.depression.txt"), Map.of("meta2", "meta2"))); + + vectorStore.add(documents); + elasticsearchClient.indices().refresh(); + + // Delete documents with meta1 using string filter + vectorStore.delete("meta1 == 'meta1'"); + elasticsearchClient.indices().refresh(); + + IndicesStats stats = elasticsearchClient.indices() + .stats(s -> s.index("spring-ai-document-index")) + .indices() + .get("spring-ai-document-index"); + assertThat(stats.total().docs().count()).isEqualTo(2L); + + // Clean up + vectorStore.delete(List.of("2", "3")); + elasticsearchClient.indices().refresh(); + }); + } + @ParameterizedTest(name = "{0} : {displayName} ") @ValueSource(strings = { "cosine", "l2_norm", "dot_product" }) public void addAndSearchTest(String similarityFunction) { diff --git a/vector-stores/spring-ai-pgvector-store/src/main/java/org/springframework/ai/vectorstore/pgvector/PgVectorStore.java b/vector-stores/spring-ai-pgvector-store/src/main/java/org/springframework/ai/vectorstore/pgvector/PgVectorStore.java index 19b70507db8..0def197f13f 100644 --- a/vector-stores/spring-ai-pgvector-store/src/main/java/org/springframework/ai/vectorstore/pgvector/PgVectorStore.java +++ b/vector-stores/spring-ai-pgvector-store/src/main/java/org/springframework/ai/vectorstore/pgvector/PgVectorStore.java @@ -45,6 +45,7 @@ import org.springframework.ai.vectorstore.AbstractVectorStoreBuilder; import org.springframework.ai.vectorstore.SearchRequest; import org.springframework.ai.vectorstore.VectorStore; +import org.springframework.ai.vectorstore.filter.Filter; import org.springframework.ai.vectorstore.filter.FilterExpressionConverter; import org.springframework.ai.vectorstore.observation.AbstractObservationVectorStore; import org.springframework.ai.vectorstore.observation.VectorStoreObservationContext; @@ -315,6 +316,22 @@ public Optional doDelete(List idList) { return Optional.of(updateCount == idList.size()); } + @Override + protected void doDelete(Filter.Expression filterExpression) { + String nativeFilterExpression = this.filterExpressionConverter.convertExpression(filterExpression); + + String sql = "DELETE FROM " + getFullyQualifiedTableName() + " WHERE metadata::jsonb @@ '" + + nativeFilterExpression + "'::jsonpath"; + + // Execute the delete + try { + this.jdbcTemplate.update(sql); + } + catch (Exception e) { + throw new IllegalStateException("Failed to delete documents by filter", e); + } + } + @Override public List doSimilaritySearch(SearchRequest request) { diff --git a/vector-stores/spring-ai-pgvector-store/src/test/java/org/springframework/ai/vectorstore/pgvector/PgVectorStoreIT.java b/vector-stores/spring-ai-pgvector-store/src/test/java/org/springframework/ai/vectorstore/pgvector/PgVectorStoreIT.java index 82a70237a2f..4cd00d3b800 100644 --- a/vector-stores/spring-ai-pgvector-store/src/test/java/org/springframework/ai/vectorstore/pgvector/PgVectorStoreIT.java +++ b/vector-stores/spring-ai-pgvector-store/src/test/java/org/springframework/ai/vectorstore/pgvector/PgVectorStoreIT.java @@ -1,5 +1,5 @@ /* - * Copyright 2023-2024 the original author or authors. + * 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. @@ -22,6 +22,7 @@ import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.UUID; import java.util.stream.Stream; @@ -29,6 +30,7 @@ import com.zaxxer.hikari.HikariDataSource; import org.junit.Assert; +import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; @@ -45,6 +47,7 @@ import org.springframework.ai.openai.api.OpenAiApi; import org.springframework.ai.vectorstore.SearchRequest; import org.springframework.ai.vectorstore.VectorStore; +import org.springframework.ai.vectorstore.filter.Filter; import org.springframework.ai.vectorstore.filter.FilterExpressionTextParser.FilterExpressionParseException; import org.springframework.ai.vectorstore.pgvector.PgVectorStore.PgIndexType; import org.springframework.beans.factory.annotation.Value; @@ -364,6 +367,121 @@ public void searchWithThreshold(String distanceType) { }); } + @Test + public void deleteByIds() { + this.contextRunner.withPropertyValues("test.spring.ai.vectorstore.pgvector.distanceType=COSINE_DISTANCE") + .run(context -> { + VectorStore vectorStore = context.getBean(VectorStore.class); + + // Create test documents + var bgDocument = new Document("The World is Big and Salvation Lurks Around the Corner", + Map.of("country", "BG", "year", 2020)); + var nlDocument = new Document("The World is Big and Salvation Lurks Around the Corner", + Map.of("country", "NL", "year", 2021)); + var bgDocument2 = new Document("The World is Big and Salvation Lurks Around the Corner", + Map.of("country", "BG", "year", 2023)); + + // Add documents to store + vectorStore.add(List.of(bgDocument, nlDocument, bgDocument2)); + + // Verify initial state + SearchRequest searchRequest = SearchRequest.builder() + .query("The World") + .topK(5) + .similarityThresholdAll() + .build(); + + List results = vectorStore.similaritySearch(searchRequest); + assertThat(results).hasSize(3); + + // Delete two documents by ID + Optional deleteResult = vectorStore.delete(List.of(bgDocument.getId(), nlDocument.getId())); + + assertThat(deleteResult).isPresent().contains(true); + + // Verify deletion + results = vectorStore.similaritySearch(searchRequest); + assertThat(results).hasSize(1); + assertThat(results.get(0).getId()).isEqualTo(bgDocument2.getId()); + + // Remove all documents from the store + dropTable(context); + }); + } + + @Test + public void deleteByFilter() { + this.contextRunner.withPropertyValues("test.spring.ai.vectorstore.pgvector.distanceType=COSINE_DISTANCE") + .run(context -> { + VectorStore vectorStore = context.getBean(VectorStore.class); + + // Create test documents + var bgDocument = new Document("The World is Big and Salvation Lurks Around the Corner", + Map.of("country", "BG", "year", 2020)); + var nlDocument = new Document("The World is Big and Salvation Lurks Around the Corner", + Map.of("country", "NL", "year", 2021)); + var bgDocument2 = new Document("The World is Big and Salvation Lurks Around the Corner", + Map.of("country", "BG", "year", 2023)); + + // Add documents to store + vectorStore.add(List.of(bgDocument, nlDocument, bgDocument2)); + + // Verify initial state + SearchRequest searchRequest = SearchRequest.builder() + .query("The World") + .topK(5) + .similarityThresholdAll() + .build(); + + List results = vectorStore.similaritySearch(searchRequest); + assertThat(results).hasSize(3); + + // Create filter to delete all documents with country=BG + Filter.Expression filterExpression = new Filter.Expression(Filter.ExpressionType.EQ, + new Filter.Key("country"), new Filter.Value("BG")); + + // Delete documents using filter + vectorStore.delete(filterExpression); + + // Verify deletion - should only have NL document remaining + results = vectorStore.similaritySearch(searchRequest); + assertThat(results).hasSize(1); + assertThat(results.get(0).getMetadata()).containsEntry("country", "NL"); + + // Remove all documents from the store + dropTable(context); + }); + } + + @Test + public void deleteWithStringFilterExpression() { + this.contextRunner.run(context -> { + VectorStore vectorStore = context.getBean(VectorStore.class); + + var bgDocument = new Document("The World is Big and Salvation Lurks Around the Corner", + Map.of("country", "BG", "year", 2020)); + var nlDocument = new Document("The World is Big and Salvation Lurks Around the Corner", + Map.of("country", "NL", "year", 2021)); + var bgDocument2 = new Document("The World is Big and Salvation Lurks Around the Corner", + Map.of("country", "BG", "year", 2023)); + + vectorStore.add(List.of(bgDocument, nlDocument, bgDocument2)); + + var searchRequest = SearchRequest.builder().query("The World").topK(5).similarityThresholdAll().build(); + + List results = vectorStore.similaritySearch(searchRequest); + assertThat(results).hasSize(3); + + vectorStore.delete("country == 'BG'"); + + results = vectorStore.similaritySearch(searchRequest); + assertThat(results).hasSize(1); + assertThat(results.get(0).getMetadata()).containsEntry("country", "NL"); + + vectorStore.delete(List.of(nlDocument.getId())); + }); + } + @SpringBootConfiguration @EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class }) public static class TestApplication {