|
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.
|
|
23 | 23 | import java.util.List;
|
24 | 24 | import java.util.Map;
|
25 | 25 | import java.util.UUID;
|
| 26 | +import java.util.stream.Collectors; |
26 | 27 |
|
27 | 28 | import javax.sql.DataSource;
|
28 | 29 |
|
29 | 30 | import oracle.jdbc.pool.OracleDataSource;
|
30 | 31 | import org.junit.Assert;
|
31 | 32 | import org.junit.jupiter.api.Disabled;
|
| 33 | +import org.junit.jupiter.api.Test; |
32 | 34 | import org.junit.jupiter.params.ParameterizedTest;
|
33 | 35 | import org.junit.jupiter.params.provider.CsvSource;
|
34 | 36 | import org.junit.jupiter.params.provider.ValueSource;
|
|
43 | 45 | import org.springframework.ai.transformers.TransformersEmbeddingModel;
|
44 | 46 | import org.springframework.ai.vectorstore.SearchRequest;
|
45 | 47 | import org.springframework.ai.vectorstore.VectorStore;
|
| 48 | +import org.springframework.ai.vectorstore.filter.Filter; |
46 | 49 | import org.springframework.ai.vectorstore.filter.FilterExpressionTextParser;
|
47 | 50 | import org.springframework.beans.factory.annotation.Value;
|
48 | 51 | import org.springframework.boot.SpringBootConfiguration;
|
@@ -313,6 +316,120 @@ public void searchWithThreshold(String distanceType) {
|
313 | 316 | });
|
314 | 317 | }
|
315 | 318 |
|
| 319 | + @Test |
| 320 | + void deleteByFilter() { |
| 321 | + this.contextRunner.withPropertyValues("test.spring.ai.vectorstore.oracle.distanceType=COSINE", |
| 322 | + "test.spring.ai.vectorstore.oracle.searchAccuracy=" + OracleVectorStore.DEFAULT_SEARCH_ACCURACY) |
| 323 | + .run(context -> { |
| 324 | + VectorStore vectorStore = context.getBean(VectorStore.class); |
| 325 | + |
| 326 | + var bgDocument = new Document("The World is Big and Salvation Lurks Around the Corner", |
| 327 | + Map.of("country", "BG", "year", 2020)); |
| 328 | + var nlDocument = new Document("The World is Big and Salvation Lurks Around the Corner", |
| 329 | + Map.of("country", "NL")); |
| 330 | + var bgDocument2 = new Document("The World is Big and Salvation Lurks Around the Corner", |
| 331 | + Map.of("country", "BG", "year", 2023)); |
| 332 | + |
| 333 | + vectorStore.add(List.of(bgDocument, nlDocument, bgDocument2)); |
| 334 | + |
| 335 | + Filter.Expression filterExpression = new Filter.Expression(Filter.ExpressionType.EQ, |
| 336 | + new Filter.Key("country"), new Filter.Value("BG")); |
| 337 | + |
| 338 | + vectorStore.delete(filterExpression); |
| 339 | + |
| 340 | + List<Document> results = vectorStore.similaritySearch(SearchRequest.builder() |
| 341 | + .query("The World") |
| 342 | + .topK(5) |
| 343 | + .similarityThresholdAll() |
| 344 | + .build()); |
| 345 | + |
| 346 | + assertThat(results).hasSize(1); |
| 347 | + assertThat(results.get(0).getMetadata()) |
| 348 | + .containsKey("country") |
| 349 | + .hasEntrySatisfying("country", value -> |
| 350 | + assertThat(value.toString().replace("\"", "")).isEqualTo("NL")); |
| 351 | + |
| 352 | + dropTable(context, ((OracleVectorStore) vectorStore).getTableName()); |
| 353 | + }); |
| 354 | + } |
| 355 | + |
| 356 | + @Test |
| 357 | + void deleteWithStringFilterExpression() { |
| 358 | + this.contextRunner.withPropertyValues("test.spring.ai.vectorstore.oracle.distanceType=COSINE", |
| 359 | + "test.spring.ai.vectorstore.oracle.searchAccuracy=" + OracleVectorStore.DEFAULT_SEARCH_ACCURACY) |
| 360 | + .run(context -> { |
| 361 | + VectorStore vectorStore = context.getBean(VectorStore.class); |
| 362 | + |
| 363 | + var bgDocument = new Document("The World is Big and Salvation Lurks Around the Corner", |
| 364 | + Map.of("country", "BG", "year", 2020)); |
| 365 | + var nlDocument = new Document("The World is Big and Salvation Lurks Around the Corner", |
| 366 | + Map.of("country", "NL")); |
| 367 | + var bgDocument2 = new Document("The World is Big and Salvation Lurks Around the Corner", |
| 368 | + Map.of("country", "BG", "year", 2023)); |
| 369 | + |
| 370 | + vectorStore.add(List.of(bgDocument, nlDocument, bgDocument2)); |
| 371 | + |
| 372 | + vectorStore.delete("country == 'BG'"); |
| 373 | + |
| 374 | + List<Document> results = vectorStore.similaritySearch(SearchRequest.builder() |
| 375 | + .query("The World") |
| 376 | + .topK(5) |
| 377 | + .similarityThresholdAll() |
| 378 | + .build()); |
| 379 | + |
| 380 | + assertThat(results).hasSize(1); |
| 381 | + assertThat(results.get(0).getMetadata()) |
| 382 | + .containsKey("country") |
| 383 | + .hasEntrySatisfying("country", value -> |
| 384 | + assertThat(value.toString().replace("\"", "")).isEqualTo("NL")); |
| 385 | + |
| 386 | + dropTable(context, ((OracleVectorStore) vectorStore).getTableName()); |
| 387 | + }); |
| 388 | + } |
| 389 | + |
| 390 | + @Test |
| 391 | + void deleteWithComplexFilterExpression() { |
| 392 | + this.contextRunner.withPropertyValues("test.spring.ai.vectorstore.oracle.distanceType=COSINE", |
| 393 | + "test.spring.ai.vectorstore.oracle.searchAccuracy=" + OracleVectorStore.DEFAULT_SEARCH_ACCURACY) |
| 394 | + .run(context -> { |
| 395 | + VectorStore vectorStore = context.getBean(VectorStore.class); |
| 396 | + |
| 397 | + var doc1 = new Document("Content 1", Map.of("type", "A", "priority", 1)); |
| 398 | + var doc2 = new Document("Content 2", Map.of("type", "A", "priority", 2)); |
| 399 | + var doc3 = new Document("Content 3", Map.of("type", "B", "priority", 1)); |
| 400 | + |
| 401 | + vectorStore.add(List.of(doc1, doc2, doc3)); |
| 402 | + |
| 403 | + // Complex filter expression: (type == 'A' AND priority > 1) |
| 404 | + Filter.Expression priorityFilter = new Filter.Expression(Filter.ExpressionType.GT, |
| 405 | + new Filter.Key("priority"), new Filter.Value(1)); |
| 406 | + Filter.Expression typeFilter = new Filter.Expression(Filter.ExpressionType.EQ, |
| 407 | + new Filter.Key("type"), new Filter.Value("A")); |
| 408 | + Filter.Expression complexFilter = new Filter.Expression(Filter.ExpressionType.AND, |
| 409 | + typeFilter, priorityFilter); |
| 410 | + |
| 411 | + vectorStore.delete(complexFilter); |
| 412 | + |
| 413 | + var results = vectorStore.similaritySearch(SearchRequest.builder() |
| 414 | + .query("Content") |
| 415 | + .topK(5) |
| 416 | + .similarityThresholdAll() |
| 417 | + .build()); |
| 418 | + |
| 419 | + assertThat(results).hasSize(2); |
| 420 | + assertThat(results.stream() |
| 421 | + .map(doc -> doc.getMetadata().get("type").toString().replace("\"", "")) |
| 422 | + .collect(Collectors.toList())) |
| 423 | + .containsExactlyInAnyOrder("A", "B"); |
| 424 | + assertThat(results.stream() |
| 425 | + .map(doc -> Integer.parseInt(doc.getMetadata().get("priority").toString())) |
| 426 | + .collect(Collectors.toList())) |
| 427 | + .containsExactlyInAnyOrder(1, 1); |
| 428 | + |
| 429 | + dropTable(context, ((OracleVectorStore) vectorStore).getTableName()); |
| 430 | + }); |
| 431 | + } |
| 432 | + |
316 | 433 | @SpringBootConfiguration
|
317 | 434 | @EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class })
|
318 | 435 | public static class TestClient {
|
|
0 commit comments