跳到主要内容
版本:1.0.4

Providers

Spring AI 支持 20 种向量数据库实现,通过 spring.ai.vectorstore.type 配置属性切换,所有实现遵循统一的 VectorStore 接口。

1. 厂商总览

类型标识厂商部署模式特点
pgvectorPGVector自托管PostgreSQL 扩展,最流行的开源方案
pineconePinecone云服务全托管向量数据库,gRPC API
qdrantQdrant自托管/云高性能 Rust 实现,支持量化索引
milvusMilvus自托管/云分布式向量数据库,支持多种索引
redisRedis自托管/云RediSearch + RedisJSON 模块
chromaChroma自托管轻量级,适合原型开发
elasticsearchElasticsearch自托管/云全文搜索 + 向量搜索一体
opensearchOpenSearch自托管/云AWS 托管的 Elasticsearch 分支
azureAzure AI Search云服务Azure 原生向量搜索
azure-cosmos-dbAzure Cosmos DB云服务Azure 多模数据库
cassandraCassandra自托管分布式 NoSQL,适合大规模数据
coherenceCoherence自托管Oracle Coherence 内存网格
couchbaseCouchbase自托管/云分布式 NoSQL 数据库
gemfireGemFire自托管VMware 内存数据网格
hanadbSAP HANA自托管企业级内存数据库
mariadbMariaDB自托管MySQL 分支,向量扩展
mongodb-atlasMongoDB Atlas云服务MongoDB 托管向量搜索
neo4jNeo4j自托管/云图数据库 + 向量索引
oracleOracle自托管/云Oracle 23ai AI Vector Search
typesenseTypesense自托管/云轻量级搜索引擎
weaviateWeaviate自托管/云原生向量数据库,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-nameStringpublicPostgreSQL schema 名称
table-nameStringvector_store向量存储表名
dimensionsint-1(自动检测)向量维度,-1 表示从 EmbeddingModel 自动获取
index-typePgIndexTypeHNSW索引类型
distance-typePgDistanceTypeCOSINE_DISTANCE距离算法
id-typePgIdTypeUUID主键 ID 类型
remove-existing-vector-store-tablebooleanfalse启动时是否删除已有表
schema-validationbooleanfalse是否校验表结构一致性
max-document-batch-sizeint10000单次写入最大文档数

索引类型(PgIndexType)

说明
NONE不建立索引,执行精确搜索(适合小数据集)
IVFFLAT倒排文件索引,构建快,适合百万级数据
HNSW分层可导航小世界图,查询性能最优,构建较慢

距离算法(PgDistanceType)

PostgreSQL 运算符适用场景
COSINE_DISTANCE<=>语义相似度(推荐)
EUCLIDEAN_DISTANCE<->向量空间距离
NEGATIVE_INNER_PRODUCT<#>点积相似度

ID 类型(PgIdType)

UUID(默认)、TEXTINTEGERSERIALBIGSERIAL。使用 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-keyPinecone API 密钥(必需)
index-namePinecone 索引名称(必需)
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

专属配置

属性默认值说明
hostlocalhostQdrant 服务地址
port6334gRPC 端口
api-key-API 密钥(Qdrant Cloud 必需)
use-tlsfalse是否启用 TLS
collection-namevector_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

专属配置

属性默认值说明
hostlocalhostMilvus 服务地址
port19530Milvus 端口
database-namedefault数据库名称
collection-namevector_store集合名称
embedding-dimension-1向量维度,-1 自动检测
index-typeIVF_FLAT索引类型
metric-typeCOSINE度量类型:COSINEL2IP
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

专属配置

属性默认值说明
hostlocalhostRedis 服务地址
port6379Redis 端口
index-namespring-ai-indexRediSearch 索引名称
prefixembedding:文档 Key 前缀
content-field-namecontent文档内容字段名
embedding-field-nameembedding向量字段名
vector-algorithmHSNW向量索引算法:HSNWFLAT

元数据字段配置

Redis 支持额外的元数据索引字段配置,为元数据字段建立二级索引以加速过滤查询:

var store = RedisVectorStore.builder(jedis, embeddingModel)
.metadataFields(
MetadataField.tag("category"),
MetadataField.numeric("year"),
MetadataField.text("title")
)
.build();

MetadataField 类型:

类型方法说明
tagMetadataField.tag(name)标签字段,支持精确匹配
numericMetadataField.numeric(name)数值字段,支持范围查询
textMetadataField.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.hosthttp://localhostChroma 服务地址
client.port8000Chroma 端口
client.key-token-API 密钥
tenant-namedefault_tenant租户名称
database-namedefault_database数据库名称
collection-namedefault_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
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 管理部署复杂度
PGVectorNONE, IVFFLAT, HNSWCosine, Euclidean, InnerProduct支持JdbcTemplate自动建表中(需安装扩展)
Pinecone自动(云托管)Cosine(内置)模拟(搜索后删除)Pinecone gRPC无需管理低(全托管)
Qdrant自动Cosine(内置)支持QdrantClient自动建集合
MilvusIVF_FLAT 等Cosine, L2, IP支持MilvusServiceClient自动建集合高(分布式)
RedisFLAT, HSNWCosine(内置)支持JedisPooledFT.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
零运维、快速启动PineconeQdrant Cloud
高性能 + 自托管Qdrant
大规模分布式Milvus
已有 Redis StackRedis
本地原型开发Chroma
全文搜索 + 向量搜索一体Elasticsearch
Azure 生态Azure AI Search
MongoDB 生态MongoDB Atlas
图数据 + 向量Neo4j
企业级 Oracle 环境Oracle 23ai