首页/AI/RAG 与向量库速查/FAISS IndexFlat 精确检索

FAISS IndexFlat 精确检索

IndexFlatL2 / IndexFlatIP 是暴力精确检索索引:逐一比较全部向量,结果为准确保基线;数据量在十万级内、或用于验证近似索引的召回时常用。

RAG 与向量库速查AI更新于 2026-10-12

IndexFlatIP 找相似(内积)

IndexFlatIP 找相似(内积)
import faiss, numpy as np

xb = np.random.rand(100000, 384).astype("float32")
faiss.normalize_L2(xb)                      # 归一化后内积等价余弦
index = faiss.IndexFlatIP(384)
index.add(xb)
q = np.random.rand(1, 384).astype("float32")
faiss.normalize_L2(q)
D, I = index.search(q, 5)                   # D=相似度, I=向量序号
print(I[0], D[0])
Flat 索引无需训练、可随时添加;替换为 IVF/HNSW 前先以它为召回基线。

易错点

  • 用 L2 索引配归一化向量却不换 IP,度量语义与文档不一致
  • 百万级数据继续用 Flat,单次查询扫描全量导致延迟不可接受

数据来源与参考

  • FAISS wiki(IndexFlat / MetricType)
​‌‌​​‌​​​‌‌​‌‌‌‌​‌‌​​​‌‌​‌‌‌​​‌‌​​‌​‌‌‌​​‌‌‌​​​‌​‌‌​‌​​‌​‌‌‌​​​‌​‌‌​‌​​‌​‌‌​​​‌‌​‌‌‌​‌​​​‌‌​‌‌‌‌​​‌​‌‌‌​​‌‌​​​‌‌​‌‌​‌‌‌‌​‌‌​‌‌​‌