FAISS Explained: Building Fast Vector Search Indexes for Embeddings at Scale

FAISS is Meta’s open-source library for fast vector search over embeddings. Flat, IVF, HNSW and product quantisation indexes explained with code.

By Galactic News Editorial · Published 2026-11-26T13:48:31+00:00 · Updated 2026-11-26T13:48:31+00:00

FAISS vector search tutorial showing embedding indexes at scale

FAISS Explained: Building Fast Vector Search Indexes for Embeddings at Scale

Every retrieval system faces the same problem. You have millions of embeddings, and you need the nearest ones to a query in milliseconds. A brute-force scan works at small scale and dies at large scale. FAISS, Facebook AI Similarity Search, is the open-source library that solves this. This tutorial explains what FAISS is, how its index types work, and how to pick the right one for your embeddings.

What Is FAISS?

FAISS is an open-source C++ library, with complete Python wrappers, for exact and approximate nearest-neighbour search over dense vectors. Meta open-sourced it under the MIT licence. It is a library, not a database. It exposes no network protocol, no query language, no authentication and no metadata planner. You import it into your process, build an index in memory, and search it.

That simplicity is the point. A vector database adds persistence, filtering and scaling on top of search. FAISS gives you the search itself, with maximum speed and control. It is the standard choice for retrieval-augmented generation pipelines, semantic search, image deduplication and recommender systems where the team wants to own the retrieval layer.

FAISS works with indexes. An index is an object you populate with vectors and then query. Different index types make different trade-offs between speed, accuracy and memory. The rest of this tutorial is about choosing among them.

Flat Indexes: Exact Search and the Baseline

IndexFlatL2 and IndexFlatIP are the simplest indexes. They compare the query against every vector in the collection. The results are exact. There is no training step. The cost grows linearly with collection size.

Use a flat index for collections under roughly ten thousand vectors, or whenever exact results matter more than speed. It is also the correctness baseline: build a flat index first, measure its recall, then compare every approximate index against it. FAISS expects vectors as NumPy arrays of type float32. IndexFlatIP uses inner product, which equals cosine similarity when vectors are L2-normalised.

A quick example:

import faiss
import numpy as np

dimension = 768
index = faiss.IndexFlatL2(dimension)
vectors = np.random.rand(10000, dimension).astype('float32')
index.add(vectors)
distances, indices = index.search(query, 10)

IVF Indexes: Clustering for Speed

IndexIVFFlat partitions the vector space into clusters with a k-means-trained coarse quantiser. A query is then compared only with the vectors in the nearest clusters, instead of the whole collection. The number of clusters is nlist. The number of clusters searched per query is nprobe.

IVF indexes need a training pass before vectors can be added. Training determines the cluster centres, so train on a representative sample of your data. The FAISS guidelines suggest between 4√N and 16√N clusters for N vectors. Raising nprobe improves recall and costs speed. That single knob is the main tuning lever for IVF.

quantizer = faiss.IndexFlatL2(dimension)
nlist = 100
index = faiss.IndexIVFFlat(quantizer, dimension, nlist)
index.train(training_vectors)
index.add(vectors)
index.nprobe = 10
distances, indices = index.search(query, 10)

IVF suits collections from tens of thousands to around a million vectors. It is the balanced default when flat search gets too slow.

HNSW Indexes: Graph Search for Lowest Latency

IndexHNSWFlat builds a multi-layer proximity graph over the vectors. A query walks the graph from the top layer down, homing in on the nearest neighbours. It is the fastest of FAISS's CPU index types at a given recall level. On the SIFT1M benchmark, FAISS's own guide reports roughly 0.02 milliseconds per query for HNSW against roughly 0.14 milliseconds for IVFFlat at comparable recall.

HNSW needs no training. Three parameters control it. M sets the number of graph connections per node, with 32 as a common default. efConstruction controls build-time search width and therefore graph quality. efSearch controls query-time search width and therefore recall. Raise efSearch for better recall at higher latency.

index = faiss.IndexHNSWFlat(dimension, 32)
index.hnsw.efConstruction = 40
index.hnsw.efSearch = 16
index.add(vectors)

HNSW costs more memory per vector than IVF, and vectors cannot be removed once added. It fits read-heavy workloads where latency matters most and the corpus changes slowly.

Product Quantisation: Compressing Vectors for Scale

Product quantisation, or PQ, compresses each vector into a short code. It splits a vector into subvectors and quantises each against a learned codebook. IndexPQ applies compression alone. IndexIVFPQ combines IVF clustering with PQ compression of the residuals, giving both speed and memory reduction. OPQ applies a learned rotation before PQ to reduce quantisation error at the same code length.

The memory savings are large. On SIFT1M, an OPQ32_128, IVF4096, PQ32 index takes about 40 MB, against roughly 520 MB for uncompressed IVFFlat. The cost is recall: compression loses information, and the loss narrows as code length grows. Use PQ when the corpus is too large for RAM, typically from one million vectors upward.

Choosing the Right FAISS Index

Index typeTrainingAccuracySpeedMemoryBest for
IndexFlatL2 / IndexFlatIPNoneExact, 100%Slow at scaleHighUnder 10K vectors, exact results
IndexIVFFlatYes95–99%FastMedium10K to 1M vectors, balanced
IndexHNSWFlatNoneAbout 99%FastestHighLow latency, read-heavy
IndexIVFPQ / OPQ + IVFPQYes90–95%FastLow1M+ vectors, limited RAM

The FAISS index guidelines give size-based defaults. Start with IndexFlatIP on normalised embeddings. Move to IndexIVFFlat below about one million vectors when flat is too slow. Use IVF with HNSW and PQ between one and ten million vectors. Use IndexHNSWFlat when you have memory to spare and want the fastest queries.

Installing FAISS and Going to Production

The supported install is conda: conda install -c pytorch -c conda-forge faiss-cpu. A GPU build, faiss-gpu, exists for CUDA machines. There is also a pip package, faiss-cpu, for simpler setups.

For production, remember three things. First, persist the index with faiss.write_index and load it with faiss.read_index, so restarts do not rebuild it. Second, measure recall against a flat index on your own data, not on a benchmark. Third, know when to stop: if you need filtering, multi-tenancy, replication or a network API, that is the point to move from FAISS the library to a vector database built on top of it.

Bottom Line

FAISS is the fastest way to search embeddings at scale, and the choice of index is the whole game. Flat is exact and simple. IVF is the balanced default. HNSW is the fastest at a given recall. PQ is the answer when RAM runs out. Start flat, measure, and move up only when the numbers say you must.