paulserban.eu

Writing Edition

Paul Serban

Writing, snippets & book notes

Cheat sheet - Post

Choosing a Vector Search Algorithm for Production AI Systems

← Back to Beyond Brute Force: A Practical Guide to Vector Search Algorithms for AI Engineers

Vector search finds the closest embeddings to a query vector in a large collection, and the indexing strategy you pick trades off recall, latency, and memory in ways that shape your entire retrieval architecture.

Overview

Key Concepts

Exact k-NN (Brute-Force Search)

scores = corpus @ query  # cosine sim via dot product on normalized vectors
top_k_idx = np.argpartition(-scores, k)[:k]  # avoid full O(n log n) sort

Graph-Based ANN: HNSW

Partition-Based ANN: IVF, Product Quantization, IVF-PQ

Clustering + Exact Search

Hybrid Lexical + Vector Search

const contribution = 1 / (k + rank);
scores.set(id, (scores.get(id) ?? 0) + contribution);

Learned Indexes

Binary and Quantized Embeddings

Trade-offs / Caveats

Example in Practice

A tiered retrieval pipeline for a large-scale RAG backend: binary embeddings provide a cheap first-pass filter over the full corpus (Hamming distance, fits in RAM/cache) -> an HNSW or IVF-PQ index performs the primary ANN retrieval on the narrowed set -> results are fused with BM25 lexical hits via RRF to catch exact-match terms -> a cross-encoder reranks the final top-N shortlist before anything reaches the LLM's context window. Each stage's recall is instrumented independently, since an early cheap stage can silently discard relevant results before expensive downstream stages ever see them.

Related topics