跳到主要内容

GemFire 向量存储

Deepseek 3.2 中英对照 GemFire GemFire Vector Store

本小节将引导您完成设置 GemFireVectorStore 的过程,用于存储文档嵌入向量并执行相似性搜索。

GemFire 是一个分布式、内存中的键值存储,能以极快的速度执行读写操作。它提供高可用的并行消息队列、持续可用性以及事件驱动的架构,可在无需停机的情况下动态扩展。随着数据量需求的增加以支持高性能、实时应用,GemFire 能够轻松实现线性扩展。

GemFire VectorDB 扩展了 GemFire 的功能,成为一个多功能的向量数据库,能够高效地存储、检索并执行向量相似性搜索。

先决条件

  1. 一个启用了 GemFire VectorDB 扩展的 GemFire 集群

  2. 一个用于计算文档嵌入的 EmbeddingModel Bean。有关更多信息,请参阅 EmbeddingModel 章节。一个可以在您本地机器上运行的选项是 ONNX 和 all-MiniLM-L6-v2 Sentence Transformers。

Auto-configuration

备注

Spring AI 自动配置和 starter 模块的构件名称发生了重大变化。更多信息请参阅 升级说明

将 GemFire VectorStore Spring Boot 启动器添加到您项目的 Maven 构建文件 pom.xml 中:

<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-vector-store-gemfire</artifactId>
</dependency>

或添加到您的 Gradle build.gradle 文件

dependencies {
implementation 'org.springframework.ai:spring-ai-starter-vector-store-gemfire'
}

配置属性

您可以在Spring Boot配置中使用以下属性来进一步配置 GemFireVectorStore

属性默认值
spring.ai.vectorstore.gemfire.hostlocalhost
spring.ai.vectorstore.gemfire.port8080
spring.ai.vectorstore.gemfire.initialize-schemafalse
spring.ai.vectorstore.gemfire.index-namespring-ai-gemfire-store
spring.ai.vectorstore.gemfire.beam-width100
spring.ai.vectorstore.gemfire.max-connections16
spring.ai.vectorstore.gemfire.vector-similarity-functionCOSINE
spring.ai.vectorstore.gemfire.fields[]
spring.ai.vectorstore.gemfire.buckets0
spring.ai.vectorstore.gemfire.usernamenull
spring.ai.vectorstore.gemfire.passwordnull
spring.ai.vectorstore.gemfire.tokennull

手动配置

要单独使用 GemFireVectorStore 而无需Spring Boot的自动配置,请将以下依赖项添加到项目的Maven pom.xml 中:

<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-gemfire-store</artifactId>
</dependency>

对于Gradle用户,要在build.gradle文件的依赖项块中添加以下内容,以仅使用GemFireVectorStore

dependencies {
implementation 'org.springframework.ai:spring-ai-gemfire-store'
}

使用方法

以下是一个创建GemfireVectorStore实例的示例,而非使用自动配置

@Bean
public GemFireVectorStore vectorStore(EmbeddingModel embeddingModel) {
return GemFireVectorStore.builder(embeddingModel)
.host("localhost")
.port(7071)
.username("my-user-name")
.password("my-password")
.indexName("my-vector-index")
.fields(new String[] {"country", "year", "activationDate"}) // Optional: fields for metadata filtering
.initializeSchema(true)
.build();
}
备注

默认配置连接到位于 localhost:8080 的 GemFire 集群

  • 在您的应用程序中,创建一些文档:
List<Document> documents = List.of(
new Document("Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!!", Map.of("country", "UK", "year", 2020)),
new Document("The World is Big and Salvation Lurks Around the Corner", Map.of()),
new Document("You walk forward facing the past and you turn back toward the future.", Map.of("country", "NL", "year", 2023)));
  • 将文档添加到向量存储中:
vectorStore.add(documents);
  • 并且通过相似性搜索来检索文档:
List<Document> results = vectorStore.similaritySearch(
SearchRequest.builder().query("Spring").topK(5).build());

请检索包含文本“Spring AI rocks!!”的文档。

您也可以使用相似度阈值来限制结果数量:

List<Document> results = vectorStore.similaritySearch(
SearchRequest.builder().query("Spring").topK(5)
.similarityThreshold(0.5d).build());

元数据过滤

你可以将通用的、可移植的元数据过滤器与 GemFire VectorStore 结合使用。

例如,您可以使用文本表达式语言:

vectorStore.similaritySearch(SearchRequest.builder()
.query("The World")
.topK(5)
.similarityThreshold(0.7)
.filterExpression("country == 'BG' && year >= 2020").build());

或通过编程方式使用 Filter.Expression DSL:

FilterExpressionBuilder b = new FilterExpressionBuilder();

vectorStore.similaritySearch(SearchRequest.builder()
.query("The World")
.topK(5)
.similarityThreshold(0.7)
.filterExpression(b.and(
b.eq("country", "BG"),
b.gte("year", 2020)).build()).build());
备注

这些(可移植的)过滤器表达式会自动转换为GemFire VectorDB专有的查询格式。

例如,这个便携式过滤器表达式:

country == 'BG' && year >= 2020

被转换为专有的 GemFire VectorDB 过滤器格式:

country:BG AND year:[2020 TO *]

GemFire VectorStore 支持广泛的过滤操作:

  • 相等: country == 'BG'country:BG

  • 不相等: city != 'Sofia'city: NOT Sofia

  • 大于: year > 2020year:{2020 TO *]

  • 大于等于: year >= 2020year:[2020 TO *]

  • 小于: year < 2025year:[* TO 2025}

  • 小于等于: year <= 2025year:[* TO 2025]

  • 属于: country in ['BG', 'NL']country:(BG OR NL)

  • 不属于: country nin ['BG', 'NL']NOT country:(BG OR NL)

  • 与/或: 用于组合条件的逻辑运算符

  • 分组: 使用括号处理复杂表达式

  • 日期过滤: 日期值使用 ISO 8601 格式 (例如 2024-01-07T14:29:12Z)

:::重要
要在 GemFire VectorStore 中使用元数据过滤,你必须在创建向量存储时指定可被过滤的元数据字段。这可以通过构建器中的 fields 参数来实现:

GemFireVectorStore.builder(embeddingModel)
.fields(new String[] {"country", "year", "activationDate"})
.build();

或者通过配置属性来指定:

spring.ai.vectorstore.gemfire.fields=country,year,activationDate

:::