Skip to content

Commit 16a596f

Browse files
sobychackomarkpollack
authored andcommitted
Add getNativeClient API to VectorStore interface
Adds getNativeClient API to VectorStore interface allowing access to the underlying native client implementation. This change: - Adds getNativeClient() default method to VectorStore interface returning Optional<T> - Implements getNativeClient() in all vector store implementations exposing their respective native clients - Adds integration tests verifying native client access for all implementations Fixes: #2137 Signed-off-by: Soby Chacko <[email protected]>
1 parent 54463e6 commit 16a596f

File tree

53 files changed

+618
-2
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+618
-2
lines changed

spring-ai-core/src/main/java/org/springframework/ai/vectorstore/VectorStore.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,24 @@ default List<Document> similaritySearch(String query) {
108108
return this.similaritySearch(SearchRequest.builder().query(query).build());
109109
}
110110

111+
/**
112+
* Returns the native client if available in this vector store implementation.
113+
*
114+
* Note on usage: 1. Returns empty Optional when no native client is available 2. Due
115+
* to Java type erasure, runtime type checking is not possible
116+
*
117+
* Example usage: When working with implementation with known native client:
118+
* Optional<NativeClientType> client = vectorStore.getNativeClient();
119+
*
120+
* Note: Using Optional<?> will return the native client if one exists, rather than an
121+
* empty Optional. For type safety, prefer using the specific client type.
122+
* @return Optional containing native client if available, empty Optional otherwise
123+
* @param <T> The type of the native client
124+
*/
125+
default <T> Optional<T> getNativeClient() {
126+
return Optional.empty();
127+
}
128+
111129
/**
112130
* Builder interface for creating VectorStore instances. Implements a fluent builder
113131
* pattern for configuring observation-related settings.

spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs.adoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ public interface VectorStore extends DocumentWriter {
4444
List<Document> similaritySearch(String query);
4545

4646
List<Document> similaritySearch(SearchRequest request);
47+
48+
default <T> Optional<T> getNativeClient() {
49+
return Optional.empty();
50+
}
4751
}
4852
```
4953

spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/apache-cassandra.adoc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,3 +319,20 @@ nodetool import wikidata articles ${CASSANDRA_DATA}/data/wikidata/articles-*/
319319
* An alternative to `nodetool import` is to just restart Cassandra.
320320
* If there are any failures in the indexes they will be rebuilt automatically.
321321
====
322+
323+
== Accessing the Native Client
324+
325+
The Cassandra Vector Store implementation provides access to the underlying native Cassandra client (`CqlSession`) through the `getNativeClient()` method:
326+
327+
[source,java]
328+
----
329+
CassandraVectorStore vectorStore = context.getBean(CassandraVectorStore.class);
330+
Optional<CqlSession> nativeClient = vectorStore.getNativeClient();
331+
332+
if (nativeClient.isPresent()) {
333+
CqlSession session = nativeClient.get();
334+
// Use the native client for Cassandra-specific operations
335+
}
336+
----
337+
338+
The native client gives you access to Cassandra-specific features and operations that might not be exposed through the `VectorStore` interface.

spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/azure-cosmos-db.adoc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,3 +205,20 @@ Add the following dependency in your Maven project:
205205
<artifactId>spring-ai-azure-cosmos-db-store</artifactId>
206206
</dependency>
207207
----
208+
209+
== Accessing the Native Client
210+
211+
The Azure Cosmos DB Vector Store implementation provides access to the underlying native Azure Cosmos DB client (`CosmosClient`) through the `getNativeClient()` method:
212+
213+
[source,java]
214+
----
215+
CosmosDBVectorStore vectorStore = context.getBean(CosmosDBVectorStore.class);
216+
Optional<CosmosClient> nativeClient = vectorStore.getNativeClient();
217+
218+
if (nativeClient.isPresent()) {
219+
CosmosClient client = nativeClient.get();
220+
// Use the native client for Azure Cosmos DB-specific operations
221+
}
222+
----
223+
224+
The native client gives you access to Azure Cosmos DB-specific features and operations that might not be exposed through the `VectorStore` interface.

spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/azure.adoc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,3 +226,20 @@ is converted into the following Azure OData link:https://learn.microsoft.com/en-
226226
----
227227
$filter search.in(meta_country, 'UK,NL', ',') and meta_year ge 2020
228228
----
229+
230+
== Accessing the Native Client
231+
232+
The Azure Vector Store implementation provides access to the underlying native Azure Search client (`SearchClient`) through the `getNativeClient()` method:
233+
234+
[source,java]
235+
----
236+
AzureVectorStore vectorStore = context.getBean(AzureVectorStore.class);
237+
Optional<SearchClient> nativeClient = vectorStore.getNativeClient();
238+
239+
if (nativeClient.isPresent()) {
240+
SearchClient client = nativeClient.get();
241+
// Use the native client for Azure Search-specific operations
242+
}
243+
----
244+
245+
The native client gives you access to Azure Search-specific features and operations that might not be exposed through the `VectorStore` interface.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
== Accessing the Native Client
3+
4+
The Coherence Vector Store implementation provides access to the underlying native Coherence client (`Session`) through the `getNativeClient()` method:
5+
6+
[source,java]
7+
----
8+
CoherenceVectorStore vectorStore = context.getBean(CoherenceVectorStore.class);
9+
Optional<Session> nativeClient = vectorStore.getNativeClient();
10+
11+
if (nativeClient.isPresent()) {
12+
Session session = nativeClient.get();
13+
// Use the native client for Coherence-specific operations
14+
}
15+
----
16+
17+
The native client gives you access to Coherence-specific features and operations that might not be exposed through the `VectorStore` interface.

spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/elasticsearch.adoc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,3 +269,20 @@ public EmbeddingModel embeddingModel() {
269269
return new OpenAiEmbeddingModel(new OpenAiApi(System.getenv("OPENAI_API_KEY")));
270270
}
271271
----
272+
273+
== Accessing the Native Client
274+
275+
The Elasticsearch Vector Store implementation provides access to the underlying native Elasticsearch client (`ElasticsearchClient`) through the `getNativeClient()` method:
276+
277+
[source,java]
278+
----
279+
ElasticsearchVectorStore vectorStore = context.getBean(ElasticsearchVectorStore.class);
280+
Optional<ElasticsearchClient> nativeClient = vectorStore.getNativeClient();
281+
282+
if (nativeClient.isPresent()) {
283+
ElasticsearchClient client = nativeClient.get();
284+
// Use the native client for Elasticsearch-specific operations
285+
}
286+
----
287+
288+
The native client gives you access to Elasticsearch-specific features and operations that might not be exposed through the `VectorStore` interface.

spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/mariadb.adoc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,3 +204,20 @@ vectorStore.similaritySearch(SearchRequest.builder()
204204
----
205205

206206
NOTE: These filter expressions are automatically converted into the equivalent MariaDB JSON path expressions.
207+
208+
== Accessing the Native Client
209+
210+
The MariaDB Vector Store implementation provides access to the underlying native JDBC client (`JdbcTemplate`) through the `getNativeClient()` method:
211+
212+
[source,java]
213+
----
214+
MariaDBVectorStore vectorStore = context.getBean(MariaDBVectorStore.class);
215+
Optional<JdbcTemplate> nativeClient = vectorStore.getNativeClient();
216+
217+
if (nativeClient.isPresent()) {
218+
JdbcTemplate jdbc = nativeClient.get();
219+
// Use the native client for MariaDB-specific operations
220+
}
221+
----
222+
223+
The native client gives you access to MariaDB-specific features and operations that might not be exposed through the `VectorStore` interface.

spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/milvus.adoc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,3 +229,20 @@ If Docker complains about resources, then execute:
229229
----
230230
docker system prune --all --force --volumes
231231
----
232+
233+
== Accessing the Native Client
234+
235+
The Milvus Vector Store implementation provides access to the underlying native Milvus client (`MilvusServiceClient`) through the `getNativeClient()` method:
236+
237+
[source,java]
238+
----
239+
MilvusVectorStore vectorStore = context.getBean(MilvusVectorStore.class);
240+
Optional<MilvusServiceClient> nativeClient = vectorStore.getNativeClient();
241+
242+
if (nativeClient.isPresent()) {
243+
MilvusServiceClient client = nativeClient.get();
244+
// Use the native client for Milvus-specific operations
245+
}
246+
----
247+
248+
The native client gives you access to Milvus-specific features and operations that might not be exposed through the `VectorStore` interface.

spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/mongodb.adoc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,3 +228,20 @@ To get started with Spring AI and MongoDB:
228228

229229
* See the https://www.mongodb.com/docs/atlas/atlas-vector-search/ai-integrations/spring-ai/#std-label-spring-ai[Getting Started guide for Spring AI Integration].
230230
* For a comprehensive code example demonstrating Retrieval Augmented Generation (RAG) with Spring AI and MongoDB, refer to this https://www.mongodb.com/developer/languages/java/retrieval-augmented-generation-spring-ai/[detailed tutorial].
231+
232+
== Accessing the Native Client
233+
234+
The MongoDB Atlas Vector Store implementation provides access to the underlying native MongoDB client (`MongoClient`) through the `getNativeClient()` method:
235+
236+
[source,java]
237+
----
238+
MongoDBAtlasVectorStore vectorStore = context.getBean(MongoDBAtlasVectorStore.class);
239+
Optional<MongoClient> nativeClient = vectorStore.getNativeClient();
240+
241+
if (nativeClient.isPresent()) {
242+
MongoClient client = nativeClient.get();
243+
// Use the native client for MongoDB-specific operations
244+
}
245+
----
246+
247+
The native client gives you access to MongoDB-specific features and operations that might not be exposed through the `VectorStore` interface.

0 commit comments

Comments
 (0)