Couchbase
本节将引导您完成设置 CouchbaseSearchVectorStore
的过程,用于存储文档嵌入并利用 Couchbase 执行相似性搜索。
Couchbase 是一种分布式的 JSON 文档数据库,具备关系型数据库管理系统(DBMS)的所有理想特性。其众多功能中,特别值得一提的是,它允许用户通过基于向量的存储与检索方式查询信息。
先决条件
一个正在运行的 Couchbase 实例。以下选项可用:
- Couchbase Docker
- Capella-Couchbase 即服务
- 本地安装 Couchbase
- Couchbase Kubernetes Operator
自动配置(Auto-configuration)
Spring AI 自动配置和启动器模块的工件名称发生了重大变化。 详情请参阅 升级说明 。
Spring AI 为 Couchbase 向量存储提供了 Spring Boot 自动配置。要启用此功能,请将以下依赖项添加到项目的 Maven pom.xml 文件中:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-vector-store-couchbase</artifactId>
</dependency>
或添加到您的 Gradle build.gradle 构建文件中。
dependencies {
implementation 'org.springframework.ai:spring-ai-couchbase-store-spring-boot-starter'
}
向量存储实现能够为您初始化配置的存储桶、作用域、集合和搜索索引,采用默认选项,但您需通过在相应构造函数中指定 initializeSchema
布尔参数来主动选择启用此功能。
这是一项重大变更!在 Spring AI 的早期版本中,默认情况下会进行此模式初始化。
请查看 向量存储的配置参数 列表,以了解默认值和配置选项。
此外,您还需要一个配置好的 EmbeddingModel bean。更多信息请参考 EmbeddingModel 部分。
现在,您可以在应用程序中自动装配 CouchbaseSearchVectorStore 作为向量存储使用。
@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 Qdrant
vectorStore.add(documents);
// Retrieve documents similar to a query
List<Document> results = vectorStore.similaritySearch(SearchRequest.query("Spring").withTopK(5));
配置属性(Configuration Properties)
要连接到 Couchbase 并使用 CouchbaseSearchVectorStore,您需要提供实例的访问详细信息。可以通过 Spring Boot 的 application.properties
进行配置:
spring.ai.openai.api-key=<key>
spring.couchbase.connection-string=<conn_string>
spring.couchbase.username=<username>
spring.couchbase.password=<password>
若您倾向于使用环境变量来管理如密码或 API 密钥等敏感信息,您有多种选择:
选项 1:使用 Spring 表达式语言(SpEL)
您可以使用自定义的环境变量名称,并通过 SpEL 在应用程序配置中引用它们:
# In application.yml
spring:
ai:
openai:
api-key: ${OPENAI_API_KEY}
couchbase:
connection-string: ${COUCHBASE_CONN_STRING}
username: ${COUCHBASE_USER}
password: ${COUCHBASE_PASSWORD}
# In your environment or .env file
export OPENAI_API_KEY=<api-key>
export COUCHBASE_CONN_STRING=<couchbase connection string like couchbase://localhost>
export COUCHBASE_USER=<couchbase username>
export COUCHBASE_PASSWORD=<couchbase password>
选项二:通过编程方式访问环境变量
或者,您可以在 Java 代码中访问环境变量:
String apiKey = System.getenv("OPENAI_API_KEY");
这种方法为您在命名环境变量时提供了灵活性,同时确保敏感信息不会出现在应用程序配置文件中。
若选择创建 Shell 脚本以便未来工作便捷,请确保在启动应用程序前通过 “source” 命令执行该文件,即:source <your_script_name>.sh
。
Spring Boot 的 Couchbase 集群自动配置功能将创建一个 bean 实例,该实例将由 CouchbaseSearchVectorStore 使用。
Spring Boot 中以 spring.couchbase.*
开头的属性用于配置 Couchbase 集群实例:
属性 | 说明 | 默认值 |
---|---|---|
spring.couchbase.connection-string |
Couchbase 连接字符串 | couchbase://localhost |
spring.couchbase.password |
用于 Couchbase 身份验证的密码。 | - |
spring.couchbase.username |
用于 Couchbase 身份验证的用户名。 | - |
spring.couchbase.env.io.minEndpoints |
每个节点的最小套接字数。 | 1 |
spring.couchbase.env.io.maxEndpoints |
每个节点的最大套接字数。 | 12 |
spring.couchbase.env.io.idleHttpConnectionTimeout |
HTTP 连接在关闭并从池中移除前可以空闲的时间长度。 | 1s |
spring.couchbase.env.ssl.enabled |
是否启用 SSL 支持。如果提供了 “bundle”,除非另有说明,否则会自动启用。 | - |
spring.couchbase.env.ssl.bundle |
SSL 包名称。 | - |
spring.couchbase.env.timeouts.connect |
存储桶连接超时时间。 | 10s |
spring.couchbase.env.timeouts.disconnect |
存储桶断开连接超时时间。 | 10s |
spring.couchbase.env.timeouts.key-value |
特定键值操作的超时时间。 (注:此属性在表中重复出现,可能一个是普通操作,一个是持久化操作,见下行) | 2500ms |
spring.couchbase.env.timeouts.key-value-durable |
具有持久性级别的特定键值操作的超时时间。 (此属性更可能是上一行重复属性的正确名称和默认值) | 10s |
spring.couchbase.env.timeouts.query |
SQL++ 查询操作超时时间。 | 75s |
spring.couchbase.env.timeouts.view |
常规和地理空间视图操作超时时间。 | 75s |
spring.couchbase.env.timeouts.search |
搜索服务超时时间。 | 75s |
spring.couchbase.env.timeouts.analytics |
分析服务超时时间。 | 75s |
spring.couchbase.env.timeouts.management |
管理操作超时时间。 | 75s |
以spring.ai.vectorstore.couchbase.*
开头的属性用于配置 CouchbaseSearchVectorStore。
属性 | 说明 | 默认值 |
---|---|---|
spring.ai.vectorstore.couchbase.index-name |
存储向量的索引名称 | spring-ai-document-index |
spring.ai.vectorstore.couchbase.bucket-name |
Couchbase Bucket 的名称,它是 Scope 的父级 | default |
spring.ai.vectorstore.couchbase.scope-name |
Couchbase Scope 的名称,它是 Collection 的父级。搜索查询将在该 Scope 的上下文中执行。 | default |
spring.ai.vectorstore.couchbase.collection-name |
用于存储 Document 的 Couchbase Collection 名称 | default |
spring.ai.vectorstore.couchbase.dimensions |
向量的维度数 | 1536 |
spring.ai.vectorstore.couchbase.similarity |
要使用的相似性函数 | dot_product |
spring.ai.vectorstore.couchbase.optimization |
要使用的相似性函数 (注:此描述与 similarity 属性重复,可能应为“优化目标”,如召回率或速度) |
recall |
spring.ai.vectorstore.couchbase.initialize-schema |
是否初始化所需的 Schema | false |
以下相似度函数可供使用:
- l2_normL2
- dot_product
以下索引优化选项可供使用:
- recall
- latency
元数据过滤(Metadata Filtering)
您可以利用 Couchbase 存储中的通用、便携 元数据过滤器 。
例如,您可以使用文本表达式语言:
vectorStore.similaritySearch(
SearchRequest.defaults()
.query("The World")
.topK(TOP_K)
.filterExpression("author in ['john', 'jill'] && article_type == 'blog'"));
或通过编程方式使用 Filter.Expression DSL:
FilterExpressionBuilder b = new FilterExpressionBuilder();
vectorStore.similaritySearch(SearchRequest.defaults()
.query("The World")
.topK(TOP_K)
.filterExpression(b.and(
b.in("author","john", "jill"),
b.eq("article_type", "blog")).build()));
这些过滤表达式会被转换为等价的 Couchbase SQL++ 过滤器。
手动配置(Manual Configuration)
若不采用 Spring Boot 的自动配置,您可以手动配置 Couchbase 向量存储。为此,需在项目中添加 spring-ai-couchbase-store 依赖:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-couchbase-store</artifactId>
</dependency>
或添加到您的 Gradle build.gradle 构建文件中。
dependencies {
implementation 'org.springframework.ai:spring-ai-couchbase-store'
}
创建一个 Couchbase 集群 Bean。 阅读 Couchbase 文档 以获取关于自定义集群实例配置的更深入信息。
@Bean
public Cluster cluster() {
return Cluster.connect("couchbase://localhost", "username", "password");
}
然后使用构建器模式创建 CouchbaseSearchVectorStore
bean:
@Bean
public VectorStore couchbaseSearchVectorStore(Cluster cluster,
EmbeddingModel embeddingModel,
Boolean initializeSchema) {
return CouchbaseSearchVectorStore
.builder(cluster, embeddingModel)
.bucketName("test")
.scopeName("test")
.collectionName("test")
.initializeSchema(initializeSchema)
.build();
}
// This can be any EmbeddingModel implementation.
@Bean
public EmbeddingModel embeddingModel() {
return new OpenAiEmbeddingModel(OpenAiApi.builder().apiKey(this.openaiKey).build());
}
限制
必须激活以下 Couchbase 服务:
数据、查询、索引、搜索
。虽然仅数据与搜索服务可能已足够,但查询和索引服务对于支持完整的元数据过滤机制是必需的。
最后编辑:Jeebiz 更新时间:2025-09-10 21:18