|
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.
|
|
24 | 24 | import java.util.List;
|
25 | 25 | import java.util.Map;
|
26 | 26 | import java.util.UUID;
|
| 27 | +import java.util.stream.Collectors; |
27 | 28 |
|
28 | 29 | import org.junit.jupiter.api.Test;
|
29 | 30 |
|
|
40 | 41 | import org.springframework.ai.transformers.TransformersEmbeddingModel;
|
41 | 42 | import org.springframework.ai.vectorstore.SearchRequest;
|
42 | 43 | import org.springframework.ai.vectorstore.VectorStore;
|
| 44 | +import org.springframework.ai.vectorstore.filter.Filter; |
43 | 45 | import org.springframework.boot.SpringBootConfiguration;
|
44 | 46 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
45 | 47 | import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
@@ -244,6 +246,95 @@ void searchWithThreshold() {
|
244 | 246 | });
|
245 | 247 | }
|
246 | 248 |
|
| 249 | + @Test |
| 250 | + void deleteByFilter() { |
| 251 | + this.contextRunner.run(context -> { |
| 252 | + VectorStore vectorStore = context.getBean(VectorStore.class); |
| 253 | + |
| 254 | + var bgDocument = new Document("The World is Big and Salvation Lurks Around the Corner", |
| 255 | + Map.of("country", "BG", "year", 2020)); |
| 256 | + var nlDocument = new Document("The World is Big and Salvation Lurks Around the Corner", |
| 257 | + Map.of("country", "NL")); |
| 258 | + var bgDocument2 = new Document("The World is Big and Salvation Lurks Around the Corner", |
| 259 | + Map.of("country", "BG", "year", 2023)); |
| 260 | + |
| 261 | + vectorStore.add(List.of(bgDocument, nlDocument, bgDocument2)); |
| 262 | + |
| 263 | + Filter.Expression filterExpression = new Filter.Expression(Filter.ExpressionType.EQ, |
| 264 | + new Filter.Key("country"), new Filter.Value("BG")); |
| 265 | + |
| 266 | + vectorStore.delete(filterExpression); |
| 267 | + |
| 268 | + List<Document> results = vectorStore |
| 269 | + .similaritySearch(SearchRequest.builder().query("The World").topK(5).similarityThresholdAll().build()); |
| 270 | + |
| 271 | + assertThat(results).hasSize(1); |
| 272 | + assertThat(results.get(0).getMetadata()).containsEntry("country", "NL"); |
| 273 | + |
| 274 | + ((TypesenseVectorStore) vectorStore).dropCollection(); |
| 275 | + }); |
| 276 | + } |
| 277 | + |
| 278 | + @Test |
| 279 | + void deleteWithStringFilterExpression() { |
| 280 | + this.contextRunner.run(context -> { |
| 281 | + VectorStore vectorStore = context.getBean(VectorStore.class); |
| 282 | + |
| 283 | + var bgDocument = new Document("The World is Big and Salvation Lurks Around the Corner", |
| 284 | + Map.of("country", "BG", "year", 2020)); |
| 285 | + var nlDocument = new Document("The World is Big and Salvation Lurks Around the Corner", |
| 286 | + Map.of("country", "NL")); |
| 287 | + var bgDocument2 = new Document("The World is Big and Salvation Lurks Around the Corner", |
| 288 | + Map.of("country", "BG", "year", 2023)); |
| 289 | + |
| 290 | + vectorStore.add(List.of(bgDocument, nlDocument, bgDocument2)); |
| 291 | + |
| 292 | + vectorStore.delete("country == 'BG'"); |
| 293 | + |
| 294 | + List<Document> results = vectorStore |
| 295 | + .similaritySearch(SearchRequest.builder().query("The World").topK(5).similarityThresholdAll().build()); |
| 296 | + |
| 297 | + assertThat(results).hasSize(1); |
| 298 | + assertThat(results.get(0).getMetadata()).containsEntry("country", "NL"); |
| 299 | + |
| 300 | + ((TypesenseVectorStore) vectorStore).dropCollection(); |
| 301 | + }); |
| 302 | + } |
| 303 | + |
| 304 | + @Test |
| 305 | + void deleteWithComplexFilterExpression() { |
| 306 | + this.contextRunner.run(context -> { |
| 307 | + VectorStore vectorStore = context.getBean(VectorStore.class); |
| 308 | + |
| 309 | + var doc1 = new Document("Content 1", Map.of("type", "A", "priority", 1)); |
| 310 | + var doc2 = new Document("Content 2", Map.of("type", "A", "priority", 2)); |
| 311 | + var doc3 = new Document("Content 3", Map.of("type", "B", "priority", 1)); |
| 312 | + |
| 313 | + vectorStore.add(List.of(doc1, doc2, doc3)); |
| 314 | + |
| 315 | + // Complex filter expression: (type == 'A' AND priority > 1) |
| 316 | + Filter.Expression priorityFilter = new Filter.Expression(Filter.ExpressionType.GT, |
| 317 | + new Filter.Key("priority"), new Filter.Value(1)); |
| 318 | + Filter.Expression typeFilter = new Filter.Expression(Filter.ExpressionType.EQ, new Filter.Key("type"), |
| 319 | + new Filter.Value("A")); |
| 320 | + Filter.Expression complexFilter = new Filter.Expression(Filter.ExpressionType.AND, typeFilter, |
| 321 | + priorityFilter); |
| 322 | + |
| 323 | + vectorStore.delete(complexFilter); |
| 324 | + |
| 325 | + var results = vectorStore |
| 326 | + .similaritySearch(SearchRequest.builder().query("Content").topK(5).similarityThresholdAll().build()); |
| 327 | + |
| 328 | + assertThat(results).hasSize(2); |
| 329 | + assertThat(results.stream().map(doc -> doc.getMetadata().get("type")).collect(Collectors.toList())) |
| 330 | + .containsExactlyInAnyOrder("A", "B"); |
| 331 | + assertThat(results.stream().map(doc -> doc.getMetadata().get("priority")).collect(Collectors.toList())) |
| 332 | + .containsExactlyInAnyOrder(1, 1); |
| 333 | + |
| 334 | + ((TypesenseVectorStore) vectorStore).dropCollection(); |
| 335 | + }); |
| 336 | + } |
| 337 | + |
247 | 338 | @SpringBootConfiguration
|
248 | 339 | @EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class })
|
249 | 340 | public static class TestApplication {
|
|
0 commit comments