|
1 | 1 | /*
|
2 |
| - * Copyright 2023-2024 the original author or authors. |
| 2 | + * Copyright 2023-2025 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
21 | 21 | import java.util.Map;
|
22 | 22 | import java.util.UUID;
|
23 | 23 | import java.util.concurrent.ExecutionException;
|
| 24 | +import java.util.stream.Collectors; |
24 | 25 |
|
25 | 26 | import io.qdrant.client.QdrantClient;
|
26 | 27 | import io.qdrant.client.QdrantGrpcClient;
|
|
41 | 42 | import org.springframework.ai.mistralai.api.MistralAiApi;
|
42 | 43 | import org.springframework.ai.vectorstore.SearchRequest;
|
43 | 44 | import org.springframework.ai.vectorstore.VectorStore;
|
| 45 | +import org.springframework.ai.vectorstore.filter.Filter; |
44 | 46 | import org.springframework.boot.SpringBootConfiguration;
|
45 | 47 | import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
46 | 48 | import org.springframework.context.annotation.Bean;
|
|
52 | 54 | * @author Josh Long
|
53 | 55 | * @author Eddú Meléndez
|
54 | 56 | * @author Thomas Vitale
|
| 57 | + * @author Soby Chacko |
55 | 58 | * @since 0.8.1
|
56 | 59 | */
|
57 | 60 | @Testcontainers
|
@@ -256,6 +259,91 @@ public void searchThresholdTest() {
|
256 | 259 | });
|
257 | 260 | }
|
258 | 261 |
|
| 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 | + |
259 | 347 | @SpringBootConfiguration
|
260 | 348 | public static class TestApplication {
|
261 | 349 |
|
|
0 commit comments