Providers
Spring AI 支持 20 种向量数据库实现,通过 spring.ai.vectorstore.type 配置属性切换,所有实现遵循统一的 VectorStore 接口。
1. 厂商总览
| 类型标识 | 厂商 | 部署模式 | 特点 |
|---|---|---|---|
pgvector | PGVector | 自托管 | PostgreSQL 扩展,最流行的开源方案 |
pinecone | Pinecone | 云服务 | 全托管向量数据库,gRPC API |
qdrant | Qdrant | 自托管/云 | 高性能 Rust 实现,支持量化索引 |
milvus | Milvus | 自托管/云 | 分布式向量数据库,支持多种索引 |
redis | Redis | 自托管/云 | RediSearch + RedisJSON 模块 |
chroma | Chroma | 自托管 | 轻量级,适合原型开发 |
elasticsearch | Elasticsearch | 自托管/云 | 全文搜索 + 向量搜索一体 |
opensearch | OpenSearch | 自托管/云 | AWS 托管的 Elasticsearch 分支 |
azure | Azure AI Search | 云服务 | Azure 原生向量搜索 |
azure-cosmos-db | Azure Cosmos DB | 云服务 | Azure 多模数据库 |
cassandra | Cassandra | 自托管 | 分布式 NoSQL,适合大规模数据 |
coherence | Coherence | 自托管 | Oracle Coherence 内存网格 |
couchbase | Couchbase | 自托管/云 | 分布式 NoSQL 数据库 |
gemfire | GemFire | 自托管 | VMware 内存数据网格 |
hanadb | SAP HANA | 自托管 | 企业级内存数据库 |
mariadb | MariaDB | 自托管 | MySQL 分支,向量扩展 |
mongodb-atlas | MongoDB Atlas | 云服务 | MongoDB 托管向量搜索 |
neo4j | Neo4j | 自托管/云 | 图数据库 + 向量索引 |
oracle | Oracle | 自托管/云 | Oracle 23ai AI Vector Search |
typesense | Typesense | 自托管/云 | 轻量级搜索引擎 |
weaviate | Weaviate | 自托管/云 | 原生向量数据库,Hybrid Search |
2. 通用配置模式
所有向量数据库共享三个配置层级:
spring.ai.vectorstore.type: pgvector # ① 选择厂商
spring.ai.vectorstore.pgvector: # ② 厂商专属配置
initialize-schema: true
dimensions: 1536
distance-type: COSINE_DISTANCE
index-type: HNSW
spring.ai.vectorstore.observations: # ③ 可观测性配置
log-query-response: false
自动装配条件:
- 类路径存在对应厂商的 SDK 依赖(
@ConditionalOnClass) spring.ai.vectorstore.type属性匹配- 所有实现均设置
matchIfMissing = true——即未显式设置 type 时,类路径存在哪个厂商的依赖就装配哪个
Starter 依赖格式: spring-ai-starter-vector-store-{type},例如 PGVector 的依赖为 spring-ai-starter-vector-store-pgvector。
3. PGVector
PostgreSQL 的向量扩展,是 Spring AI 中最成熟的自托管方案。使用 JSONB 存储元数据,支持三种索引类型和三种距离算法。
依赖配置
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-vector-store-pgvector</artifactId>
</dependency>
spring.ai.vectorstore.type: pgvector
spring.ai.vectorstore.pgvector:
initialize-schema: true
schema-name: public
table-name: vector_store
dimensions: 1536
id-type: UUID
index-type: HNSW
distance-type: COSINE_DISTANCE
remove-existing-vector-store-table: false
schema-validation: false
max-document-batch-size: 10000
专属配置
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
schema-name | String | public | PostgreSQL schema 名称 |
table-name | String | vector_store | 向量存储表名 |
dimensions | int | -1(自动检测) | 向量维度,-1 表示从 EmbeddingModel 自动获取 |
index-type | PgIndexType | HNSW | 索引类型 |
distance-type | PgDistanceType | COSINE_DISTANCE | 距离算法 |
id-type | PgIdType | UUID | 主键 ID 类型 |
remove-existing-vector-store-table | boolean | false | 启动时是否删除已有表 |
schema-validation | boolean | false | 是否校验表结构一致性 |
max-document-batch-size | int | 10000 | 单次写入最大文档数 |
索引类型(PgIndexType)
| 值 | 说明 |
|---|---|
NONE | 不建立索引,执行精确搜索(适合小数据集) |
IVFFLAT | 倒排文件索引,构建快,适合百万级数据 |
HNSW | 分层可导航小世界图,查询性能最优,构建较慢 |
距离算法(PgDistanceType)
| 值 | PostgreSQL 运算符 | 适用场景 |
|---|---|---|
COSINE_DISTANCE | <=> | 语义相似度(推荐) |
EUCLIDEAN_DISTANCE | <-> | 向量空间距离 |
NEGATIVE_INNER_PRODUCT | <#> | 点积相似度 |
ID 类型(PgIdType)
UUID(默认)、TEXT、INTEGER、SERIAL、BIGSERIAL。使用 UUID 可避免与业务主键冲突,SERIAL 则适合需要自增整数的场景。
手动创建
var store = PgVectorStore.builder(jdbcTemplate, embeddingModel)
.schemaName("public")
.vectorTableName("my_documents")
.dimensions(1536)
.distanceType(PgVectorStore.PgDistanceType.COSINE_DISTANCE)
.indexType(PgVectorStore.PgIndexType.HNSW)
.idType(PgVectorStore.PgIdType.UUID)
.initializeSchema(true)
.build();
4. Pinecone
Pinecone 是全托管云向量数据库,通过 gRPC API 通信,免运维。
依赖配置
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-vector-store-pinecone</artifactId>
</dependency>
spring.ai.vectorstore.type: pinecone
spring.ai.vectorstore.pinecone:
api-key: ${PINECONE_API_KEY}
index-name: my-index
namespace: my-namespace
content-field-name: document_content
distance-metadata-field-name: distance
专属配置
| 属性 | 说明 |
|---|---|
api-key | Pinecone API 密钥(必需) |
index-name | Pinecone 索引名称(必需) |
namespace | 命名空间,用于逻辑隔离(可选,默认空字符串) |
content-field-name | 文档内容在 metadata 中的字段名,默认 "document_content" |
distance-metadata-field-name | 距离得分的 metadata 字段名,默认 "distance" |
注意: Pinecone API 不支持按条件直接删除文档,Spring AI 的实现通过先搜索再按 ID 删除来模拟此功能。
手动创建
var store = PineconeVectorStore.builder(pineconeApi, embeddingModel)
.namespace("my-namespace")
.contentFieldName("doc_content")
.build();
5. Qdrant
Qdrant 是 Rust 实现的高性能向量数据库,支持本地部署和 Qdrant Cloud。
依赖配置
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-vector-store-qdrant</artifactId>
</dependency>
spring.ai.vectorstore.type: qdrant
spring.ai.vectorstore.qdrant:
host: localhost
port: 6334
api-key: ${QDRANT_API_KEY}
use-tls: false
collection-name: vector_store
initialize-schema: true
专属配置
| 属性 | 默认值 | 说明 |
|---|---|---|
host | localhost | Qdrant 服务地址 |
port | 6334 | gRPC 端口 |
api-key | - | API 密钥(Qdrant Cloud 必需) |
use-tls | false | 是否启用 TLS |
collection-name | vector_store | 集合名称 |
手动创建
var store = QdrantVectorStore.builder(qdrantClient, embeddingModel)
.collectionName("my_collection")
.initializeSchema(true)
.build();
6. Milvus
Milvus 是分布式向量数据库,支持多种索引类型和度量方式,兼容 Zilliz Cloud。
依赖配置
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-vector-store-milvus</artifactId>
</dependency>
spring.ai.vectorstore.type: milvus
spring.ai.vectorstore.milvus:
host: localhost
port: 19530
database-name: default
collection-name: vector_store
embedding-dimension: 1536
index-type: IVF_FLAT
metric-type: COSINE
index-parameters: '{"nlist": 1024}'
initialize-schema: true
专属配置
| 属性 | 默认值 | 说明 |
|---|---|---|
host | localhost | Milvus 服务地址 |
port | 19530 | Milvus 端口 |
database-name | default | 数据库名称 |
collection-name | vector_store | 集合名称 |
embedding-dimension | -1 | 向量维度,-1 自动检测 |
index-type | IVF_FLAT | 索引类型 |
metric-type | COSINE | 度量类型:COSINE、L2、IP |
index-parameters | {"nlist":1024} | 索引参数 JSON |
Milvus 专属搜索请求
Milvus 提供了 SearchRequest 的扩展,支持原生 Milvus 过滤表达式和搜索参数:
List<Document> results = vectorStore.similaritySearch(
MilvusSearchRequest.builder()
.query("Spring AI 是什么")
.topK(5)
.nativeExpression("category == \"documentation\"")
.searchParamsJson("{\"nprobe\": 128}")
.build()
);
7. Redis
Redis 向量存储需要 Redis Stack(包含 RediSearch 和 RedisJSON 模块)。
依赖配置
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-vector-store-redis</artifactId>
</dependency>
spring.ai.vectorstore.type: redis
spring.ai.vectorstore.redis:
host: localhost
port: 6379
index-name: spring-ai-index
prefix: "embedding:"
content-field-name: content
embedding-field-name: embedding
vector-algorithm: HSNW
initialize-schema: true
专属配置
| 属性 | 默认值 | 说明 |
|---|---|---|
host | localhost | Redis 服务地址 |
port | 6379 | Redis 端口 |
index-name | spring-ai-index | RediSearch 索引名称 |
prefix | embedding: | 文档 Key 前缀 |
content-field-name | content | 文档内容字段名 |
embedding-field-name | embedding | 向量字段名 |
vector-algorithm | HSNW | 向量索引算法:HSNW 或 FLAT |
元数据字段配置
Redis 支持额外的元数据索引字段配置,为元数据字段建立二级索引以加速过滤查询:
var store = RedisVectorStore.builder(jedis, embeddingModel)
.metadataFields(
MetadataField.tag("category"),
MetadataField.numeric("year"),
MetadataField.text("title")
)
.build();
MetadataField 类型:
| 类型 | 方法 | 说明 |
|---|---|---|
tag | MetadataField.tag(name) | 标签字段,支持精确匹配 |
numeric | MetadataField.numeric(name) | 数值字段,支持范围查询 |
text | MetadataField.text(name) | 文本字段,支持全文搜索 |
8. Chroma
Chroma 是轻量级开源向量数据库,REST API 通信,适合本地开发和小规模应用。
依赖配置
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-vector-store-chroma</artifactId>
</dependency>
spring.ai.vectorstore.type: chroma
spring.ai.vectorstore.chroma:
client:
host: http://localhost
port: 8000
key-token: ${CHROMA_API_KEY}
tenant-name: default_tenant
database-name: default_database
collection-name: default_collection
initialize-schema: true
专属配置
| 属性 | 默认值 | 说明 |
|---|---|---|
client.host | http://localhost | Chroma 服务地址 |
client.port | 8000 | Chroma 端口 |
client.key-token | - | API 密钥 |
tenant-name | default_tenant | 租户名称 |
database-name | default_database | 数据库名称 |
collection-name | default_collection | 集合名称 |
9. 其他厂商速查
以下厂商同样遵循 spring.ai.vectorstore.{type} 的配置前缀模式,仅列出关键属性。
Elasticsearch
spring.ai.vectorstore.type: elasticsearch
spring.ai.vectorstore.elasticsearch:
uris: http://localhost:9200
index-name: spring-ai-index
dimensions: 1536
similarity: cosine
OpenSearch
spring.ai.vectorstore.type: opensearch
spring.ai.vectorstore.opensearch:
uris: http://localhost:9200
index-name: spring-ai-index
Azure AI Search
spring.ai.vectorstore.type: azure
spring.ai.vectorstore.azure:
endpoint: ${AZURE_AI_SEARCH_ENDPOINT}
api-key: ${AZURE_AI_SEARCH_API_KEY}
index-name: spring-ai-index
Azure Cosmos DB
spring.ai.vectorstore.type: azure-cosmos-db
spring.ai.vectorstore.azure-cosmos-db:
endpoint: ${AZURE_COSMOS_ENDPOINT}
key: ${AZURE_COSMOS_KEY}
database-name: spring-ai-db
container-name: spring-ai-container
Cassandra
spring.ai.vectorstore.type: cassandra
spring.ai.vectorstore.cassandra:
keyspace: spring_ai
table: vector_store
dimensions: 1536
MongoDB Atlas
spring.ai.vectorstore.type: mongodb-atlas
spring.ai.vectorstore.mongodb-atlas:
connection-string: ${MONGODB_ATLAS_URI}
database-name: spring_ai
collection-name: vector_store
index-name: default
Neo4j
spring.ai.vectorstore.type: neo4j
spring.ai.vectorstore.neo4j:
database-name: neo4j
embedding-dimension: 1536
index-name: spring-ai-index
label: Document
embedding-property: embedding
Oracle
spring.ai.vectorstore.type: oracle
spring.ai.vectorstore.oracle:
table-name: vector_store
index-type: IVF
distance-type: COSINE
Typesense
spring.ai.vectorstore.type: typesense
spring.ai.vectorstore.typesense:
api-key: ${TYPESENSE_API_KEY}
protocol: http
host: localhost
port: 8108
collection-name: spring_ai_vector_store
embedding-dimension: 1536
Weaviate
spring.ai.vectorstore.type: weaviate
spring.ai.vectorstore.weaviate:
scheme: http
host: localhost:8080
api-key: ${WEAVIATE_API_KEY}
index-name: SpringAiVectorStore
MariaDB
spring.ai.vectorstore.type: mariadb
spring.ai.vectorstore.mariadb:
schema-name: ai
table-name: vector_store
dimensions: 1536
GemFire
spring.ai.vectorstore.type: gemfire
spring.ai.vectorstore.gemfire:
index-name: vector-index
dimensions: 1536
Couchbase
spring.ai.vectorstore.type: couchbase
spring.ai.vectorstore.couchbase:
connection-string: ${COUCHBASE_CONNECTION_STRING}
username: ${COUCHBASE_USERNAME}
password: ${COUCHBASE_PASSWORD}
bucket-name: spring-ai
scope-name: _default
collection-name: vector_store
Coherence
spring.ai.vectorstore.type: coherence
spring.ai.vectorstore.coherence:
session-name: default
cache-name: vector-store
SAP HANA
spring.ai.vectorstore.type: hanadb
spring.ai.vectorstore.hanadb:
table-name: VECTOR_STORE
top-k: 4
10. 功能对比
| 厂商 | 索引类型 | 距离算法 | Filter 删除 | 原生客户端 | Schema 管理 | 部署复杂度 |
|---|---|---|---|---|---|---|
| PGVector | NONE, IVFFLAT, HNSW | Cosine, Euclidean, InnerProduct | 支持 | JdbcTemplate | 自动建表 | 中(需安装扩展) |
| Pinecone | 自动(云托管) | Cosine(内置) | 模拟(搜索后删除) | Pinecone gRPC | 无需管理 | 低(全托管) |
| Qdrant | 自动 | Cosine(内置) | 支持 | QdrantClient | 自动建集合 | 低 |
| Milvus | IVF_FLAT 等 | Cosine, L2, IP | 支持 | MilvusServiceClient | 自动建集合 | 高(分布式) |
| Redis | FLAT, HSNW | Cosine(内置) | 支持 | JedisPooled | FT.CREATE | 中(需 Redis Stack) |
| Chroma | 自动 | Cosine(内置) | 支持 | ChromaApi(REST) | 自动建集合 | 低 |
| Elasticsearch | 自动 | Cosine, L2, DotProduct | 支持 | ElasticsearchClient | 自动建索引 | 中 |
| OpenSearch | 自动 | Cosine, L2 | 支持 | OpenSearchClient | 自动建索引 | 中 |
| Azure | 自动(云托管) | Cosine(内置) | 支持 | SearchIndexClient | 自动建索引 | 低(全托管) |
| Neo4j | 自动 | Cosine(内置) | 支持 | Neo4jClient | 自动建索引 | 中 |
11. 选型建议
| 场景 | 推荐 |
|---|---|
| 已有 PostgreSQL,不想引入新服务 | PGVector |
| 零运维、快速启动 | Pinecone 或 Qdrant Cloud |
| 高性能 + 自托管 | Qdrant |
| 大规模分布式 | Milvus |
| 已有 Redis Stack | Redis |
| 本地原型开发 | Chroma |
| 全文搜索 + 向量搜索一体 | Elasticsearch |
| Azure 生态 | Azure AI Search |
| MongoDB 生态 | MongoDB Atlas |
| 图数据 + 向量 | Neo4j |
| 企业级 Oracle 环境 | Oracle 23ai |