Skip to content

GH-2137: Add getNativeClient API to VectorStore interface #2161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,24 @@ default List<Document> similaritySearch(String query) {
return this.similaritySearch(SearchRequest.builder().query(query).build());
}

/**
* Returns the native client if available in this vector store implementation.
*
* Note on usage: 1. Returns empty Optional when no native client is available 2. Due
* to Java type erasure, runtime type checking is not possible
*
* Example usage: When working with implementation with known native client:
* Optional<NativeClientType> client = vectorStore.getNativeClient();
*
* Note: Using Optional<?> will return the native client if one exists, rather than an
* empty Optional. For type safety, prefer using the specific client type.
* @return Optional containing native client if available, empty Optional otherwise
* @param <T> The type of the native client
*/
default <T> Optional<T> getNativeClient() {
return Optional.empty();
}

/**
* Builder interface for creating VectorStore instances. Implements a fluent builder
* pattern for configuring observation-related settings.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,13 @@ public VectorStoreObservationContext.Builder createObservationContextBuilder(Str
.similarityMetric("cosine");
}

@Override
public <T> Optional<T> getNativeClient() {
@SuppressWarnings("unchecked")
T client = (T) this.container;
return Optional.of(client);
}

/**
* Builder class for creating {@link CosmosDBVectorStore} instances.
* <p>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023-2024 the original author or authors.
* Copyright 2023-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,9 +19,11 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;

import com.azure.cosmos.CosmosAsyncClient;
import com.azure.cosmos.CosmosAsyncContainer;
import com.azure.cosmos.CosmosClientBuilder;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -169,6 +171,15 @@ void testSimilaritySearchWithFilter() {
assertThat(results4).isEmpty();
}

@Test
void getNativeClientTest() {
this.contextRunner.run(context -> {
CosmosDBVectorStore vectorStore = context.getBean(CosmosDBVectorStore.class);
Optional<CosmosAsyncContainer> nativeClient = vectorStore.getNativeClient();
assertThat(nativeClient).isPresent();
});
}

@SpringBootConfiguration
@EnableAutoConfiguration
public static class TestApplication {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,13 @@ public VectorStoreObservationContext.Builder createObservationContextBuilder(Str
.similarityMetric(this.initializeSchema ? VectorStoreSimilarityMetric.COSINE.value() : null);
}

@Override
public <T> Optional<T> getNativeClient() {
@SuppressWarnings("unchecked")
T client = (T) this.searchClient;
return Optional.of(client);
}

public record MetadataField(String name, SearchFieldDataType fieldType) {

public static MetadataField text(String name) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023-2024 the original author or authors.
* Copyright 2023-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -23,10 +23,12 @@
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

import com.azure.core.credential.AzureKeyCredential;
import com.azure.search.documents.SearchClient;
import com.azure.search.documents.indexes.SearchIndexClient;
import com.azure.search.documents.indexes.SearchIndexClientBuilder;
import org.awaitility.Awaitility;
Expand Down Expand Up @@ -318,6 +320,15 @@ public void searchThresholdTest() {
});
}

@Test
void getNativeClientTest() {
this.contextRunner.run(context -> {
AzureVectorStore vectorStore = context.getBean(AzureVectorStore.class);
Optional<SearchClient> nativeClient = vectorStore.getNativeClient();
assertThat(nativeClient).isPresent();
});
}

@SpringBootConfiguration
@EnableAutoConfiguration
public static class Config {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,13 @@ private void ensureTableColumnsExist(int vectorDimension) {
}
}

@Override
public <T> Optional<T> getNativeClient() {
@SuppressWarnings("unchecked")
T client = (T) this.session;
return Optional.of(client);
}

/**
* Indexes are automatically created with COSINE. This can be changed manually via
* cqlsh
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -524,6 +525,15 @@ void deleteWithComplexFilterExpression() {
});
}

@Test
void getNativeClientTest() {
this.contextRunner.run(context -> {
CassandraVectorStore vectorStore = context.getBean(CassandraVectorStore.class);
Optional<CqlSession> nativeClient = vectorStore.getNativeClient();
assertThat(nativeClient).isPresent();
});
}

@SpringBootConfiguration
@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class })
public static class TestApplication {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,13 @@ public VectorStoreObservationContext.Builder createObservationContextBuilder(Str
.dimensions(this.embeddingModel.dimensions());
}

@Override
public <T> Optional<T> getNativeClient() {
@SuppressWarnings("unchecked")
T client = (T) this.session;
return Optional.of(client);
}

/**
* Builder class for creating {@link CoherenceVectorStore} instances.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Stream;

Expand Down Expand Up @@ -290,6 +291,15 @@ public void searchWithThreshold() {
});
}

@Test
void getNativeClientTest() {
this.contextRunner.run(context -> {
CoherenceVectorStore vectorStore = context.getBean(CoherenceVectorStore.class);
Optional<Session> nativeClient = vectorStore.getNativeClient();
assertThat(nativeClient).isPresent();
});
}

private static boolean isSortedByDistance(final List<Document> documents) {
final List<Double> distances = documents.stream()
.map(doc -> (Double) doc.getMetadata().get(DocumentMetadata.DISTANCE.value()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,13 @@ private String getSimilarityMetric() {
return SIMILARITY_TYPE_MAPPING.get(this.options.getSimilarity()).value();
}

@Override
public <T> Optional<T> getNativeClient() {
@SuppressWarnings("unchecked")
T client = (T) this.elasticsearchClient;
return Optional.of(client);
}

/**
* Creates a new builder instance for ElasticsearchVectorStore.
* @return a new ElasticsearchBuilder instance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -529,6 +530,26 @@ public void overDefaultSizeTest() {
});
}

@Test
public void getNativeClientTest() {
getContextRunner().run(context -> {
ElasticsearchVectorStore vectorStore = context.getBean("vectorStore_cosine",
ElasticsearchVectorStore.class);

// Test successful native client retrieval
Optional<ElasticsearchClient> nativeClient = vectorStore.getNativeClient();
assertThat(nativeClient).isPresent();

// Verify client functionality
ElasticsearchClient client = nativeClient.get();
IndicesStats stats = client.indices()
.stats(s -> s.index("spring-ai-document-index"))
.indices()
.get("spring-ai-document-index");
assertThat(stats).isNotNull();
});
}

@SpringBootConfiguration
@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class })
public static class TestApplication {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,13 @@ private String getSimilarityMetric() {
return SIMILARITY_TYPE_MAPPING.get(this.distanceType).value();
}

@Override
public <T> Optional<T> getNativeClient() {
@SuppressWarnings("unchecked")
T client = (T) this.jdbcTemplate;
return Optional.of(client);
}

public enum MariaDBDistanceType {

EUCLIDEAN, COSINE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand Down Expand Up @@ -461,6 +462,15 @@ public void deleteWithComplexFilterExpression() {
});
}

@Test
void getNativeClientTest() {
this.contextRunner.run(context -> {
MariaDBVectorStore vectorStore = context.getBean(MariaDBVectorStore.class);
Optional<JdbcTemplate> nativeClient = vectorStore.getNativeClient();
assertThat(nativeClient).isPresent();
});
}

@SpringBootConfiguration
@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class })
public static class TestApplication {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,13 @@ private String getSimilarityMetric() {
return SIMILARITY_TYPE_MAPPING.get(this.metricType).value();
}

@Override
public <T> Optional<T> getNativeClient() {
@SuppressWarnings("unchecked")
T client = (T) this.milvusClient;
return Optional.of(client);
}

public static class Builder extends AbstractVectorStoreBuilder<Builder> {

private final MilvusServiceClient milvusClient;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -378,6 +379,15 @@ public void deleteWithComplexFilterExpression() {
});
}

@Test
void getNativeClientTest() {
this.contextRunner.withPropertyValues("test.spring.ai.vectorstore.milvus.metricType=COSINE").run(context -> {
MilvusVectorStore vectorStore = context.getBean(MilvusVectorStore.class);
Optional<MilvusServiceClient> nativeClient = vectorStore.getNativeClient();
assertThat(nativeClient).isPresent();
});
}

@SpringBootConfiguration
@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class })
public static class TestApplication {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,13 @@ public VectorStoreObservationContext.Builder createObservationContextBuilder(Str
.fieldName(this.pathName);
}

@Override
public <T> Optional<T> getNativeClient() {
@SuppressWarnings("unchecked")
T client = (T) this.mongoTemplate;
return Optional.of(client);
}

/**
* Creates a new builder instance for MongoDBAtlasVectorStore.
* @return a new MongoDBBuilder instance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -355,6 +356,15 @@ void deleteWithComplexFilterExpression() {
});
}

@Test
void getNativeClientTest() {
this.contextRunner.run(context -> {
MongoDBAtlasVectorStore vectorStore = context.getBean(MongoDBAtlasVectorStore.class);
Optional<MongoTemplate> nativeClient = vectorStore.getNativeClient();
assertThat(nativeClient).isPresent();
});
}

public static String getText(String uri) {
var resource = new DefaultResourceLoader().getResource(uri);
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,13 @@ private String getSimilarityMetric() {
return SIMILARITY_TYPE_MAPPING.get(this.distanceType).value();
}

@Override
public <T> Optional<T> getNativeClient() {
@SuppressWarnings("unchecked")
T client = (T) this.driver;
return Optional.of(client);
}

/**
* An enum to configure the distance function used in the Neo4j vector index.
*/
Expand Down
Loading