|
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.
|
|
37 | 37 | import org.springframework.ai.transformers.TransformersEmbeddingModel;
|
38 | 38 | import org.springframework.ai.vectorstore.SearchRequest;
|
39 | 39 | import org.springframework.ai.vectorstore.VectorStore;
|
| 40 | +import org.springframework.ai.vectorstore.filter.Filter; |
40 | 41 | import org.springframework.boot.SpringBootConfiguration;
|
41 | 42 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
42 | 43 | import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
@@ -254,6 +255,62 @@ public void searchWithThreshold() {
|
254 | 255 | });
|
255 | 256 | }
|
256 | 257 |
|
| 258 | + @Test |
| 259 | + void deleteByFilter() { |
| 260 | + this.contextRunner.run(context -> { |
| 261 | + VectorStore vectorStore = context.getBean(VectorStore.class); |
| 262 | + |
| 263 | + var bgDocument = new Document("The World is Big and Salvation Lurks Around the Corner", |
| 264 | + Map.of("country", "BG", "year", 2020)); |
| 265 | + var nlDocument = new Document("The World is Big and Salvation Lurks Around the Corner", |
| 266 | + Map.of("country", "NL")); |
| 267 | + var bgDocument2 = new Document("The World is Big and Salvation Lurks Around the Corner", |
| 268 | + Map.of("country", "BG", "year", 2023)); |
| 269 | + |
| 270 | + vectorStore.add(List.of(bgDocument, nlDocument, bgDocument2)); |
| 271 | + |
| 272 | + Filter.Expression filterExpression = new Filter.Expression(Filter.ExpressionType.EQ, |
| 273 | + new Filter.Key("country"), new Filter.Value("BG")); |
| 274 | + |
| 275 | + vectorStore.delete(filterExpression); |
| 276 | + |
| 277 | + List<Document> results = vectorStore |
| 278 | + .similaritySearch(SearchRequest.builder().query("The World").topK(5).similarityThresholdAll().build()); |
| 279 | + |
| 280 | + assertThat(results).hasSize(1); |
| 281 | + assertThat(results.get(0).getMetadata()).containsEntry("country", "NL"); |
| 282 | + |
| 283 | + vectorStore.delete(List.of(nlDocument.getId())); |
| 284 | + |
| 285 | + }); |
| 286 | + } |
| 287 | + |
| 288 | + @Test |
| 289 | + void deleteWithStringFilterExpression() { |
| 290 | + this.contextRunner.run(context -> { |
| 291 | + VectorStore vectorStore = context.getBean(VectorStore.class); |
| 292 | + |
| 293 | + var bgDocument = new Document("The World is Big and Salvation Lurks Around the Corner", |
| 294 | + Map.of("country", "BG", "year", 2020)); |
| 295 | + var nlDocument = new Document("The World is Big and Salvation Lurks Around the Corner", |
| 296 | + Map.of("country", "NL")); |
| 297 | + var bgDocument2 = new Document("The World is Big and Salvation Lurks Around the Corner", |
| 298 | + Map.of("country", "BG", "year", 2023)); |
| 299 | + |
| 300 | + vectorStore.add(List.of(bgDocument, nlDocument, bgDocument2)); |
| 301 | + |
| 302 | + vectorStore.delete("country == 'BG'"); |
| 303 | + |
| 304 | + List<Document> results = vectorStore |
| 305 | + .similaritySearch(SearchRequest.builder().query("The World").topK(5).similarityThresholdAll().build()); |
| 306 | + |
| 307 | + assertThat(results).hasSize(1); |
| 308 | + assertThat(results.get(0).getMetadata()).containsEntry("country", "NL"); |
| 309 | + |
| 310 | + vectorStore.delete(List.of(nlDocument.getId())); |
| 311 | + }); |
| 312 | + } |
| 313 | + |
257 | 314 | @SpringBootConfiguration
|
258 | 315 | @EnableAutoConfiguration
|
259 | 316 | public static class TestApplication {
|
|
0 commit comments