Skip to content

Commit 241cc8b

Browse files
committed
Add filter-based deletion to Oracle vector store
Add string-based filter deletion alongside the Filter.Expression-based deletion for Oracle vector store, providing consistent deletion capabilities with other vector store implementations. Key changes: - Add delete(Filter.Expression) implementation using Oracle JSON_EXISTS - Leverage existing SqlJsonPathFilterExpressionConverter for JSON path expressions - Add comprehensive integration tests for filter deletion - Support both simple and complex filter expressions - Handle Oracle-specific JSON types in test assertions This maintains consistency with other vector store implementations while utilizing Oracle's JSON path capabilities for efficient metadata-based deletion. Signed-off-by: Soby Chacko <[email protected]>
1 parent 8403049 commit 241cc8b

File tree

2 files changed

+128
-3
lines changed

2 files changed

+128
-3
lines changed

vector-stores/spring-ai-oracle-store/src/main/java/org/springframework/ai/vectorstore/oracle/OracleVectorStore.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,13 @@
3939

4040
import org.springframework.ai.document.Document;
4141
import org.springframework.ai.document.DocumentMetadata;
42-
import org.springframework.ai.embedding.BatchingStrategy;
4342
import org.springframework.ai.embedding.EmbeddingModel;
4443
import org.springframework.ai.embedding.EmbeddingOptionsBuilder;
45-
import org.springframework.ai.embedding.TokenCountBatchingStrategy;
4644
import org.springframework.ai.observation.conventions.VectorStoreProvider;
4745
import org.springframework.ai.observation.conventions.VectorStoreSimilarityMetric;
4846
import org.springframework.ai.vectorstore.AbstractVectorStoreBuilder;
4947
import org.springframework.ai.vectorstore.SearchRequest;
48+
import org.springframework.ai.vectorstore.filter.Filter;
5049
import org.springframework.ai.vectorstore.filter.FilterExpressionConverter;
5150
import org.springframework.ai.vectorstore.observation.AbstractObservationVectorStore;
5251
import org.springframework.ai.vectorstore.observation.VectorStoreObservationContext;
@@ -311,6 +310,25 @@ public Optional<Boolean> doDelete(final List<String> idList) {
311310
return Optional.of(deleteCount == idList.size());
312311
}
313312

313+
@Override
314+
protected void doDelete(Filter.Expression filterExpression) {
315+
Assert.notNull(filterExpression, "Filter expression must not be null");
316+
317+
try {
318+
String jsonPath = this.filterExpressionConverter.convertExpression(filterExpression);
319+
String sql = String.format("DELETE FROM %s WHERE JSON_EXISTS(metadata, '%s')", this.tableName, jsonPath);
320+
321+
logger.debug("Executing delete with filter: {}", sql);
322+
323+
int deletedCount = this.jdbcTemplate.update(sql);
324+
logger.debug("Deleted {} documents matching filter expression", deletedCount);
325+
}
326+
catch (Exception e) {
327+
logger.error("Failed to delete documents by filter: {}", e.getMessage(), e);
328+
throw new IllegalStateException("Failed to delete documents by filter", e);
329+
}
330+
}
331+
314332
@Override
315333
public List<Document> doSimilaritySearch(SearchRequest request) {
316334
try {

vector-stores/spring-ai-oracle-store/src/test/java/org/springframework/ai/vectorstore/oracle/OracleVectorStoreIT.java

Lines changed: 108 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023-2024 the original author or authors.
2+
* Copyright 2023-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,12 +23,14 @@
2323
import java.util.List;
2424
import java.util.Map;
2525
import java.util.UUID;
26+
import java.util.stream.Collectors;
2627

2728
import javax.sql.DataSource;
2829

2930
import oracle.jdbc.pool.OracleDataSource;
3031
import org.junit.Assert;
3132
import org.junit.jupiter.api.Disabled;
33+
import org.junit.jupiter.api.Test;
3234
import org.junit.jupiter.params.ParameterizedTest;
3335
import org.junit.jupiter.params.provider.CsvSource;
3436
import org.junit.jupiter.params.provider.ValueSource;
@@ -43,6 +45,7 @@
4345
import org.springframework.ai.transformers.TransformersEmbeddingModel;
4446
import org.springframework.ai.vectorstore.SearchRequest;
4547
import org.springframework.ai.vectorstore.VectorStore;
48+
import org.springframework.ai.vectorstore.filter.Filter;
4649
import org.springframework.ai.vectorstore.filter.FilterExpressionTextParser;
4750
import org.springframework.beans.factory.annotation.Value;
4851
import org.springframework.boot.SpringBootConfiguration;
@@ -313,6 +316,110 @@ public void searchWithThreshold(String distanceType) {
313316
});
314317
}
315318

