GemFire Vector Store

本节将引导您完成 GemFireVectorStore 的设置,以存储文档嵌入并执行相似性搜索。

GemFire 是一种分布式、内存中的键值存储系统,能以极快的速度执行读写操作。它提供了高可用的并行消息队列、持续可用性以及一种可动态扩展而无需停机的 事件驱动架构 。随着您的数据规模需求增长以支持高性能的实时应用程序,GemFire 能够轻松实现线性扩展。

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

先决条件

启用了 GemFire VectorDB 扩展的 GemFire 集群

安装 GemFire VectorDB 扩展

一个用于计算文档嵌入的 EmbeddingModel 。更多信息请参考 EmbeddingModel 部分。 一个可在本地机器上运行的选项是 ONNXall-MiniLM-L6-v2 句子转换器。

自动配置

Spring AI 自动配置及启动器模块的构件名称已发生重大变更。 更多详情请参阅 升级说明 。

将 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

属性 (Property) 默认值 (Default value)
spring.ai.vectorstore.gemfire.host localhost
spring.ai.vectorstore.gemfire.port 8080
spring.ai.vectorstore.gemfire.initialize-schema false
spring.ai.vectorstore.gemfire.index-name spring-ai-gemfire-store
spring.ai.vectorstore.gemfire.beam-width 100
spring.ai.vectorstore.gemfire.max-connections 16
spring.ai.vectorstore.gemfire.vector-similarity-function COSINE (余弦相似度)
spring.ai.vectorstore.gemfire.fields []
spring.ai.vectorstore.gemfire.buckets 0

手动配置

若仅使用 GemFireVectorStore,且不依赖 Spring Boot 的自动配置,请将以下依赖项添加至项目的 Maven pom.xml 文件中:

org.springframework.ai spring-ai-gemfire-store

对于 Gradle 用户,在‘build.gradle‘文件的‘dependencies‘块中添加以下内容,以仅使用 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)
        .indexName("my-vector-index")
        .initializeSchema(true)
        .build();
}

GemFire VectorStore 目前尚未支持 元数据过滤器 。

默认配置连接到位于 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 太棒了!!” 文本的文档。

您还可以通过设置相似度阈值来限制结果数量:

List<Document> results = vectorStore.similaritySearch(
   SearchRequest.builder().query("Spring").topK(5)
      .similarityThreshold(0.5d).build());
作者:Jeebiz  创建时间:2025-08-03 12:04
最后编辑:Jeebiz  更新时间:2025-09-28 09:15