MariaDB Vector

This section walks you through setting up the MariaDB VectorStore to store document embeddings and perform similarity searches.

MariaDB vector is part of MariaDB 11.7 and enables storing and searching over machine learning-generated embeddings.

Auto-Configuration

Add the MariaDBVectorStore boot starter dependency to your project:

<dependency>
	<groupId>org.springframework.ai</groupId>
	<artifactId>spring-ai-mariadb-store-spring-boot-starter</artifactId>
</dependency>

or to your Gradle build.gradle build file.

dependencies {
    implementation 'org.springframework.ai:spring-ai-mariadb-store-spring-boot-starter'
}

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.

The Vector Store also requires an EmbeddingModel instance to calculate embeddings for the documents. You can pick one of the available EmbeddingModel Implementations.

For example, to use the OpenAI EmbeddingModel, add the following dependency to your project:

<dependency>
	<groupId>org.springframework.ai</groupId>
	<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>

or to your Gradle build.gradle build file.

dependencies {
    implementation 'org.springframework.ai:spring-ai-openai-spring-boot-starter'
}
Refer to the Dependency Management section to add the Spring AI BOM to your build file. Refer to the Repositories section to add Milestone and/or Snapshot Repositories to your build file.

To connect to and configure the MariaDBVectorStore, you need to provide access details for your instance. A simple configuration can be provided via Spring Boot’s application.yml.

spring:
  datasource:
    url: jdbc:mariadb://localhost/db
    username: myUser
    password: myPassword
  ai:
	vectorstore:
	  mariadbvector:
		distance-type: COSINE
		dimensions: 1536
If you run MariaDBvector as a Spring Boot dev service via Docker Compose or Testcontainers, you don’t need to configure URL, username and password since they are autoconfigured by Spring Boot.
Check the list of configuration parameters to learn about the default values and configuration options.

Now you can auto-wire the MariaDBVectorStore in your application and use it

@Autowired VectorStore vectorStore;

// ...

List<Document> documents = List.of(
    new Document("Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!!", Map.of("meta1", "meta1")),
    new Document("The World is Big and Salvation Lurks Around the Corner"),
    new Document("You walk forward facing the past and you turn back toward the future.", Map.of("meta2", "meta2")));

// Add the documents to PGVector
vectorStore.add(documents);

// Retrieve documents similar to a query
List<Document> results = this.vectorStore.similaritySearch(SearchRequest.query("Spring").withTopK(5));

Configuration properties

You can use the following properties in your Spring Boot configuration to customize the MariaDB vector store.

Property Description Default value

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

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.

-

spring.ai.vectorstore.mariadb.remove-existing-vector-store-table

Deletes the existing vector_store table on start up.

false

spring.ai.vectorstore.mariadb.initialize-schema

Whether to initialize the required schema

false

spring.ai.vectorstore.mariadb.schema-name

Vector store schema name

null

spring.ai.vectorstore.mariadb.table-name

Vector store table name

vector_store

spring.ai.vectorstore.mariadb.schema-validation

Enables schema and table name validation to ensure they are valid and existing objects.

false

If you configure a custom schema and/or table name, consider enabling schema validation by setting spring.ai.vectorstore.mariadb.schema-validation=true. This ensures the correctness of the names and reduces the risk of SQL injection attacks.

Metadata filtering

You can leverage the generic, portable metadata filters with the MariaDB Vector store.

For example, you can use either the text expression language:

vectorStore.similaritySearch(
    SearchRequest.defaults()
    .withQuery("The World")
    .withTopK(TOP_K)
    .withSimilarityThreshold(SIMILARITY_THRESHOLD)
    .withFilterExpression("author in ['john', 'jill'] && article_type == 'blog'"));

or programmatically using the Filter.Expression DSL:

FilterExpressionBuilder b = new FilterExpressionBuilder();

vectorStore.similaritySearch(SearchRequest.defaults()
    .withQuery("The World")
    .withTopK(TOP_K)
    .withSimilarityThreshold(SIMILARITY_THRESHOLD)
    .withFilterExpression(b.and(
        b.in("author","john", "jill"),
        b.eq("article_type", "blog")).build()));
These filter expressions are converted into the equivalent PgVector filters.

Manual Configuration

Instead of using the Spring Boot auto-configuration, you can manually configure the MariaDBVectorStore. For this you need to add the MariaDB connector and JdbcTemplate auto-configuration dependencies to your project:

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

<dependency>
	<groupId>org.mariadb.jdbc</groupId>
	<artifactId>mariadb-java-client</artifactId>
	<scope>runtime</scope>
</dependency>

<dependency>
	<groupId>org.springframework.ai</groupId>
	<artifactId>spring-ai-mariadb-store</artifactId>
</dependency>
Refer to the Dependency Management section to add the Spring AI BOM to your build file.

To configure MariaDB Vector in your application, you can use the following setup:

@Bean
public VectorStore vectorStore(JdbcTemplate jdbcTemplate, EmbeddingModel embeddingModel) {
	return new MariaDBVectorStore(jdbcTemplate, embeddingModel);
}