319+
@Test
320+
void deleteByFilter() {
321+
this.contextRunner
322+
.withPropertyValues("test.spring.ai.vectorstore.oracle.distanceType=COSINE",
323+
"test.spring.ai.vectorstore.oracle.searchAccuracy=" + OracleVectorStore.DEFAULT_SEARCH_ACCURACY)
324+
.run(context -> {
325+
VectorStore vectorStore = context.getBean(VectorStore.class);
326+
327+
var bgDocument = new Document("The World is Big and Salvation Lurks Around the Corner",
328+
Map.of("country", "BG", "year", 2020));
329+
var nlDocument = new Document("The World is Big and Salvation Lurks Around the Corner",
330+
Map.of("country", "NL"));
331+
var bgDocument2 = new Document("The World is Big and Salvation Lurks Around the Corner",
332+
Map.of("country", "BG", "year", 2023));
333+
334+
vectorStore.add(List.of(bgDocument, nlDocument, bgDocument2));
335+
336+
Filter.Expression filterExpression = new Filter.Expression(Filter.ExpressionType.EQ,
337+
new Filter.Key("country"), new Filter.Value("BG"));
338+
339+
vectorStore.delete(filterExpression);
340+
341+
List<Document> results = vectorStore.similaritySearch(
342+
SearchRequest.builder().query("The World").topK(5).similarityThresholdAll().build());
343+
344+
assertThat(results).hasSize(1);
345+
assertThat(results.get(0).getMetadata()).containsKey("country")
346+
.hasEntrySatisfying("country",
347+
value -> assertThat(value.toString().replace("\"", "")).isEqualTo("NL"));
348+
349+
dropTable(context, ((OracleVectorStore) vectorStore).getTableName());
350+
});
351+
}
352+
353+
@Test
354+
void deleteWithStringFilterExpression() {
355+
this.contextRunner
356+
.withPropertyValues("test.spring.ai.vectorstore.oracle.distanceType=COSINE",
357+
"test.spring.ai.vectorstore.oracle.searchAccuracy=" + OracleVectorStore.DEFAULT_SEARCH_ACCURACY)
358+
.run(context -> {
359+
VectorStore vectorStore = context.getBean(VectorStore.class);
360+
361+
var bgDocument = new Document("The World is Big and Salvation Lurks Around the Corner",
362+
Map.of("country", "BG", "year", 2020));
363+
var nlDocument = new Document("The World is Big and Salvation Lurks Around the Corner",
364+
Map.of("country", "NL"));
365+
var bgDocument2 = new Document("The World is Big and Salvation Lurks Around the Corner",
366+
Map.of("country", "BG", "year", 2023));
367+
368+
vectorStore.add(List.of(bgDocument, nlDocument, bgDocument2));
369+
370+
vectorStore.delete("country == 'BG'");
371+
372+
List<Document> results = vectorStore.similaritySearch(
373+
SearchRequest.builder().query("The World").topK(5).similarityThresholdAll().build());
374+
375+
assertThat(results).hasSize(1);
376+
assertThat(results.get(0).getMetadata()).containsKey("country")
377+
.hasEntrySatisfying("country",
378+
value -> assertThat(value.toString().replace("\"", "")).isEqualTo("NL"));
379+
380+
dropTable(context, ((OracleVectorStore) vectorStore).getTableName());
381+
});
382+
}
383+
384+
@Test
385+
void deleteWithComplexFilterExpression() {
386+
this.contextRunner
387+
.withPropertyValues("test.spring.ai.vectorstore.oracle.distanceType=COSINE",
388+
"test.spring.ai.vectorstore.oracle.searchAccuracy=" + OracleVectorStore.DEFAULT_SEARCH_ACCURACY)
389+
.run(context -> {
390+
VectorStore vectorStore = context.getBean(VectorStore.class);
391+
392+
var doc1 = new Document("Content 1", Map.of("type", "A", "priority", 1));
393+
var doc2 = new Document("Content 2", Map.of("type", "A", "priority", 2));
394+
var doc3 = new Document("Content 3", Map.of("type", "B", "priority", 1));
395+
396+
vectorStore.add(List.of(doc1, doc2, doc3));
397+
398+
// Complex filter expression: (type == 'A' AND priority > 1)
399+
Filter.Expression priorityFilter = new Filter.Expression(Filter.ExpressionType.GT,
400+
new Filter.Key("priority"), new Filter.Value(1));
401+
Filter.Expression typeFilter = new Filter.Expression(Filter.ExpressionType.EQ, new Filter.Key("type"),
402+
new Filter.Value("A"));
403+
Filter.Expression complexFilter = new Filter.Expression(Filter.ExpressionType.AND, typeFilter,
404+
priorityFilter);
405+
406+
vectorStore.delete(complexFilter);
407+
408+
var results = vectorStore.similaritySearch(
409+
SearchRequest.builder().query("Content").topK(5).similarityThresholdAll().build());
410+
411+
assertThat(results).hasSize(2);
412+
assertThat(results.stream()
413+
.map(doc -> doc.getMetadata().get("type").toString().replace("\"", ""))
414+
.collect(Collectors.toList())).containsExactlyInAnyOrder("A", "B");
415+
assertThat(results.stream()
416+
.map(doc -> Integer.parseInt(doc.getMetadata().get("priority").toString()))
417+
.collect(Collectors.toList())).containsExactlyInAnyOrder(1, 1);
418+
419+
dropTable(context, ((OracleVectorStore) vectorStore).getTableName());
420+
});
421+
}
422+
316423
@SpringBootConfiguration
317424
@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class })
318425
public static class TestClient {

0 commit comments

Comments
 (0)