Skip to content

Commit bca65de

Browse files
sobychackomarkpollack
authored andcommitted
Add filter-based deletion to Redis vector store
Add string-based filter deletion alongside the Filter.Expression-based deletion for Redis vector store, providing consistent deletion capabilities with other vector store implementations. Key changes: - Add delete(Filter.Expression) implementation using Redis FT.SEARCH and JSON.DEL - Configure metadata fields properly to support numeric and tag operations - Support both simple and complex filter expressions - Handle Redis-specific JSON string responses in tests - Add comprehensive integration tests for filter deletion cases This maintains consistency with other vector store implementations while utilizing Redis Search capabilities for efficient metadata-based deletion. Signed-off-by: Soby Chacko <[email protected]>
1 parent 81d5618 commit bca65de

File tree

2 files changed

+134
-2
lines changed

2 files changed

+134
-2
lines changed

vector-stores/spring-ai-redis-store/src/main/java/org/springframework/ai/vectorstore/redis/RedisVectorStore.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import org.springframework.ai.vectorstore.AbstractVectorStoreBuilder;
5454
import org.springframework.ai.vectorstore.SearchRequest;
5555
import org.springframework.ai.vectorstore.VectorStore;
56+
import org.springframework.ai.vectorstore.filter.Filter;
5657
import org.springframework.ai.vectorstore.filter.FilterExpressionConverter;
5758
import org.springframework.ai.vectorstore.observation.AbstractObservationVectorStore;
5859
import org.springframework.ai.vectorstore.observation.VectorStoreObservationContext;
@@ -296,6 +297,45 @@ public Optional<Boolean> doDelete(List<String> idList) {
296297
}
297298
}
298299

300+
@Override
301+
protected void doDelete(Filter.Expression filterExpression) {
302+
Assert.notNull(filterExpression, "Filter expression must not be null");
303+
304+
try {
305+
String filterStr = this.filterExpressionConverter.convertExpression(filterExpression);
306+
307+
List<String> matchingIds = new ArrayList<>();
308+
SearchResult searchResult = this.jedis.ftSearch(this.indexName, filterStr);
309+
310+
for (redis.clients.jedis.search.Document doc : searchResult.getDocuments()) {
311+
String docId = doc.getId();
312+
matchingIds.add(docId.replace(key(""), "")); // Remove the key prefix to
313+
// get original ID
314+
}
315+
316+
if (!matchingIds.isEmpty()) {
317+
try (Pipeline pipeline = this.jedis.pipelined()) {
318+
for (String id : matchingIds) {
319+
pipeline.jsonDel(key(id));
320+
}
321+
List<Object> responses = pipeline.syncAndReturnAll();
322+
Optional<Object> errResponse = responses.stream().filter(Predicate.not(RESPONSE_DEL_OK)).findAny();
323+
324+
if (errResponse.isPresent()) {
325+
logger.error(() -> "Could not delete document: " + errResponse.get());
326+
throw new IllegalStateException("Failed to delete some documents");
327+
}
328+
}
329+
330+
logger.debug(() -> "Deleted " + matchingIds.size() + " documents matching filter expression");
331+
}
332+
}
333+
catch (Exception e) {
334+
logger.error(e, () -> "Failed to delete documents by filter");
335+
throw new IllegalStateException("Failed to delete documents by filter", e);
336+
}
337+
}
338+
299339
@Override
300340
public List<Document> doSimilaritySearch(SearchRequest request) {
301341

vector-stores/spring-ai-redis-store/src/test/java/org/springframework/ai/vectorstore/redis/RedisVectorStoreIT.java

Lines changed: 94 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023-2024 the original author or authors.
2+
* Copyright 2023-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@
2222
import java.util.List;
2323
import java.util.Map;
2424
import java.util.UUID;
25+
import java.util.stream.Collectors;
2526

2627
import com.redis.testcontainers.RedisStackContainer;
2728
import org.junit.jupiter.api.BeforeEach;
@@ -36,6 +37,7 @@
3637
import org.springframework.ai.transformers.TransformersEmbeddingModel;
3738
import org.springframework.ai.vectorstore.SearchRequest;
3839
import org.springframework.ai.vectorstore.VectorStore;
40+
import org.springframework.ai.vectorstore.filter.Filter;
3941
import org.springframework.ai.vectorstore.redis.RedisVectorStore.MetadataField;
4042
import org.springframework.boot.SpringBootConfiguration;
4143
import org.springframework.boot.autoconfigure.AutoConfigurations;
@@ -53,6 +55,7 @@
5355
* @author Julien Ruaux
5456
* @author Eddú Meléndez
5557
* @author Thomas Vitale
58+
* @author Soby Chacko
5659
*/
5760
@Testcontainers
5861
class RedisVectorStoreIT {
@@ -260,6 +263,90 @@ void searchWithThreshold() {
260263
});
261264
}
262265

