Skip to content

参考

基于 Pinecone / Milvus / pgvector / Qdrant / Weaviate / Chroma 官方文档 2026 编写。完整 API 见各库文档。

各库核心特性对比

语言部署默认索引混合检索元数据过滤
Pinecone闭源托管 SaaS / serverless内部托管原生强(namespaces + filter)
MilvusGo自托管 + 托管(Zilliz)FLAT/IVF/HNSW/DISKANNdense+sparsepartition + expr
WeaviateGo自托管 + 云HNSW原生 hybrid (RRF)强(where filter)
QdrantRust自托管 + 云HNSWdense+sparse (RRF)强(payload filter)
ChromaPython嵌入式 / 服务器HNSW全文+向量(v0.5+)where
pgvectorC (PG ext)复用 PostgresHNSW / IVFFlat需手动配 tsvectorSQL WHERE

距离度量与对应操作符

度量公式PineconeMilvusQdrantpgvector 操作符
余弦 cosine1 - cos(θ)cosineCOSINECosine<=>vector_cosine_ops
L2 欧氏sqrt(Σ(Δ)²)euclideanL2Euclid<->vector_l2_ops
内积A·BdotproductIPDot<#>vector_ip_ops,负内积)

Milvus 索引矩阵

索引类型内存召回构建速度最佳场景
FLAT精确最高100%< 10 万,要求精确
IVF_FLAT近似大规模,速度召回均衡
IVF_SQ8近似(标量量化)内存吃紧
IVF_PQ近似(乘积量化)最低较低极致省内存
HNSW近似(图)很高低延迟高召回
DISKANN近似(磁盘图)低(RAM)海量(超 RAM)

HNSW 参数

参数名默认说明
MilvusM16邻居数
MilvusefConstruction200建图候选
Milvusef(查询)64查询候选
pgvectorm16邻居数
pgvectoref_construction64建图候选
pgvectorhnsw.ef_search(GUC)40查询候选
Qdrantm16邻居数
Qdrantef_construct100建图候选
Qdrantef(查询 search_params128查询候选

IVF / IVFFlat 参数

参数经验值
Milvus nlist聚类桶数sqrt(N)-4*sqrt(N)
Milvus nprobe(查询)探测桶数nlist 的 1%-5%
pgvector lists聚类桶数N/1000,至少 10
pgvector ivfflat.probes(GUC)探测桶数1(默认,常需调高)

Pinecone

创建 serverless 索引

python
from pinecone import Pinecone, ServerlessSpec

pc = Pinecone(api_key=os.getenv("PINECONE_API_KEY"))
pc.create_index(
    name="docs",
    dimension=1536,
    metric="cosine",          # cosine | dotproduct | euclidean
    spec=ServerlessSpec(cloud="aws", region="us-east-1"),
)

查询

python
index = pc.Index("docs")
res = index.query(
    vector=[0.1, ...],
    top_k=10,
    include_metadata=True,
    filter={"category": {"$eq": "faq"}, "year": {"$gte": 2024}},
    namespace="tenant_a",   # 多租户隔离
)

特性

  • serverless:按存储 + 查询付费,自动扩缩
  • pod(s1/p1/p2):2025-08 停售新户,老户仍可用,p99 ~30ms
  • namespaces:逻辑隔离,适合多租户
  • hybrid search:dense + sparse 同查

Milvus

创建集合

python
from pymilvus import MilvusClient

client = MilvusClient(uri="http://localhost:19530")  # 或 "milvus.db" (Lite)
client.create_collection(
    collection_name="docs",
    dimension=1536,
    metric_type="COSINE",
    index_type="HNSW",
    index_params={"M": 16, "efConstruction": 200},
)

部署形态

形态说明
Milvus Lite嵌入 Python 进程,本地开发
Milvus Standalone单机 Docker
Milvus DistributedK8s 集群
Zilliz Cloud托管版

pgvector

安装

sql
CREATE EXTENSION IF NOT EXISTS vector;

索引语法

sql
-- HNSW(推荐)
CREATE INDEX ON docs USING hnsw (embedding vector_cosine_ops)
  WITH (m = 16, ef_construction = 200);

-- IVFFlat(构建快,省内存)
CREATE INDEX ON docs USING ivfflat (embedding vector_cosine_ops)
  WITH (lists = 100);

-- 在线重建(不停服)
CREATE INDEX CONCURRENTLY ON docs USING hnsw (embedding vector_cosine_ops);

查询操作符

操作符含义配对 opclass
<->L2 欧氏距离vector_l2_ops
<#>负内积vector_ip_ops
<=>余弦距离vector_cosine_ops
sql
-- 余弦最近邻
SELECT id, embedding <=> '[0.1,...]'::vector AS dist
FROM docs ORDER BY embedding <=> '[0.1,...]'::vector LIMIT 10;

-- 验证走索引
EXPLAIN SELECT id FROM docs ORDER BY embedding <=> '[0.1,...]'::vector LIMIT 10;

关键 GUC

GUC默认说明
hnsw.ef_search40HNSW 查询召回
ivfflat.probes1IVFFlat 探测桶数
maintenance_work_mem64MB建索引内存,建议提到 1GB+

Weaviate

混合检索

python
result = collection.query.hybrid(
    query="如何重置密码",
    alpha=0.5,            # 0=纯BM25,1=纯dense
    limit=10,
    filters=Filter.by_property("category").equal("faq"),
)

索引

  • 默认 HNSW(dense)
  • 倒排索引(BM25 / sparse)
  • hybrid = BM25 + dense + RRF 融合

Qdrant

创建集合

python
from qdrant_client import QdrantClient
from qdrant_client.models import Distance, VectorParams

client = QdrantClient(":memory:")
client.create_collection("docs",
    vectors_config=VectorParams(size=1536, distance=Distance.COSINE))

Payload 过滤

python
client.search("docs", query_vector=[...], limit=5,
    query_filter={
        "must": [
            {"key": "category", "match": {"value": "faq"}},
            {"key": "year", "range": {"gte": 2024}},
        ]
    })

混合检索

python
client.query_points("docs",
    prefetch=[
        models.PrefetchQuery(query=[...], using="dense", limit=20),
        models.PrefetchQuery(query=sparse_vec, using="sparse", limit=20),
    ],
    query=models.FusionQuery(fusion=models.Fusion.RRF),
    limit=10,
)

Chroma

python
import chromadb

client = chromadb.PersistentClient(path="./chroma_db")
collection = client.get_or_create_collection("docs")

# 不传 embeddings 自动用默认模型
collection.add(documents=["..."], ids=["1"], metadatas=[{"src": "a"}])

# 查询
res = collection.query(query_texts=["相关问题"], n_results=5,
    where={"src": "a"})

选型决策矩阵

维度PineconeMilvusWeaviateQdrantChromapgvector
托管serverlessZillizCloudCloud有(各云)
自建✓(嵌入式)
规模上限极大
索引丰富度内部托管最全HNSW+倒排HNSWHNSWHNSW/IVFFlat
混合检索原生dense+sparse原生 hybriddense+sparse全文+向量手动配
多租户namespacespartitioncollectioncollectionschema
复用现有 DBPostgres
上手难度极低低(懂 PG)

资源链接