|
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. |
|
40 | 40 | import org.springframework.ai.openai.api.OpenAiApi; |
41 | 41 | import org.springframework.ai.vectorstore.SearchRequest; |
42 | 42 | import org.springframework.ai.vectorstore.VectorStore; |
| 43 | +import org.springframework.ai.vectorstore.filter.Filter; |
43 | 44 | import org.springframework.boot.SpringBootConfiguration; |
44 | 45 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration; |
45 | 46 | import org.springframework.boot.test.context.runner.ApplicationContextRunner; |
@@ -255,6 +256,105 @@ public void searchWithThreshold() { |
255 | 256 | }); |
256 | 257 | } |
257 | 258 |
|
| 259 | + @Test |
| 260 | + void deleteByFilter() { |
| 261 | + this.contextRunner.run(context -> { |
| 262 | + VectorStore vectorStore = context.getBean(VectorStore.class); |
| 263 | + |
| 264 | + var bgDocument = new Document("The World is Big and Salvation Lurks Around the Corner", |
| 265 | + Map.of("country", "BG", "year", 2020)); |
| 266 | + var nlDocument = new Document("The World is Big and Salvation Lurks Around the Corner", |
| 267 | + Map.of("country", "NL", "year", 2021)); |
| 268 | + var bgDocument2 = new Document("The World is Big and Salvation Lurks Around the Corner", |
| 269 | + Map.of("country", "BG", "year", 2023)); |
| 270 | + |
| 271 | + vectorStore.add(List.of(bgDocument, nlDocument, bgDocument2)); |
| 272 | + Thread.sleep(5000); // Wait for indexing |
| 273 | + |
| 274 | + SearchRequest searchRequest = SearchRequest.builder() |
| 275 | + .query("The World") |
| 276 | + .topK(5) |
| 277 | + .similarityThresholdAll() |
| 278 | + .build(); |
| 279 | + |
| 280 | + List<Document> results = vectorStore.similaritySearch(searchRequest); |
| 281 | + assertThat(results).hasSize(3); |
| 282 | + |
| 283 | + Filter.Expression filterExpression = new Filter.Expression(Filter.ExpressionType.EQ, |
| 284 | + new Filter.Key("country"), new Filter.Value("BG")); |
| 285 | + |
| 286 | + vectorStore.delete(filterExpression); |
| 287 | + Thread.sleep(1000); // Wait for deletion to be processed |
| 288 | + |
| 289 | + results = vectorStore.similaritySearch(searchRequest); |
| 290 | + assertThat(results).hasSize(1); |
| 291 | + assertThat(results.get(0).getMetadata()).containsEntry("country", "NL"); |
| 292 | + }); |
| 293 | + } |
| 294 | + |
| 295 | + @Test |
| 296 | + void deleteWithStringFilterExpression() { |
| 297 | + this.contextRunner.run(context -> { |
| 298 | + VectorStore vectorStore = context.getBean(VectorStore.class); |
| 299 | + |
| 300 | + var bgDocument = new Document("The World is Big and Salvation Lurks Around the Corner", |
| 301 | + Map.of("country", "BG", "year", 2020)); |
| 302 | + var nlDocument = new Document("The World is Big and Salvation Lurks Around the Corner", |
| 303 | + Map.of("country", "NL", "year", 2021)); |
| 304 | + var bgDocument2 = new Document("The World is Big and Salvation Lurks Around the Corner", |
| 305 | + Map.of("country", "BG", "year", 2023)); |
| 306 | + |
| 307 | + vectorStore.add(List.of(bgDocument, nlDocument, bgDocument2)); |
| 308 | + Thread.sleep(5000); // Wait for indexing |
| 309 | + |
| 310 | + var searchRequest = SearchRequest.builder().query("The World").topK(5).similarityThresholdAll().build(); |
| 311 | + |
| 312 | + List<Document> results = vectorStore.similaritySearch(searchRequest); |
| 313 | + assertThat(results).hasSize(3); |
| 314 | + |
| 315 | + vectorStore.delete("country == 'BG'"); |
| 316 | + Thread.sleep(1000); // Wait for deletion to be processed |
| 317 | + |
| 318 | + results = vectorStore.similaritySearch(searchRequest); |
| 319 | + assertThat(results).hasSize(1); |
| 320 | + assertThat(results.get(0).getMetadata()).containsEntry("country", "NL"); |
| 321 | + }); |
| 322 | + } |
| 323 | + |
| 324 | + @Test |
| 325 | + void deleteWithComplexFilterExpression() { |
| 326 | + this.contextRunner.run(context -> { |
| 327 | + VectorStore vectorStore = context.getBean(VectorStore.class); |
| 328 | + |
| 329 | + var doc1 = new Document("Content 1", Map.of("type", "A", "priority", 1)); |
| 330 | + var doc2 = new Document("Content 2", Map.of("type", "A", "priority", 2)); |
| 331 | + var doc3 = new Document("Content 3", Map.of("type", "B", "priority", 1)); |
| 332 | + |
| 333 | + vectorStore.add(List.of(doc1, doc2, doc3)); |
| 334 | + Thread.sleep(5000); // Wait for indexing |
| 335 | + |
| 336 | + // Complex filter expression: (type == 'A' AND priority > 1) |
| 337 | + Filter.Expression priorityFilter = new Filter.Expression(Filter.ExpressionType.GT, |
| 338 | + new Filter.Key("priority"), new Filter.Value(1)); |
| 339 | + Filter.Expression typeFilter = new Filter.Expression(Filter.ExpressionType.EQ, new Filter.Key("type"), |
| 340 | + new Filter.Value("A")); |
| 341 | + Filter.Expression complexFilter = new Filter.Expression(Filter.ExpressionType.AND, typeFilter, |
| 342 | + priorityFilter); |
| 343 | + |
| 344 | + vectorStore.delete(complexFilter); |
| 345 | + Thread.sleep(1000); // Wait for deletion to be processed |
| 346 | + |
| 347 | + var results = vectorStore |
| 348 | + .similaritySearch(SearchRequest.builder().query("Content").topK(5).similarityThresholdAll().build()); |
| 349 | + |
| 350 | + assertThat(results).hasSize(2); |
| 351 | + assertThat(results.stream().map(doc -> doc.getMetadata().get("type")).collect(Collectors.toList())) |
| 352 | + .containsExactlyInAnyOrder("A", "B"); |
| 353 | + assertThat(results.stream().map(doc -> doc.getMetadata().get("priority")).collect(Collectors.toList())) |
| 354 | + .containsExactlyInAnyOrder(1, 1); |
| 355 | + }); |
| 356 | + } |
| 357 | + |
258 | 358 | public static String getText(String uri) { |
259 | 359 | var resource = new DefaultResourceLoader().getResource(uri); |
260 | 360 | try { |
|
0 commit comments