|
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.
|
|
20 | 20 | import java.util.List;
|
21 | 21 | import java.util.Map;
|
22 | 22 | import java.util.UUID;
|
| 23 | +import java.util.stream.Collectors; |
23 | 24 |
|
24 | 25 | import org.junit.Assert;
|
25 | 26 | import org.junit.jupiter.api.BeforeEach;
|
|
39 | 40 | import org.springframework.ai.openai.api.OpenAiApi;
|
40 | 41 | import org.springframework.ai.vectorstore.SearchRequest;
|
41 | 42 | import org.springframework.ai.vectorstore.VectorStore;
|
| 43 | +import org.springframework.ai.vectorstore.filter.Filter; |
42 | 44 | import org.springframework.ai.vectorstore.filter.FilterExpressionTextParser;
|
43 | 45 | import org.springframework.boot.SpringBootConfiguration;
|
44 | 46 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
|
53 | 55 | * @author Michael Simons
|
54 | 56 | * @author Christian Tzolov
|
55 | 57 | * @author Thomas Vitale
|
| 58 | + * @author Soby Chacko |
56 | 59 | */
|
57 | 60 | @Testcontainers
|
58 | 61 | @EnabledIfEnvironmentVariable(named = "OPENAI_API_KEY", matches = ".+")
|
@@ -301,6 +304,99 @@ void ensureIdIndexGetsCreated() {
|
301 | 304 | .isTrue());
|
302 | 305 | }
|
303 | 306 |
|
| 307 | + @Test |
| 308 | + void deleteByFilter() { |
| 309 | + this.contextRunner.run(context -> { |
| 310 | + VectorStore vectorStore = context.getBean(VectorStore.class); |
| 311 | + |
| 312 | + var bgDocument = new Document("The World is Big and Salvation Lurks Around the Corner", |
| 313 | + Map.of("country", "BG", "year", 2020)); |
| 314 | + var nlDocument = new Document("The World is Big and Salvation Lurks Around the Corner", |
| 315 | + Map.of("country", "NL", "year", 2021)); |
| 316 | + var bgDocument2 = new Document("The World is Big and Salvation Lurks Around the Corner", |
| 317 | + Map.of("country", "BG", "year", 2023)); |
| 318 | + |
| 319 | + vectorStore.add(List.of(bgDocument, nlDocument, bgDocument2)); |
| 320 | + |
| 321 | + SearchRequest searchRequest = SearchRequest.builder() |
| 322 | + .query("The World") |
| 323 | + .topK(5) |
| 324 | + .similarityThresholdAll() |
| 325 | + .build(); |
| 326 | + |
| 327 | + List<Document> results = vectorStore.similaritySearch(searchRequest); |
| 328 | + assertThat(results).hasSize(3); |
| 329 | + |
| 330 | + Filter.Expression filterExpression = new Filter.Expression(Filter.ExpressionType.EQ, |
| 331 | + new Filter.Key("country"), new Filter.Value("BG")); |
| 332 | + |
| 333 | + vectorStore.delete(filterExpression); |
| 334 | + |
| 335 | + results = vectorStore.similaritySearch(searchRequest); |
| 336 | + assertThat(results).hasSize(1); |
| 337 | + assertThat(results.get(0).getMetadata()).containsEntry("country", "NL"); |
| 338 | + }); |
| 339 | + } |
| 340 | + |
| 341 | + @Test |
| 342 | + void deleteWithStringFilterExpression() { |
| 343 | + this.contextRunner.run(context -> { |
| 344 | + VectorStore vectorStore = context.getBean(VectorStore.class); |
| 345 | + |
| 346 | + var bgDocument = new Document("The World is Big and Salvation Lurks Around the Corner", |
| 347 | + Map.of("country", "BG", "year", 2020)); |
| 348 | + var nlDocument = new Document("The World is Big and Salvation Lurks Around the Corner", |
| 349 | + Map.of("country", "NL", "year", 2021)); |
| 350 | + var bgDocument2 = new Document("The World is Big and Salvation Lurks Around the Corner", |
| 351 | + Map.of("country", "BG", "year", 2023)); |
| 352 | + |
| 353 | + vectorStore.add(List.of(bgDocument, nlDocument, bgDocument2)); |
| 354 | + |
| 355 | + var searchRequest = SearchRequest.builder().query("The World").topK(5).similarityThresholdAll().build(); |
| 356 | + |
| 357 | + List<Document> results = vectorStore.similaritySearch(searchRequest); |
| 358 | + assertThat(results).hasSize(3); |
| 359 | + |
| 360 | + vectorStore.delete("country == 'BG'"); |
| 361 | + |
| 362 | + results = vectorStore.similaritySearch(searchRequest); |
| 363 | + assertThat(results).hasSize(1); |
| 364 | + assertThat(results.get(0).getMetadata()).containsEntry("country", "NL"); |
| 365 | + }); |
| 366 | + } |
| 367 | + |
| 368 | + @Test |
| 369 | + void deleteWithComplexFilterExpression() { |
| 370 | + this.contextRunner.run(context -> { |
| 371 | + VectorStore vectorStore = context.getBean(VectorStore.class); |
| 372 | + |
| 373 | + var doc1 = new Document("Content 1", Map.of("type", "A", "priority", 1L)); |
| 374 | + var doc2 = new Document("Content 2", Map.of("type", "A", "priority", 2L)); |
| 375 | + var doc3 = new Document("Content 3", Map.of("type", "B", "priority", 1L)); |
| 376 | + |
| 377 | + vectorStore.add(List.of(doc1, doc2, doc3)); |
| 378 | + |
| 379 | + // Complex filter expression: (type == 'A' AND priority > 1) |
| 380 | + Filter.Expression priorityFilter = new Filter.Expression(Filter.ExpressionType.GT, |
| 381 | + new Filter.Key("priority"), new Filter.Value(1)); |
| 382 | + Filter.Expression typeFilter = new Filter.Expression(Filter.ExpressionType.EQ, new Filter.Key("type"), |
| 383 | + new Filter.Value("A")); |
| 384 | + Filter.Expression complexFilter = new Filter.Expression(Filter.ExpressionType.AND, typeFilter, |
| 385 | + priorityFilter); |
| 386 | + |
| 387 | + vectorStore.delete(complexFilter); |
| 388 | + |
| 389 | + var results = vectorStore |
| 390 | + .similaritySearch(SearchRequest.builder().query("Content").topK(5).similarityThresholdAll().build()); |
| 391 | + |
| 392 | + assertThat(results).hasSize(2); |
| 393 | + assertThat(results.stream().map(doc -> doc.getMetadata().get("type")).collect(Collectors.toList())) |
| 394 | + .containsExactlyInAnyOrder("A", "B"); |
| 395 | + assertThat(results.stream().map(doc -> doc.getMetadata().get("priority")).collect(Collectors.toList())) |
| 396 | + .containsExactlyInAnyOrder(1L, 1L); |
| 397 | + }); |
| 398 | + } |
| 399 | + |
304 | 400 | @SpringBootConfiguration
|
305 | 401 | @EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class })
|
306 | 402 | public static class TestApplication {
|
|
0 commit comments