|
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,110 @@ 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() |
| 356 | + .query("The World") |
| 357 | + .topK(5) |
| 358 | + .similarityThresholdAll() |
| 359 | + .build(); |
| 360 | + |
| 361 | + List<Document> results = vectorStore.similaritySearch(searchRequest); |
| 362 | + assertThat(results).hasSize(3); |
| 363 | + |
| 364 | + vectorStore.delete("country == 'BG'"); |
| 365 | + |
| 366 | + results = vectorStore.similaritySearch(searchRequest); |
| 367 | + assertThat(results).hasSize(1); |
| 368 | + assertThat(results.get(0).getMetadata()).containsEntry("country", "NL"); |
| 369 | + }); |
| 370 | + } |
| 371 | + |
| 372 | + @Test |
| 373 | + void deleteWithComplexFilterExpression() { |
| 374 | + this.contextRunner.run(context -> { |
| 375 | + VectorStore vectorStore = context.getBean(VectorStore.class); |
| 376 | + |
| 377 | + var doc1 = new Document("Content 1", Map.of("type", "A", "priority", 1L)); |
| 378 | + var doc2 = new Document("Content 2", Map.of("type", "A", "priority", 2L)); |
| 379 | + var doc3 = new Document("Content 3", Map.of("type", "B", "priority", 1L)); |
| 380 | + |
| 381 | + vectorStore.add(List.of(doc1, doc2, doc3)); |
| 382 | + |
| 383 | + // Complex filter expression: (type == 'A' AND priority > 1) |
| 384 | + Filter.Expression priorityFilter = new Filter.Expression(Filter.ExpressionType.GT, |
| 385 | + new Filter.Key("priority"), new Filter.Value(1)); |
| 386 | + Filter.Expression typeFilter = new Filter.Expression(Filter.ExpressionType.EQ, |
| 387 | + new Filter.Key("type"), new Filter.Value("A")); |
| 388 | + Filter.Expression complexFilter = new Filter.Expression(Filter.ExpressionType.AND, |
| 389 | + typeFilter, priorityFilter); |
| 390 | + |
| 391 | + vectorStore.delete(complexFilter); |
| 392 | + |
| 393 | + var results = vectorStore.similaritySearch(SearchRequest.builder() |
| 394 | + .query("Content") |
| 395 | + .topK(5) |
| 396 | + .similarityThresholdAll() |
| 397 | + .build()); |
| 398 | + |
| 399 | + assertThat(results).hasSize(2); |
| 400 | + assertThat(results.stream() |
| 401 | + .map(doc -> doc.getMetadata().get("type")) |
| 402 | + .collect(Collectors.toList())) |
| 403 | + .containsExactlyInAnyOrder("A", "B"); |
| 404 | + assertThat(results.stream() |
| 405 | + .map(doc -> doc.getMetadata().get("priority")) |
| 406 | + .collect(Collectors.toList())) |
| 407 | + .containsExactlyInAnyOrder(1L, 1L); |
| 408 | + }); |
| 409 | + } |
| 410 | + |
304 | 411 | @SpringBootConfiguration |
305 | 412 | @EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class }) |
306 | 413 | public static class TestApplication { |
|
0 commit comments