Skip to content

Commit bffccfc

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 <soby.chacko@broadcom.com>
1 parent 8403049 commit bffccfc

File tree

2 files changed

+140
-3
lines changed

2 files changed

+140
-3
lines changed

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

Lines changed: 22 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,27 @@ 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')",
320+
this.tableName,
321+
jsonPath);
322+
323+
logger.debug("Executing delete with filter: {}", sql);
324+
325+
int deletedCount = this.jdbcTemplate.update(sql);
326+
logger.debug("Deleted {} documents matching filter expression", deletedCount);
327+
}
328+
catch (Exception e) {
329+
logger.error("Failed to delete documents by filter: {}", e.getMessage(), e);
330+
throw new IllegalStateException("Failed to delete documents by filter", e);
331+
}
332+
}
333+
314334
@Override
315335
public List<Document> doSimilaritySearch(SearchRequest request) {
316336
try {

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

Lines changed: 118 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,120 @@ public void searchWithThreshold(String distanceType) {
313316
});
314317
}
315318

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+
316433
@SpringBootConfiguration
317434
@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class })
318435
public static class TestClient {

0 commit comments

Comments
 (0)