首页/AI/RAG 与向量库速查/HNSW 图索引与参数
本包目录(229 条)
架构与流程30 条解析与切分33 条
Embedding 与检索50 条
向量数据库49 条重排与生成32 条评估与优化35 条

HNSW 图索引与参数

HNSW(Hierarchical Navigable Small World)把向量连成多层邻近图:上层稀疏用于快速跳转,底层稠密用于精搜,是当前向量检索常用的图索引之一。

RAG 与向量库速查AI更新于 2026-10-12
M每层最大连接数,常用 16~64
efConstruction建索引时的候选队列,常用 100~500
efSearch查询时的候选队列,越大越准越慢

FAISS 构建与查询

FAISS 构建与查询
import faiss, numpy as np

d = 768
index = faiss.IndexHNSWFlat(d, 32)          # M=32
index.hnsw.efConstruction = 200
index.hnsw.efSearch = 64
index.add(np.random.rand(10000, d).astype("float32"))
D, I = index.search(np.random.rand(1, d).astype("float32"), 10)

Milvus 建索引

Milvus 建索引
# pymilvus 2.x
index_params = client.prepare_index_params()
index_params.add_index(field_name="vector", index_type="HNSW",
                       metric_type="COSINE", params={"M": 32, "efConstruction": 200})
efSearch 可在查询时调;M 与 efConstruction 影响内存与建索引时长,调整需重建索引。

易错点

  • 把 efSearch 调到超过底层节点总数,收益有限还增加延迟
  • 删除向量后图结构不会自动修复,长期高频删除的场景要评估重建索引策略

数据来源与参考

  • Malkov & Yashunin, Efficient and robust approximate nearest neighbor search using Hierarchical Navigable Small World graphs (arXiv:1603.09320)
  • FAISS wiki(HNSW)
  • Milvus 官方文档(HNSW)
​‌‌​​‌​​​‌‌​‌‌‌‌​‌‌​​​‌‌​‌‌‌​​‌‌​​‌​‌‌‌​​‌‌‌​​​‌​‌‌​‌​​‌​‌‌‌​​​‌​‌‌​‌​​‌​‌‌​​​‌‌​‌‌‌​‌​​​‌‌​‌‌‌‌​​‌​‌‌‌​​‌‌​​​‌‌​‌‌​‌‌‌‌​‌‌​‌‌​‌