Skip to content
Spring AI 文档版本 2.1.0-SNAPSHOT 源快照 5196bbe3 更新于 2026-10-16

GemFire 向量库

本节引导你设置 GemFireVectorStore 以存储文档嵌入并执行相似度搜索。

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

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

前置条件

  1. 一个启用了 GemFire VectorDB 扩展的 GemFire 集群
  1. 用于计算文档嵌入的 EmbeddingModel bean。更多信息参见 EmbeddingModel 一节。 一个可在本地机器上运行的选项是 ONNX 和 all-MiniLM-L6-v2 Sentence Transformers。

自动配置

NOTE

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

把 GemFire VectorStore 的 Spring Boot starter 添加到你项目的 Maven 构建文件 pom.xml:

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

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

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

配置属性

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

属性
默认值
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
spring.ai.vectorstore.gemfire.username
null
spring.ai.vectorstore.gemfire.password
null
spring.ai.vectorstore.gemfire.token
null

手动配置

要只使用 GemFireVectorStore、而不使用 Spring Boot 的自动配置,把以下依赖添加到你的项目的 Maven pom.xml:

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

对于 Gradle 用户,把以下内容添加到你的 build.gradle 文件的 dependencies 块下,以仅使用 GemFireVectorStore:

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

用量

下面是一个示例,创建 GemfireVectorStore 的实例而非使用 AutoConfiguration

java
@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();
}

NOTE

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

  • 在你的应用中,创建几个文档:
java
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)));
  • 把文档添加到向量库:
java
vectorStore.add(documents);
  • 以及,通过相似度搜索检索文档:
java
List<Document> results = vectorStore.similaritySearch(
   SearchRequest.builder().query("Spring").topK(5).build());

你应能取回包含文本 "Spring AI rocks!!" 的文档。

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

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

元数据过滤

你也可以在 GemFire VectorStore 上利用通用、可移植的元数据过滤器。

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

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

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

java
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());

NOTE

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

例如,这个可移植的过滤表达式:

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

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

country:BG AND year:[2020 TO *]

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

  • 相等:country == 'BG' → country:BG
  • 不相等:city != 'Sofia' → city: NOT Sofia
  • 大于:year > 2020 → year:{2020 TO *]
  • 大于等于:year >= 2020 → year:[2020 TO *]
  • 小于:year < 2025 → year:[* TO 2025}
  • 小于等于:year <= 2025 → year:[* TO 2025]
  • IN:country in ['BG', 'NL'] → country:(BG OR NL)
  • NOT IN:country nin ['BG', 'NL'] → NOT country:(BG OR NL)
  • AND/OR:用于组合条件的逻辑运算符
  • 分组:对复杂表达式使用括号
  • 日期过滤:以 ISO 8601 格式的日期值(例如 2024-01-07T14:29:12Z)

WARNING

要在 GemFire VectorStore 上使用元数据过滤,你必须在创建向量库时指定可过滤的元数据字段。这通过构建器中的 fields 参数完成:

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

或通过配置属性:

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

BytesRouter 是社区翻译站点,所有译文仅供参考,一切以官方英文原文为准。