Skip to content

Commit 81d5618

Browse files
sobychackomarkpollack
authored andcommitted
Add filter-based deletion to Qdrant vector store
Add string-based filter deletion alongside the Filter.Expression-based deletion for Qdrant vector store, providing consistent deletion capabilities with other vector store implementations. Key changes: - Add delete(Filter.Expression) implementation using Qdrant's filter API - Leverage existing QdrantFilterExpressionConverter for filter translation - Use Qdrant's native deleteAsync with filter capabilities - Add comprehensive integration tests for filter deletion - Support both simple and complex filter expressions This maintains consistency with other vector store implementations while utilizing Qdrant's native filtering capabilities for efficient metadata-based deletion. Signed-off-by: Soby Chacko <[email protected]>
1 parent b30713e commit 81d5618

File tree

2 files changed

+118
-5
lines changed

2 files changed

+118
-5
lines changed

vector-stores/spring-ai-qdrant-store/src/main/java/org/springframework/ai/vectorstore/qdrant/QdrantVectorStore.java

Lines changed: 29 additions & 4 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.
@@ -32,20 +32,20 @@
3232
import io.qdrant.client.grpc.Points.ScoredPoint;
3333
import io.qdrant.client.grpc.Points.SearchPoints;
3434
import io.qdrant.client.grpc.Points.UpdateStatus;
35+
import org.apache.commons.logging.LogFactory;
3536

3637
import org.springframework.ai.document.Document;
3738
import org.springframework.ai.document.DocumentMetadata;
38-
import org.springframework.ai.embedding.BatchingStrategy;
3939
import org.springframework.ai.embedding.EmbeddingModel;
4040
import org.springframework.ai.embedding.EmbeddingOptionsBuilder;
41-
import org.springframework.ai.embedding.TokenCountBatchingStrategy;
4241
import org.springframework.ai.model.EmbeddingUtils;
4342
import org.springframework.ai.observation.conventions.VectorStoreProvider;
4443
import org.springframework.ai.vectorstore.AbstractVectorStoreBuilder;
4544
import org.springframework.ai.vectorstore.SearchRequest;
4645
import org.springframework.ai.vectorstore.observation.AbstractObservationVectorStore;
4746
import org.springframework.ai.vectorstore.observation.VectorStoreObservationContext;
4847
import org.springframework.beans.factory.InitializingBean;
48+
import org.springframework.core.log.LogAccessor;
4949
import org.springframework.util.Assert;
5050

5151
/**
@@ -122,10 +122,12 @@
122122
* @author Josh Long
123123
* @author Soby Chacko
124124
* @author Thomas Vitale
125-
* @since 0.8.1
125+
* @since 1.0.0
126126
*/
127127
public class QdrantVectorStore extends AbstractObservationVectorStore implements InitializingBean {
128128

129+
private static final LogAccessor logger = new LogAccessor(LogFactory.getLog(QdrantVectorStore.class));
130+
129131
public static final String DEFAULT_COLLECTION_NAME = "vector_store";
130132

131133
private static final String CONTENT_FIELD_NAME = "doc_content";
@@ -214,6 +216,29 @@ public Optional<Boolean> doDelete(List<String> documentIds) {
214216
}
215217
}
216218

219+
@Override
220+
protected void doDelete(org.springframework.ai.vectorstore.filter.Filter.Expression filterExpression) {
221+
Assert.notNull(filterExpression, "Filter expression must not be null");
222+
223+
try {
224+
Filter filter = this.filterExpressionConverter.convertExpression(filterExpression);
225+
226+
io.qdrant.client.grpc.Points.UpdateResult response = this.qdrantClient
227+
.deleteAsync(this.collectionName, filter)
228+
.get();
229+
230+
if (response.getStatus() != io.qdrant.client.grpc.Points.UpdateStatus.Completed) {
231+
throw new IllegalStateException("Failed to delete documents by filter: " + response.getStatus());
232+
}
233+
234+
logger.debug("Deleted documents matching filter expression");
235+
}
236+
catch (Exception e) {
237+
logger.error(e, "Failed to delete documents by filter: " + e.getMessage());
238+
throw new IllegalStateException("Failed to delete documents by filter", e);
239+
}
240+
}
241+
217242
/**
218243
* Performs a similarity search on the vector store.
219244
* @param request The {@link SearchRequest} object containing the query and other

vector-stores/spring-ai-qdrant-store/src/test/java/org/springframework/ai/vectorstore/qdrant/QdrantVectorStoreIT.java

Lines changed: 89 additions & 1 deletion
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.
@@ -21,6 +21,7 @@
2121
import java.util.Map;
2222
import java.util.UUID;
2323
import java.util.concurrent.ExecutionException;
24+
import java.util.stream.Collectors;
2425

2526
import io.qdrant.client.QdrantClient;
2627
import io.qdrant.client.QdrantGrpcClient;
@@ -41,6 +42,7 @@
4142
import org.springframework.ai.mistralai.api.MistralAiApi;
4243
import org.springframework.ai.vectorstore.SearchRequest;
4344
import org.springframework.ai.vectorstore.VectorStore;
45+
import org.springframework.ai.vectorstore.filter.Filter;
4446
import org.springframework.boot.SpringBootConfiguration;
4547
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
4648
import org.springframework.context.annotation.Bean;
@@ -52,6 +54,7 @@
5254
* @author Josh Long
5355
* @author Eddú Meléndez
5456
* @author Thomas Vitale
57+
* @author Soby Chacko
5558
* @since 0.8.1
5659
*/
5760
@Testcontainers
@@ -256,6 +259,91 @@ public void searchThresholdTest() {
256259
});
257260
}
258261

