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

Vector Search ANN

About 2904 characters Updated Nov 29, 2026 24 views AI wiki

Approximate nearest neighbor (ANN) search in vector retrieval quickly finds the top k records most similar to a query among hundreds of thousands to billions of high-dimensional vectors. It is the engine behind RAG, recommendation recall and image search.

Core Principles

Exact search computes a distance per vector, O(N*d); at tens of millions of vectors and d of 768 or 1536, a CPU query takes seconds and is unusable. The curse of dimensionality worsens it: distances concentrate, the gap between nearest and farthest flattens, and tree indexes such as KD-Tree degenerate to full scans.

ANN gives up 100% recall, trading 95% to 99% recall for 10 to 1000 times speedup. There are three main index structures.

Quantization: PQ (product quantization) splits vectors into segments, clusters each and replaces floating points with short codes, compressing 32 to 64 times; IVF-PQ first partitions space into Voronoi cells by k-means, searches only the nearest nprobe cells and approximates distance within them with PQ codes. Faiss's IndexIVFPQ represents this route.

Graphs: HNSW (hierarchical navigable small world) builds a multi-layer neighbor graph, sparse on top for long jumps and dense at the bottom for fine search, reaching logarithmic complexity by greedy traversal; it offers the best overall recall and speed, with M and efSearch controlling connectivity and search width.

Hashing: LSH maps similar vectors to the same bucket with high probability by random projection, and variants are widely used in deduplication.

Key Points

Handle filtering: business queries carry metadata filters (time, category, permissions) that pure ANN cannot handle; pre-filter, post-filter or prune during graph search (such as Milvus's filter-first), or recall collapses at high filter rates.

Hybrid retrieval: vector search is strong on gist but weak on exact matches (model numbers, proper nouns), so production combines BM25 with dense vectors and merges via RRF or a learned reranker.

The embedding model sets the ceiling: the index only accelerates; recall quality mainly comes from the embedding model (BGE, E5, text-embedding-3) and its query-instruction consistency, so switching indexes gains far less than switching embeddings.

Main tools: Faiss (library, most flexible), HNSWlib, ScaNN, and Milvus, Qdrant, Weaviate and pgvector (database services), with managed versions from most clouds.

The evolution runs from quantization to graph indexes, and now to disk indexes (DiskANN) and GPU-native indexes at billion scale.

Common Misconceptions

Misconception one: watching latency and not recall, dropping efSearch until recall hits 70%; plot the recall-latency curve and pick a working point by business tolerance. Misconception two: using cosine without normalizing; mixing inner product and cosine yields inconsistent ranking, and inner-product indexes need L2 normalization first. Misconception three: choosing an index without delete and merge support under frequent updates, so tombstones pile up and performance drifts. Misconception four: expecting a stronger index to lift business metrics while ignoring that the embedding model and chunking are the main sources of recall quality.

Application Scenarios

RAG knowledge base retrieval, candidate recall, image and video search, semantic recall in AI search, dedup and neighbor clustering, log anomaly detection, and molecular similarity screening.