Prompts Forms Resume Reports ★ Saved ★ Saved★ + Submit a tool
Techniques

Faiss Vector Index

About 3019 characters Updated Nov 30, 2026 21 views AI wiki

Faiss (Facebook AI Similarity Search) is a high-performance vector similarity search library open-sourced by Meta AI, written in C++ with official Python bindings for CPU and GPU, built to "find nearest neighbors fast among hundreds of millions of high-dimensional vectors." In RAG and semantic search it is typically the kernel inside a vector database, the component that makes semantic retrieval production-ready.

Core Principles

Faiss's core is replacing brute force with approximate nearest neighbor (ANN) search. Brute force computes the query distance to every vector, growing linearly, and 100 million 768-dimensional vectors cannot answer in real time. Faiss cuts cost two ways: quantization, using product quantization (PQ) to segment vectors, cluster them and replace floats with short codes for tens of times compression; and space partitioning, using an inverted file (IVF) to cluster cells and scan only the nearest nprobe cells.

The other main route is graph indexing, HNSW (Hierarchical Navigable Small World): vectors form a multi-layer neighbor graph, descending greedily from sparse top layers to the dense bottom, with near-logarithmic query cost. HNSW has good recall and speed at the price of high memory, slow build and difficult deletion.

Faiss splits an index into Index, Metric (inner product or L2) and parameters (nlist, nprobe, m, efSearch), combined through a factory string: IVF4096,PQ64 means 4096 cells with 64-segment PQ, and HNSW32 means 32 edges per layer.

Key Points

First, match index type to data scale. Under 100,000 vectors, Flat is exact with zero recall loss; at the million scale use IVF, or IVF+PQ for compression; above ten million with ample memory and high recall needs, use HNSW or IVF+HNSW. Second, PQ needs enough training samples: each sub-quantizer should see several thousand vectors, or accuracy collapses.

Third, nprobe and efSearch are the recall knobs: larger nprobe scans more cells with linearly rising latency, larger efSearch refines graph search; sweep parameters on real queries and plot recall against latency. Fourth, normalization sets the metric: after L2 normalization inner product equals cosine, so use METRIC_INNER_PRODUCT, or results will diverge. Fifth, Faiss is not a database and cannot filter by business fields; to search within a category, build small per-category indexes or filter manually with IDSelector (limited efficiency). It has no persistence transactions, metadata filtering or sharding, all built on top.

Common Misconceptions

The most common is treating Faiss as a vector database for production; it is a low-level library, so filtering, permissions, CRUD and persistence are yours to build. The second is trusting ANN everywhere: in finance and legal work with zero-miss requirements, recall loss can be fatal, so ANN coarse recall needs reranking fallback. The third is ignoring index-model version binding; switching the embedding model without rebuilding leaves dimensions or semantics inconsistent, and when dimensions happen to match no error is raised, only "everything retrieved is wrong." The fourth is using average recall as the sole metric, ignoring long-tail queries that decide real user experience.

Application Scenarios

Semantic recall for RAG, image search, similar product recommendation, face retrieval and dedup, audio fingerprinting, similar code retrieval, cross-lingual clustering, massive news dedup, offline batch comparison, vector recall in recommenders, image copyright tracing, and multimodal image-text cross-checking.