262+
@Test
263+
void deleteByFilter() {
264+
this.contextRunner.run(context -> {
265+
VectorStore vectorStore = context.getBean(VectorStore.class);
266+
267+
var bgDocument = new Document("The World is Big and Salvation Lurks Around the Corner",
268+
Map.of("country", "Bulgaria", "number", 3));
269+
var nlDocument = new Document("The World is Big and Salvation Lurks Around the Corner",
270+
Map.of("country", "Netherlands", "number", 90));
271+
272+
vectorStore.add(List.of(bgDocument, nlDocument));
273+
274+
Filter.Expression filterExpression = new Filter.Expression(Filter.ExpressionType.EQ,
275+
new Filter.Key("country"), new Filter.Value("Bulgaria"));
276+
277+
vectorStore.delete(filterExpression);
278+
279+
List<Document> results = vectorStore
280+
.similaritySearch(SearchRequest.builder().query("The World").topK(5).similarityThresholdAll().build());
281+
282+
assertThat(results).hasSize(1);
283+
assertThat(results.get(0).getMetadata()).containsEntry("country", "Netherlands");
284+
285+
vectorStore.delete(List.of(nlDocument.getId()));
286+
});
287+
}
288+
289+
@Test
290+
void deleteWithStringFilterExpression() {
291+
this.contextRunner.run(context -> {
292+
VectorStore vectorStore = context.getBean(VectorStore.class);
293+
294+
var bgDocument = new Document("The World is Big and Salvation Lurks Around the Corner",
295+
Map.of("country", "Bulgaria", "number", 3));
296+
var nlDocument = new Document("The World is Big and Salvation Lurks Around the Corner",
297+
Map.of("country", "Netherlands", "number", 90));
298+
299+
vectorStore.add(List.of(bgDocument, nlDocument));
300+
301+
vectorStore.delete("number > 50");
302+
303+
List<Document> results = vectorStore
304+
.similaritySearch(SearchRequest.builder().query("The World").topK(5).similarityThresholdAll().build());
305+
306+
assertThat(results).hasSize(1);
307+
assertThat(results.get(0).getMetadata()).containsEntry("country", "Bulgaria");
308+
309+
vectorStore.delete(List.of(bgDocument.getId()));
310+
});
311+
}
312+
313+
@Test
314+
void deleteWithComplexFilterExpression() {
315+
this.contextRunner.run(context -> {
316+
VectorStore vectorStore = context.getBean(VectorStore.class);
317+
318+
var doc1 = new Document("Content 1", Map.of("type", "A", "priority", 1));
319+
var doc2 = new Document("Content 2", Map.of("type", "A", "priority", 2));
320+
var doc3 = new Document("Content 3", Map.of("type", "B", "priority", 1));
321+
322+
vectorStore.add(List.of(doc1, doc2, doc3));
323+
324+
// Complex filter expression: (type == 'A' AND priority > 1)
325+
Filter.Expression priorityFilter = new Filter.Expression(Filter.ExpressionType.GT,
326+
new Filter.Key("priority"), new Filter.Value(1));
327+
Filter.Expression typeFilter = new Filter.Expression(Filter.ExpressionType.EQ, new Filter.Key("type"),
328+
new Filter.Value("A"));
329+
Filter.Expression complexFilter = new Filter.Expression(Filter.ExpressionType.AND, typeFilter,
330+
priorityFilter);
331+
332+
vectorStore.delete(complexFilter);
333+
334+
var results = vectorStore
335+
.similaritySearch(SearchRequest.builder().query("Content").topK(5).similarityThresholdAll().build());
336+
337+
assertThat(results).hasSize(2);
338+
assertThat(results.stream().map(doc -> doc.getMetadata().get("type")).collect(Collectors.toList()))
339+
.containsExactlyInAnyOrder("A", "B");
340+
assertThat(results.stream().map(doc -> doc.getMetadata().get("priority")).collect(Collectors.toList()))
341+
.containsExactlyInAnyOrder(1L, 1L);
342+
343+
vectorStore.delete(List.of(doc1.getId(), doc3.getId()));
344+
});
345+
}
346+
259347
@SpringBootConfiguration
260348
public static class TestApplication {
261349

0 commit comments

Comments
 (0)