ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

89_Spring AI 干货笔记之 GemFire 向量存储

2026/8/5 12:22:07 拓冰建站 浏览量
89_Spring AI 干货笔记之 GemFire 向量存储

一、GemFire 向量存储

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

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

GemFire VectorDB 扩展了 GemFire 的能力,作为一个通用的向量数据库,可以高效地存储、检索和执行向量相似性搜索。

二、先决条件

  • 启用了 GemFire VectorDB 扩展的 GemFire 集群,安装 GemFire VectorDB 扩展

  • 一个 EmbeddingModel bean 来计算文档嵌入。有关更多信息,请参阅 EmbeddingModel 部分。一个可在本地机器上运行的选项是 ONNX 和 all-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'}

3.1 配置属性

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

四、手动配置

如果仅使用 GemFireVectorStore 而不使用 Spring Boot 的自动配置,请将以下依赖项添加到项目的 Maven pom.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 实例,而不是使用自动配置:

@BeanpublicGemFireVectorStorevectorStore(EmbeddingModelembeddingModel){returnGemFireVectorStore.builder(embeddingModel).host("localhost").port(7071).username("my-user-name").password("my-password").indexName("my-vector-index").fields(newString[]{"country","year","activationDate"})// 可选:用于元数据过滤的字段.initializeSchema(true).build();}

默认配置连接到 localhost:8080 上的 GemFire 集群。

在应用程序中,创建一些文档:

List<Document>documents=List.of(newDocument("Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!!",Map.of("country","UK","year",2020)),newDocument("The World is Big and Salvation Lurks Around the Corner",Map.of()),newDocument("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("世界").topK(5).similarityThreshold(0.7).filterExpression("country == 'BG' && year >= 2020").build());

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

FilterExpressionBuilderb=newFilterExpressionBuilder();vectorStore.similaritySearch(SearchRequest.builder().query("世界").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 > 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)

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

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

或者通过配置属性:

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