MariaDB Vector Store
本节将引导您设置 MariaDBVectorStore
以存储文档嵌入并执行相似性搜索。
This section walks you through setting up MariaDBVectorStore
to store document embeddings and perform similarity searches.
MariaDB Vector 是 MariaDB 11.7 的一部分,它支持存储和搜索机器学习生成的嵌入。它使用向量索引提供高效的向量相似性搜索功能,支持余弦相似性和欧几里得距离度量。
MariaDB Vector is part of MariaDB 11.7 and enables storing and searching over machine learning-generated embeddings. It provides efficient vector similarity search capabilities using vector indexes, supporting both cosine similarity and Euclidean distance metrics.
Prerequisites
-
一个正在运行的 MariaDB (11.7+) 实例。以下选项可用:
-
A running MariaDB (11.7+) instance. The following options are available:
-
如果需要, EmbeddingModel 的 API 密钥用于生成由
MariaDBVectorStore
存储的嵌入。 -
If required, an API key for the EmbeddingModel to generate the embeddings stored by the
MariaDBVectorStore
.
Auto-Configuration
Spring AI 自动配置、启动器模块的工件名称发生了重大变化。请参阅 upgrade notes 以获取更多信息。 There has been a significant change in the Spring AI auto-configuration, starter modules' artifact names. Please refer to the upgrade notes for more information. |
Spring AI 为 MariaDB 向量存储提供了 Spring Boot 自动配置。要启用它,请将以下依赖项添加到您的项目 Maven pom.xml
文件中:
Spring AI provides Spring Boot auto-configuration for the MariaDB Vector Store.
To enable it, add the following dependency to your project’s Maven pom.xml
file:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-vector-store-mariadb</artifactId>
</dependency>
或添加到 Gradle build.gradle
构建文件中。
or to your Gradle build.gradle
build file.
dependencies {
implementation 'org.springframework.ai:spring-ai-starter-vector-store-mariadb'
}
|
Refer to the Dependency Management section to add the Spring AI BOM to your build file. |
向量存储实现可以为您初始化所需的模式,但您必须通过在相应的构造函数中指定 initializeSchema
布尔值或通过在 application.properties
文件中设置 …initialize-schema=true
来选择加入。
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.
这是一个重大更改!在早期版本的 Spring AI 中,此模式初始化是默认发生的。 |
This is a breaking change! In earlier versions of Spring AI, this schema initialization happened by default. |
此外,您还需要一个配置好的 EmbeddingModel
bean。有关更多信息,请参阅 EmbeddingModel 部分。
Additionally, you will need a configured EmbeddingModel
bean. Refer to the EmbeddingModel section for more information.
例如,要使用 OpenAI EmbeddingModel ,请添加以下依赖项:
For example, to use the OpenAI EmbeddingModel, add the following dependency:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-openai</artifactId>
</dependency>
将Maven Central和/或Snapshot存储库添加到您的构建文件中,请参阅 Artifact Repositories 部分。 |
Refer to the Artifact Repositories section to add Maven Central and/or Snapshot Repositories to your build file. |
现在您可以在应用程序中自动装配 MariaDBVectorStore
:
Now you can auto-wire the MariaDBVectorStore
in your application:
@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 MariaDB
vectorStore.add(documents);
// Retrieve documents similar to a query
List<Document> results = vectorStore.similaritySearch(SearchRequest.builder().query("Spring").topK(5).build());
Configuration Properties
要连接到 MariaDB 并使用 MariaDBVectorStore
,您需要提供实例的访问详细信息。可以通过 Spring Boot 的 application.yml
提供简单的配置:
To connect to MariaDB and use 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:
mariadb:
initialize-schema: true
distance-type: COSINE
dimensions: 1536
如果您通过 Docker Compose 或 Testcontainers 将 MariaDB Vector 作为 Spring Boot 开发服务运行,则无需配置 URL、用户名和密码,因为它们由 Spring Boot 自动配置。 |
If you run MariaDB Vector 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. |
以 spring.ai.vectorstore.mariadb.*
开头的属性用于配置 MariaDBVectorStore
:
Properties starting with spring.ai.vectorstore.mariadb.*
are used to configure the MariaDBVectorStore
:
Property | Description | Default Value |
---|---|---|
|
Whether to initialize the required schema |
|
|
Search distance type. Use |
|
|
Embeddings dimension. If not specified explicitly, will retrieve dimensions from the provided |
|
|
Deletes the existing vector store table on startup. |
|
|
Vector store schema name |
|
|
Vector store table name |
|
|
Enables schema and table name validation to ensure they are valid and existing objects. |
|
如果您配置了自定义模式和/或表名,请考虑通过设置 |
If you configure a custom schema and/or table name, consider enabling schema validation by setting |
Manual Configuration
您可以手动配置 MariaDB 向量存储,而不是使用 Spring Boot 自动配置。为此,您需要将以下依赖项添加到您的项目:
Instead of using the Spring Boot auto-configuration, you can manually configure the MariaDB vector store. For this you need to add the following 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. |
然后使用构建器模式创建 MariaDBVectorStore
bean:
Then create the MariaDBVectorStore
bean using the builder pattern:
@Bean
public VectorStore vectorStore(JdbcTemplate jdbcTemplate, EmbeddingModel embeddingModel) {
return MariaDBVectorStore.builder(jdbcTemplate, embeddingModel)
.dimensions(1536) // Optional: defaults to 1536
.distanceType(MariaDBDistanceType.COSINE) // Optional: defaults to COSINE
.schemaName("mydb") // Optional: defaults to null
.vectorTableName("custom_vectors") // Optional: defaults to "vector_store"
.contentFieldName("text") // Optional: defaults to "content"
.embeddingFieldName("embedding") // Optional: defaults to "embedding"
.idFieldName("doc_id") // Optional: defaults to "id"
.metadataFieldName("meta") // Optional: defaults to "metadata"
.initializeSchema(true) // Optional: defaults to false
.schemaValidation(true) // Optional: defaults to false
.removeExistingVectorStoreTable(false) // Optional: defaults to false
.maxDocumentBatchSize(10000) // Optional: defaults to 10000
.build();
}
// This can be any EmbeddingModel implementation
@Bean
public EmbeddingModel embeddingModel() {
return new OpenAiEmbeddingModel(new OpenAiApi(System.getenv("OPENAI_API_KEY")));
}
Metadata Filtering
您可以利用通用的、可移植的 metadata filters 与 MariaDB 向量存储。
You can leverage the generic, portable metadata filters with MariaDB Vector store.
例如,你可以使用文本表达式语言:
For example, you can use either the text expression language:
vectorStore.similaritySearch(
SearchRequest.builder()
.query("The World")
.topK(TOP_K)
.similarityThreshold(SIMILARITY_THRESHOLD)
.filterExpression("author in ['john', 'jill'] && article_type == 'blog'").build());
或使用 Filter.Expression
DSL 以编程方式:
or programmatically using the Filter.Expression
DSL:
FilterExpressionBuilder b = new FilterExpressionBuilder();
vectorStore.similaritySearch(SearchRequest.builder()
.query("The World")
.topK(TOP_K)
.similarityThreshold(SIMILARITY_THRESHOLD)
.filterExpression(b.and(
b.in("author", "john", "jill"),
b.eq("article_type", "blog")).build()).build());
这些过滤器表达式会自动转换为等效的 MariaDB JSON 路径表达式。 |
These filter expressions are automatically converted into the equivalent MariaDB JSON path expressions. |
Accessing the Native Client
MariaDB 向量存储实现通过 getNativeClient()
方法提供对底层原生 JDBC 客户端 ( JdbcTemplate
) 的访问:
The MariaDB Vector Store implementation provides access to the underlying native JDBC client (JdbcTemplate
) through the getNativeClient()
method:
MariaDBVectorStore vectorStore = context.getBean(MariaDBVectorStore.class);
Optional<JdbcTemplate> nativeClient = vectorStore.getNativeClient();
if (nativeClient.isPresent()) {
JdbcTemplate jdbc = nativeClient.get();
// Use the native client for MariaDB-specific operations
}
原生客户端允许您访问 MariaDB 特定的功能和操作,这些功能和操作可能未通过 VectorStore
接口公开。
The native client gives you access to MariaDB-specific features and operations that might not be exposed through the VectorStore
interface.