266+
@Test
267+
void deleteByFilter() {
268+
this.contextRunner.run(context -> {
269+
VectorStore vectorStore = context.getBean(VectorStore.class);
270+
271+
var bgDocument = new Document("The World is Big and Salvation Lurks Around the Corner",
272+
Map.of("country", "BG", "year", 2020));
273+
var nlDocument = new Document("The World is Big and Salvation Lurks Around the Corner",
274+
Map.of("country", "NL"));
275+
var bgDocument2 = new Document("The World is Big and Salvation Lurks Around the Corner",
276+
Map.of("country", "BG", "year", 2023));
277+
278+
vectorStore.add(List.of(bgDocument, nlDocument, bgDocument2));
279+
280+
Filter.Expression filterExpression = new Filter.Expression(Filter.ExpressionType.EQ,
281+
new Filter.Key("country"), new Filter.Value("BG"));
282+
283+
vectorStore.delete(filterExpression);
284+
285+
List<Document> results = vectorStore
286+
.similaritySearch(SearchRequest.builder().query("The World").topK(5).similarityThresholdAll().build());
287+
288+
assertThat(results).hasSize(1);
289+
assertThat(results.get(0).getMetadata()).containsEntry("country", "NL");
290+
});
291+
}
292+
293+
@Test
294+
void deleteWithStringFilterExpression() {
295+
this.contextRunner.run(context -> {
296+
VectorStore vectorStore = context.getBean(VectorStore.class);
297+
298+
var bgDocument = new Document("The World is Big and Salvation Lurks Around the Corner",
299+
Map.of("country", "BG", "year", 2020));
300+
var nlDocument = new Document("The World is Big and Salvation Lurks Around the Corner",
301+
Map.of("country", "NL"));
302+
var bgDocument2 = new Document("The World is Big and Salvation Lurks Around the Corner",
303+
Map.of("country", "BG", "year", 2023));
304+
305+
vectorStore.add(List.of(bgDocument, nlDocument, bgDocument2));
306+
307+
vectorStore.delete("country == 'BG'");
308+
309+
List<Document> results = vectorStore
310+
.similaritySearch(SearchRequest.builder().query("The World").topK(5).similarityThresholdAll().build());
311+
312+
assertThat(results).hasSize(1);
313+
assertThat(results.get(0).getMetadata()).containsEntry("country", "NL");
314+
});
315+
}
316+
317+
@Test
318+
void deleteWithComplexFilterExpression() {
319+
this.contextRunner.run(context -> {
320+
VectorStore vectorStore = context.getBean(VectorStore.class);
321+
322+
var doc1 = new Document("Content 1", Map.of("type", "A", "priority", 1));
323+
var doc2 = new Document("Content 2", Map.of("type", "A", "priority", 2));
324+
var doc3 = new Document("Content 3", Map.of("type", "B", "priority", 1));
325+
326+
vectorStore.add(List.of(doc1, doc2, doc3));
327+
328+
// Complex filter expression: (type == 'A' AND priority > 1)
329+
Filter.Expression priorityFilter = new Filter.Expression(Filter.ExpressionType.GT,
330+
new Filter.Key("priority"), new Filter.Value(1));
331+
Filter.Expression typeFilter = new Filter.Expression(Filter.ExpressionType.EQ, new Filter.Key("type"),
332+
new Filter.Value("A"));
333+
Filter.Expression complexFilter = new Filter.Expression(Filter.ExpressionType.AND, typeFilter,
334+
priorityFilter);
335+
336+
vectorStore.delete(complexFilter);
337+
338+
var results = vectorStore
339+
.similaritySearch(SearchRequest.builder().query("Content").topK(5).similarityThresholdAll().build());
340+
341+
assertThat(results).hasSize(2);
342+
assertThat(results.stream().map(doc -> doc.getMetadata().get("type")).collect(Collectors.toList()))
343+
.containsExactlyInAnyOrder("A", "B");
344+
assertThat(results.stream()
345+
.map(doc -> Integer.parseInt(doc.getMetadata().get("priority").toString()))
346+
.collect(Collectors.toList())).containsExactlyInAnyOrder(1, 1);
347+
});
348+
}
349+
263350
@SpringBootConfiguration
264351
@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class })
265352
public static class TestApplication {
@@ -271,7 +358,12 @@ public RedisVectorStore vectorStore(EmbeddingModel embeddingModel,
271358
.builder(new JedisPooled(jedisConnectionFactory.getHostName(), jedisConnectionFactory.getPort()),
272359
embeddingModel)
273360
.metadataFields(MetadataField.tag("meta1"), MetadataField.tag("meta2"), MetadataField.tag("country"),
274-
MetadataField.numeric("year"))
361+
MetadataField.numeric("year"), MetadataField.numeric("priority"), // Add
362+
// priority
363+
// as
364+
// numeric
365+
MetadataField.tag("type") // Add type as tag
366+
)
275367
.initializeSchema(true)
276368
.build();
277369
}

0 commit comments

Comments
 (0)