|
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. |
|
22 | 22 | import java.util.List; |
23 | 23 | import java.util.Map; |
24 | 24 | import java.util.UUID; |
| 25 | +import java.util.stream.Collectors; |
25 | 26 |
|
26 | 27 | import io.milvus.client.MilvusServiceClient; |
27 | 28 | import io.milvus.param.ConnectParam; |
28 | 29 | import io.milvus.param.IndexType; |
29 | 30 | import io.milvus.param.MetricType; |
| 31 | +import org.junit.jupiter.api.Test; |
30 | 32 | import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable; |
31 | 33 | import org.junit.jupiter.params.ParameterizedTest; |
32 | 34 | import org.junit.jupiter.params.provider.ValueSource; |
|
42 | 44 | import org.springframework.ai.openai.api.OpenAiApi; |
43 | 45 | import org.springframework.ai.vectorstore.SearchRequest; |
44 | 46 | import org.springframework.ai.vectorstore.VectorStore; |
| 47 | +import org.springframework.ai.vectorstore.filter.Filter; |
45 | 48 | import org.springframework.beans.factory.annotation.Value; |
46 | 49 | import org.springframework.boot.SpringBootConfiguration; |
47 | 50 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration; |
|
56 | 59 | * @author Christian Tzolov |
57 | 60 | * @author Eddú Meléndez |
58 | 61 | * @author Thomas Vitale |
| 62 | + * @author Soby Chacko |
59 | 63 | */ |
60 | 64 | @Testcontainers |
61 | 65 | @EnabledIfEnvironmentVariable(named = "OPENAI_API_KEY", matches = ".+") |
@@ -273,6 +277,107 @@ public void searchWithThreshold(String metricType) { |
273 | 277 | }); |
274 | 278 | } |
275 | 279 |
|
| 280 | + @Test |
| 281 | + public void deleteByFilter() { |
| 282 | + this.contextRunner.withPropertyValues("test.spring.ai.vectorstore.milvus.metricType=COSINE").run(context -> { |
| 283 | + VectorStore vectorStore = context.getBean(VectorStore.class); |
| 284 | + |
| 285 | + resetCollection(vectorStore); |
| 286 | + |
| 287 | + var bgDocument = new Document("The World is Big and Salvation Lurks Around the Corner", |
| 288 | + Map.of("country", "BG", "year", 2020)); |
| 289 | + var nlDocument = new Document("The World is Big and Salvation Lurks Around the Corner", |
| 290 | + Map.of("country", "NL", "year", 2021)); |
| 291 | + var bgDocument2 = new Document("The World is Big and Salvation Lurks Around the Corner", |
| 292 | + Map.of("country", "BG", "year", 2023)); |
| 293 | + |
| 294 | + vectorStore.add(List.of(bgDocument, nlDocument, bgDocument2)); |
| 295 | + |
| 296 | + SearchRequest searchRequest = SearchRequest.builder() |
| 297 | + .query("The World") |
| 298 | + .topK(5) |
| 299 | + .similarityThresholdAll() |
| 300 | + .build(); |
| 301 | + |
| 302 | + List<Document> results = vectorStore.similaritySearch(searchRequest); |
| 303 | + assertThat(results).hasSize(3); |
| 304 | + |
| 305 | + Filter.Expression filterExpression = new Filter.Expression(Filter.ExpressionType.EQ, |
| 306 | + new Filter.Key("country"), new Filter.Value("BG")); |
| 307 | + |
| 308 | + vectorStore.delete(filterExpression); |
| 309 | + |
| 310 | + // Verify deletion - should only have NL document remaining |
| 311 | + results = vectorStore.similaritySearch(searchRequest); |
| 312 | + assertThat(results).hasSize(1); |
| 313 | + assertThat(results.get(0).getMetadata()).containsEntry("country", "NL"); |
| 314 | + }); |
| 315 | + } |
| 316 | + |
| 317 | + @Test |
| 318 | + public void deleteWithStringFilterExpression() { |
| 319 | + this.contextRunner.withPropertyValues("test.spring.ai.vectorstore.milvus.metricType=COSINE").run(context -> { |
| 320 | + VectorStore vectorStore = context.getBean(VectorStore.class); |
| 321 | + |
| 322 | + resetCollection(vectorStore); |
| 323 | + |
| 324 | + var bgDocument = new Document("The World is Big and Salvation Lurks Around the Corner", |
| 325 | + Map.of("country", "BG", "year", 2020)); |
| 326 | + var nlDocument = new Document("The World is Big and Salvation Lurks Around the Corner", |
| 327 | + Map.of("country", "NL", "year", 2021)); |
| 328 | + var bgDocument2 = new Document("The World is Big and Salvation Lurks Around the Corner", |
| 329 | + Map.of("country", "BG", "year", 2023)); |
| 330 | + |
| 331 | + vectorStore.add(List.of(bgDocument, nlDocument, bgDocument2)); |
| 332 | + |
| 333 | + var searchRequest = SearchRequest.builder().query("The World").topK(5).similarityThresholdAll().build(); |
| 334 | + |
| 335 | + List<Document> results = vectorStore.similaritySearch(searchRequest); |
| 336 | + assertThat(results).hasSize(3); |
| 337 | + |
| 338 | + // Delete using string filter expression |
| 339 | + vectorStore.delete("country == 'BG'"); |
| 340 | + |
| 341 | + results = vectorStore.similaritySearch(searchRequest); |
| 342 | + assertThat(results).hasSize(1); |
| 343 | + assertThat(results.get(0).getMetadata()).containsEntry("country", "NL"); |
| 344 | + }); |
| 345 | + } |
| 346 | + |
| 347 | + @Test |
| 348 | + public void deleteWithComplexFilterExpression() { |
| 349 | + this.contextRunner.withPropertyValues("test.spring.ai.vectorstore.milvus.metricType=COSINE").run(context -> { |
| 350 | + VectorStore vectorStore = context.getBean(VectorStore.class); |
| 351 | + |
| 352 | + resetCollection(vectorStore); |
| 353 | + |
| 354 | + var doc1 = new Document("Content 1", Map.of("type", "A", "priority", 1)); |
| 355 | + var doc2 = new Document("Content 2", Map.of("type", "A", "priority", 2)); |
| 356 | + var doc3 = new Document("Content 3", Map.of("type", "B", "priority", 1)); |
| 357 | + |
| 358 | + vectorStore.add(List.of(doc1, doc2, doc3)); |
| 359 | + |
| 360 | + // Complex filter expression: (type == 'A' AND priority > 1) |
| 361 | + Filter.Expression priorityFilter = new Filter.Expression(Filter.ExpressionType.GT, |
| 362 | + new Filter.Key("priority"), new Filter.Value(1)); |
| 363 | + Filter.Expression typeFilter = new Filter.Expression(Filter.ExpressionType.EQ, new Filter.Key("type"), |
| 364 | + new Filter.Value("A")); |
| 365 | + Filter.Expression complexFilter = new Filter.Expression(Filter.ExpressionType.AND, typeFilter, |
| 366 | + priorityFilter); |
| 367 | + |
| 368 | + vectorStore.delete(complexFilter); |
| 369 | + |
| 370 | + var results = vectorStore |
| 371 | + .similaritySearch(SearchRequest.builder().query("Content").topK(5).similarityThresholdAll().build()); |
| 372 | + |
| 373 | + assertThat(results).hasSize(2); |
| 374 | + assertThat(results.stream().map(doc -> doc.getMetadata().get("type")).collect(Collectors.toList())) |
| 375 | + .containsExactlyInAnyOrder("A", "B"); |
| 376 | + assertThat(results.stream().map(doc -> doc.getMetadata().get("priority")).collect(Collectors.toList())) |
| 377 | + .containsExactlyInAnyOrder(1, 1); |
| 378 | + }); |
| 379 | + } |
| 380 | + |
276 | 381 | @SpringBootConfiguration |
277 | 382 | @EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class }) |
278 | 383 | public static class TestApplication { |
|
0 commit comments