Skip to content

Commit b30713e

Browse files
sobychackomarkpollack
authored andcommitted
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 07b9058 commit b30713e

File tree

2 files changed

+129
-2
lines changed

2 files changed

+129
-2
lines changed

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import org.springframework.ai.observation.conventions.VectorStoreSimilarityMetric;
4444
import org.springframework.ai.vectorstore.AbstractVectorStoreBuilder;
4545
import org.springframework.ai.vectorstore.SearchRequest;
46+
import org.springframework.ai.vectorstore.filter.Filter;
4647
import org.springframework.ai.vectorstore.filter.FilterExpressionConverter;
4748
import org.springframework.ai.vectorstore.observation.AbstractObservationVectorStore;
4849
import org.springframework.ai.vectorstore.observation.VectorStoreObservationContext;
@@ -308,6 +309,25 @@ public Optional<Boolean> doDelete(final List<String> idList) {
308309
return Optional.of(deleteCount == idList.size());
309310
}
310311

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

602622
}
603623

604-
private static class DocumentRowMapper implements RowMapper<Document> {
624+
private final static class DocumentRowMapper implements RowMapper<Document> {
605625

606626
@Override
607627
public Document mapRow(ResultSet rs, int rowNum) throws SQLException {

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)