Skip to content

Commit 0b00e6f

Browse files
rushertzolov
authored andcommitted
feat(vector-store) Add MariaDB Vector Store support to Spring AI
- Updated `org.springframework.boot.autoconfigure.AutoConfiguration.imports` to include MariaDB vector store auto-configuration - Created MariaDB Vector Store autoconfiguration integration tests (`MariaDbStoreAutoConfigurationIT`) - Added MariaDB store properties configuration and tests (`MariaDbStorePropertiesTests`) - Introduced new Maven modules: - `spring-ai-mariadb-store`: Core MariaDB vector store implementation - `spring-ai-starter-mariadb-store`: Spring Boot starter for MariaDB vector store - Added `MariaDBFilterExpressionConverter` to support JSON-based metadata filtering in MariaDB - Implemented filter expression conversion for MariaDB vector store queries - Added README.md with documentation link for MariaDB Vector Store - Updated project dependencies to include MariaDB JDBC driver and test containers - Configured integration testing with TestContainers for MariaDB - Added observability support for MariaDB vector store operations
1 parent d0a0c87 commit 0b00e6f

File tree

27 files changed

+2916
-16
lines changed

27 files changed

+2916
-16
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ You can find more details in the [Reference Documentation](https://docs.spring.i
2525
- [Moderation](https://docs.spring.io/spring-ai/reference/api/index.html#api/moderation)
2626
* Portable API support across AI providers for both synchronous and streaming API options are supported. Access to [model-specific features](https://docs.spring.io/spring-ai/reference/api/chatmodel.html#_chat_options) is also available.
2727
* [Structured Outputs](https://docs.spring.io/spring-ai/reference/api/structured-output-converter.html) - Mapping of AI Model output to POJOs.
28-
* Support for all major [Vector Database providers](https://docs.spring.io/spring-ai/reference/api/vectordbs.html) such as *Apache Cassandra, Azure Vector Search, Chroma, Milvus, MongoDB Atlas, Neo4j, Oracle, PostgreSQL/PGVector, PineCone, Qdrant, Redis, and Weaviate*.
28+
* Support for all major [Vector Database providers](https://docs.spring.io/spring-ai/reference/api/vectordbs.html) such as *Apache Cassandra, Azure Vector Search, Chroma, Milvus, MongoDB Atlas, MariaDB, Neo4j, Oracle, PostgreSQL/PGVector, PineCone, Qdrant, Redis, and Weaviate*.
2929
* Portable API across Vector Store providers, including a novel SQL-like [metadata filter API](https://docs.spring.io/spring-ai/reference/api/vectordbs.html#metadata-filters).
3030
* [Tools/Function Calling](https://docs.spring.io/spring-ai/reference/api/functions.html) - permits the model to request the execution of client-side tools and functions, thereby accessing necessary real-time information as required.
3131
* [Observability](https://docs.spring.io/spring-ai/reference/observability/index.html) - Provides insights into AI-related operations.

pom.xml

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
<module>vector-stores/spring-ai-elasticsearch-store</module>
5353
<module>vector-stores/spring-ai-gemfire-store</module>
5454
<module>vector-stores/spring-ai-hanadb-store</module>
55+
<module>vector-stores/spring-ai-mariadb-store</module>
5556
<module>vector-stores/spring-ai-milvus-store</module>
5657
<module>vector-stores/spring-ai-mongodb-atlas-store</module>
5758
<module>vector-stores/spring-ai-neo4j-store</module>
@@ -73,6 +74,7 @@
7374
<module>spring-ai-spring-boot-starters/spring-ai-starter-elasticsearch-store</module>
7475
<module>spring-ai-spring-boot-starters/spring-ai-starter-gemfire-store</module>
7576
<module>spring-ai-spring-boot-starters/spring-ai-starter-hanadb-store</module>
77+
<module>spring-ai-spring-boot-starters/spring-ai-starter-mariadb-store</module>
7678
<module>spring-ai-spring-boot-starters/spring-ai-starter-milvus-store</module>
7779
<module>spring-ai-spring-boot-starters/spring-ai-starter-mongodb-atlas-store</module>
7880
<module>spring-ai-spring-boot-starters/spring-ai-starter-neo4j-store</module>
@@ -212,6 +214,7 @@
212214
<qdrant.version>1.9.1</qdrant.version>
213215
<typesense.version>0.5.0</typesense.version>
214216
<opensearch-client.version>2.10.1</opensearch-client.version>
217+
<mariadb.version>3.5.1</mariadb.version>
215218
<commonmark.version>0.22.0</commonmark.version>
216219

217220
<!-- also managed by boot bom -->
@@ -678,28 +681,28 @@
678681
<exclude>org.springframework.ai.vectorstore**/Hana**IT.java</exclude>
679682
<exclude>org.springframework.ai.vectorstore**/Hana**IT.java</exclude>
680683
<exclude>org.springframework.ai.vectorstore**/Milvus**IT.java</exclude>
681-
<exclude>org.springframework.ai.vectorstore**/Mongo**IT.java</exclude>
684+
<exclude>org.springframework.ai.vectorstore**/MariaDB**IT.java</exclude>
682685
<exclude>org.springframework.ai.vectorstore**/Mongo**IT.java</exclude>
683686
<exclude>org.springframework.ai.vectorstore**/Neo4j**IT.java</exclude>
684687
<exclude>org.springframework.ai.vectorstore**/OpenSearch**IT.java</exclude>
685688
<exclude>org.springframework.ai.vectorstore**/Oracle**IT.java</exclude>
686689
<!-- <exclude>org.springframework.ai.vectorstore**/PgVector**IT.java</exclude> -->
687-
<exclude>org.springframework.ai.vectorstore**/Pinecone**IT.java</exclude>
690+
<exclude>org.springframework.ai.vectorstore**/Pinecone**IT.java</exclude>
688691
<exclude>org.springframework.ai.vectorstore.qdrant/**/**IT.java</exclude>
689692
<exclude>org.springframework.ai.vectorstore**/Qdrant**IT.java</exclude>
690693
<exclude>org.springframework.ai.vectorstore**/Redis**IT.java</exclude>
691694
<exclude>org.springframework.ai.vectorstore**/Typesense**IT.java</exclude>
692695
<exclude>org.springframework.ai.vectorstore**/Weaviate**IT.java</exclude>
693696

694697
<!-- Auto-configurations-->
695-
698+
696699
<!-- <exclude>org.springframework.ai.autoconfigure/**/**IT.java</exclude> -->
697700

698701
<exclude>org.springframework.ai.autoconfigure.anthropic/**/**IT.java</exclude>
699702
<exclude>org.springframework.ai.autoconfigure.azure/**/**IT.java</exclude>
700703
<exclude>org.springframework.ai.autoconfigure.bedrock/**/**IT.java</exclude>
701704
<exclude>org.springframework.ai.autoconfigure.huggingface/**/**IT.java</exclude>
702-
705+
703706
<exclude>org.springframework.ai.autoconfigure.chat/**/**IT.java</exclude>
704707
<exclude>org.springframework.ai.autoconfigure.embedding/**/**IT.java</exclude>
705708
<exclude>org.springframework.ai.autoconfigure.image/**/**IT.java</exclude>
@@ -713,24 +716,24 @@
713716
<!-- <exclude>org.springframework.ai.autoconfigure.openai/**/**IT.java</exclude> -->
714717
<exclude>org.springframework.ai.autoconfigure.postgresml/**/**IT.java</exclude>
715718
<exclude>org.springframework.ai.autoconfigure.qianfan/**/**IT.java</exclude>
716-
719+
717720
<exclude>org.springframework.ai.autoconfigure.retry/**/**IT.java</exclude>
718-
721+
719722
<exclude>org.springframework.ai.autoconfigure.stabilityai/**/**IT.java</exclude>
720723
<exclude>org.springframework.ai.autoconfigure.transformers/**/**IT.java</exclude>
721-
722-
<exclude>org.springframework.ai.autoconfigure.vectorstore/**/**IT.java</exclude>
723-
724+
725+
<exclude>org.springframework.ai.autoconfigure.vectorstore/**/**IT.java</exclude>
726+
724727
<exclude>org.springframework.ai.autoconfigure.vertexai/**/**IT.java</exclude>
725728
<exclude>org.springframework.ai.autoconfigure.watsonxai/**/**IT.java</exclude>
726729
<exclude>org.springframework.ai.autoconfigure.zhipuai/**/**IT.java</exclude>
727-
730+
728731
<exclude>org.springframework.ai.autoconfigure.zhipuai/**/**IT.java</exclude>
729-
730-
<!-- Test Containers -->
732+
733+
<!-- Test Containers -->
731734
<exclude>org.springframework.ai.testcontainers/**/**IT.java</exclude>
732735

733-
<!-- Test Docker Compose -->
736+
<!-- Test Docker Compose -->
734737
<exclude>package org.springframework.ai.docker.compose/**/**IT.java</exclude>
735738

736739
<!-- AI Evaluation -->

spring-ai-bom/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,12 @@
285285
<version>${project.version}</version>
286286
</dependency>
287287

288+
<dependency>
289+
<groupId>org.springframework.ai</groupId>
290+
<artifactId>spring-ai-mariadb-store</artifactId>
291+
<version>${project.version}</version>
292+
</dependency>
293+
288294
<!-- Utilities -->
289295
<dependency>
290296
<groupId>org.springframework.ai</groupId>
@@ -455,6 +461,12 @@
455461
<version>${project.version}</version>
456462
</dependency>
457463

464+
<dependency>
465+
<groupId>org.springframework.ai</groupId>
466+
<artifactId>spring-ai-mariadb-store-spring-boot-starter</artifactId>
467+
<version>${project.version}</version>
468+
</dependency>
469+
458470
<dependency>
459471
<groupId>org.springframework.ai</groupId>
460472
<artifactId>spring-ai-stability-ai-spring-boot-starter</artifactId>

spring-ai-core/src/main/java/org/springframework/ai/observation/conventions/VectorStoreProvider.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ public enum VectorStoreProvider {
6767
*/
6868
HANA("hana"),
6969

70+
/**
71+
* Vector store provided by MariaDB.
72+
*/
73+
MARIADB("mariadb"),
74+
7075
/**
7176
* Vector store provided by Milvus.
7277
*/

spring-ai-docs/src/main/antora/modules/ROOT/nav.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
** xref:api/vectordbs/chroma.adoc[]
8181
** xref:api/vectordbs/elasticsearch.adoc[]
8282
** xref:api/vectordbs/gemfire.adoc[GemFire]
83+
** xref:api/vectordbs/mariadb.adoc[]
8384
** xref:api/vectordbs/milvus.adoc[]
8485
** xref:api/vectordbs/mongodb.adoc[]
8586
** xref:api/vectordbs/neo4j.adoc[]

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ These are the available implementations of the `VectorStore` interface:
207207
* xref:api/vectordbs/chroma.adoc[Chroma Vector Store] - The https://www.trychroma.com/[Chroma] vector store.
208208
* xref:api/vectordbs/elasticsearch.adoc[Elasticsearch Vector Store] - The https://www.elastic.co/[Elasticsearch] vector store.
209209
* xref:api/vectordbs/gemfire.adoc[GemFire Vector Store] - The https://tanzu.vmware.com/content/blog/vmware-gemfire-vector-database-extension[GemFire] vector store.
210+
* xref:api/vectordbs/mariadb.adoc[MariaDB Vector Store] - The https://mariadb.com/[MariaDB] vector store.
210211
* xref:api/vectordbs/milvus.adoc[Milvus Vector Store] - The https://milvus.io/[Milvus] vector store.
211212
* xref:api/vectordbs/mongodb.adoc[MongoDB Atlas Vector Store] - The https://www.mongodb.com/atlas/database[MongoDB Atlas] vector store.
212213
* xref:api/vectordbs/neo4j.adoc[Neo4j Vector Store] - The https://neo4j.com/[Neo4j] vector store.
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
= MariaDB Vector
2+
3+
This section walks you through setting up the MariaDB `VectorStore` to store document embeddings and perform similarity searches.
4+
5+
link:https://mariadb.org/projects/mariadb-vector/[MariaDB vector] is part of MariaDB 11.7 and enables storing and searching over machine learning-generated embeddings.
6+
7+
== Auto-Configuration
8+
9+
Add the MariaDBVectorStore boot starter dependency to your project:
10+
11+
[source,xml]
12+
----
13+
<dependency>
14+
<groupId>org.springframework.ai</groupId>
15+
<artifactId>spring-ai-mariadb-store-spring-boot-starter</artifactId>
16+
</dependency>
17+
----
18+
19+
or to your Gradle `build.gradle` build file.
20+
21+
[source,groovy]
22+
----
23+
dependencies {
24+
implementation 'org.springframework.ai:spring-ai-mariadb-store-spring-boot-starter'
25+
}
26+
----
27+
28+
The vector store implementation can initialize the required schema for you, but you must opt-in by specifying the `initializeSchema` boolean in the appropriate constructor or by setting `...initialize-schema=true` in the `application.properties` file.
29+
30+
The Vector Store also requires an `EmbeddingModel` instance to calculate embeddings for the documents.
31+
You can pick one of the available xref:api/embeddings.adoc#available-implementations[EmbeddingModel Implementations].
32+
33+
For example, to use the xref:api/embeddings/openai-embeddings.adoc[OpenAI EmbeddingModel], add the following dependency to your project:
34+
35+
[source,xml]
36+
----
37+
<dependency>
38+
<groupId>org.springframework.ai</groupId>
39+
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
40+
</dependency>
41+
----
42+
43+
or to your Gradle `build.gradle` build file.
44+
45+
[source,groovy]
46+
----
47+
dependencies {
48+
implementation 'org.springframework.ai:spring-ai-openai-spring-boot-starter'
49+
}
50+
----
51+
52+
TIP: Refer to the xref:getting-started.adoc#dependency-management[Dependency Management] section to add the Spring AI BOM to your build file.
53+
Refer to the xref:getting-started.adoc#repositories[Repositories] section to add Milestone and/or Snapshot Repositories to your build file.
54+
55+
To connect to and configure the `MariaDBVectorStore`, you need to provide access details for your instance.
56+
A simple configuration can be provided via Spring Boot's `application.yml`.
57+
58+
[yml]
59+
----
60+
spring:
61+
datasource:
62+
url: jdbc:mariadb://localhost/db
63+
username: myUser
64+
password: myPassword
65+
ai:
66+
vectorstore:
67+
mariadbvector:
68+
distance-type: COSINE
69+
dimensions: 1536
70+
----
71+
72+
TIP: If you run MariaDBvector as a Spring Boot dev service via link:https://docs.spring.io/spring-boot/reference/features/dev-services.html#features.dev-services.docker-compose[Docker Compose]
73+
or link:https://docs.spring.io/spring-boot/reference/features/dev-services.html#features.dev-services.testcontainers[Testcontainers],
74+
you don't need to configure URL, username and password since they are autoconfigured by Spring Boot.
75+
76+
TIP: Check the list of xref:#mariadbvector-properties[configuration parameters] to learn about the default values and configuration options.
77+
78+
Now you can auto-wire the `MariaDBVectorStore` in your application and use it
79+
80+
[source,java]
81+
----
82+
@Autowired VectorStore vectorStore;
83+
84+
// ...
85+
86+
List<Document> documents = List.of(
87+
new Document("Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!!", Map.of("meta1", "meta1")),
88+
new Document("The World is Big and Salvation Lurks Around the Corner"),
89+
new Document("You walk forward facing the past and you turn back toward the future.", Map.of("meta2", "meta2")));
90+
91+
// Add the documents to PGVector
92+
vectorStore.add(documents);
93+
94+
// Retrieve documents similar to a query
95+
List<Document> results = this.vectorStore.similaritySearch(SearchRequest.query("Spring").withTopK(5));
96+
----
97+
98+
[[mariadbvector-properties]]
99+
=== Configuration properties
100+
101+
You can use the following properties in your Spring Boot configuration to customize the MariaDB vector store.
102+
103+
[cols="2,5,1",stripes=even]
104+
|===
105+
|Property| Description | Default value
106+
107+
|`spring.ai.vectorstore.mariadb.distance-type`| Search distance type. Defaults to `COSINE`. But if vectors are normalized to length 1, you can use `EUCLIDEAN` for best performance.| COSINE
108+
|`spring.ai.vectorstore.mariadb.dimensions`| Embeddings dimension. If not specified explicitly the PgVectorStore will retrieve the dimensions form the provided `EmbeddingModel`. Dimensions are set to the embedding column the on table creation. If you change the dimensions your would have to re-create the vector_store table as well. | -
109+
|`spring.ai.vectorstore.mariadb.remove-existing-vector-store-table` | Deletes the existing `vector_store` table on start up. | false
110+
|`spring.ai.vectorstore.mariadb.initialize-schema` | Whether to initialize the required schema | false
111+
|`spring.ai.vectorstore.mariadb.schema-name` | Vector store schema name | null
112+
|`spring.ai.vectorstore.mariadb.table-name` | Vector store table name | `vector_store`
113+
|`spring.ai.vectorstore.mariadb.schema-validation` | Enables schema and table name validation to ensure they are valid and existing objects. | false
114+
115+
|===
116+
117+
TIP: If you configure a custom schema and/or table name, consider enabling schema validation by setting `spring.ai.vectorstore.mariadb.schema-validation=true`.
118+
This ensures the correctness of the names and reduces the risk of SQL injection attacks.
119+
120+
== Metadata filtering
121+
122+
You can leverage the generic, portable link:https://docs.spring.io/spring-ai/reference/api/vectordbs.html#_metadata_filters[metadata filters] with the MariaDB Vector store.
123+
124+
For example, you can use either the text expression language:
125+
126+
[source,java]
127+
----
128+
vectorStore.similaritySearch(
129+
SearchRequest.defaults()
130+
.withQuery("The World")
131+
.withTopK(TOP_K)
132+
.withSimilarityThreshold(SIMILARITY_THRESHOLD)
133+
.withFilterExpression("author in ['john', 'jill'] && article_type == 'blog'"));
134+
----
135+
136+
or programmatically using the `Filter.Expression` DSL:
137+
138+
[source,java]
139+
----
140+
FilterExpressionBuilder b = new FilterExpressionBuilder();
141+
142+
vectorStore.similaritySearch(SearchRequest.defaults()
143+
.withQuery("The World")
144+
.withTopK(TOP_K)
145+
.withSimilarityThreshold(SIMILARITY_THRESHOLD)
146+
.withFilterExpression(b.and(
147+
b.in("author","john", "jill"),
148+
b.eq("article_type", "blog")).build()));
149+
----
150+
151+
NOTE: These filter expressions are converted into the equivalent PgVector filters.
152+
153+
== Manual Configuration
154+
155+
Instead of using the Spring Boot auto-configuration, you can manually configure the `MariaDBVectorStore`.
156+
For this you need to add the MariaDB connector and `JdbcTemplate` auto-configuration dependencies to your project:
157+
158+
[source,xml]
159+
----
160+
<dependency>
161+
<groupId>org.springframework.boot</groupId>
162+
<artifactId>spring-boot-starter-jdbc</artifactId>
163+
</dependency>
164+
165+
<dependency>
166+
<groupId>org.mariadb.jdbc</groupId>
167+
<artifactId>mariadb-java-client</artifactId>
168+
<scope>runtime</scope>
169+
</dependency>
170+
171+
<dependency>
172+
<groupId>org.springframework.ai</groupId>
173+
<artifactId>spring-ai-mariadb-store</artifactId>
174+
</dependency>
175+
----
176+
177+
TIP: Refer to the xref:getting-started.adoc#dependency-management[Dependency Management] section to add the Spring AI BOM to your build file.
178+
179+
To configure MariaDB Vector in your application, you can use the following setup:
180+
181+
[source,java]
182+
----
183+
@Bean
184+
public VectorStore vectorStore(JdbcTemplate jdbcTemplate, EmbeddingModel embeddingModel) {
185+
return new MariaDBVectorStore(jdbcTemplate, embeddingModel);
186+
}
187+
----

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
[[introduction]]
2-
= Introduction
32

43
image::spring_ai_logo_with_text.svg[Integration Problem, width=300, align="left"]
54

@@ -26,7 +25,7 @@ Spring AI provides the following features:
2625
** xref:api/audio/speech.adoc[Text to Speech]
2726
** xref:api/moderation[Moderation]
2827
* xref:api/structured-output-converter.adoc[Structured Outputs] - Mapping of AI Model output to POJOs.
29-
* Support for all major xref:api/vectordbs.adoc[Vector Database providers] such as Apache Cassandra, Azure Cosmos DB, Azure Vector Search, Chroma, Elasticsearch, GemFire, Milvus, MongoDB Atlas, Neo4j, OpenSearch, Oracle, PostgreSQL/PGVector, PineCone, Qdrant, Redis, SAP Hana, Typesense and Weaviate.
28+
* Support for all major xref:api/vectordbs.adoc[Vector Database providers] such as Apache Cassandra, Azure Cosmos DB, Azure Vector Search, Chroma, Elasticsearch, GemFire, MariaDB, Milvus, MongoDB Atlas, Neo4j, OpenSearch, Oracle, PostgreSQL/PGVector, PineCone, Qdrant, Redis, SAP Hana, Typesense and Weaviate.
3029
* Portable API across Vector Store providers, including a novel SQL-like metadata filter API.
3130
* xref:api/functions.adoc[Tools/Function Calling] - permits the model to request the execution of client-side tools and functions, thereby accessing necessary real-time information as required.
3231
* xref:observability/index.adoc[Observability] - Provides insights into AI-related operations.

spring-ai-spring-boot-autoconfigure/pom.xml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,20 @@
178178
<optional>true</optional>
179179
</dependency>
180180

181+
<!-- MariaDB Vector Store-->
182+
<dependency>
183+
<groupId>org.springframework.ai</groupId>
184+
<artifactId>spring-ai-mariadb-store</artifactId>
185+
<version>${project.parent.version}</version>
186+
<optional>true</optional>
187+
</dependency>
188+
<dependency>
189+
<groupId>org.mariadb.jdbc</groupId>
190+
<artifactId>mariadb-java-client</artifactId>
191+
<version>${mariadb.version}</version>
192+
<optional>true</optional>
193+
</dependency>
194+
181195
<!-- Chroma Vector Store -->
182196
<dependency>
183197
<groupId>org.springframework.ai</groupId>
@@ -457,6 +471,12 @@
457471
<scope>test</scope>
458472
</dependency>
459473

474+
<dependency>
475+
<groupId>org.testcontainers</groupId>
476+
<artifactId>mariadb</artifactId>
477+
<scope>test</scope>
478+
</dependency>
479+
460480
<dependency>
461481
<groupId>org.testcontainers</groupId>
462482
<artifactId>junit-jupiter</artifactId>

0 commit comments

Comments
 (0)