Sub topic 2
Approximate Nearest Neighbor Search and Recall
Assume you know basic vectors, dot products, and database indexes. Exact search compares a query with every stored vector. Approximate nearest neighbor (ANN) search uses an index to inspect only promising candidates. It is faster, but it can miss some true neighbors.
Recall@k measures the fraction of the exact top-k results that ANN returns:
recall@k = returned true neighbors / k
You usually measure ANN recall against a brute-force baseline, then tune the index until its speed and accuracy meet your needs.
HNSW: Navigating a Layered Graph
HNSW, or Hierarchical Navigable Small World, stores vectors as nodes in a graph. Each node links to nearby nodes. Upper layers contain fewer long-range links. Lower layers contain more local links.
A search starts at an upper-layer entry point and moves to a closer node until no improvement is found. It then descends through the layers. The efSearch setting controls how many candidates the search explores. A larger value usually improves recall and increases latency.
IVF: Partitioning the Space into Clusters
An inverted file (IVF) index first groups vectors into clusters. It stores each vector in the list belonging to its nearest cluster center. During a query, the index searches only the closest lists.
nlist controls the number of clusters. nprobe controls how many clusters are searched. Higher nprobe improves recall but examines more vectors. IVF works well when the data is large and reasonably distributed.
Compressing Vectors with Product Quantization
Product quantization (PQ) reduces memory use. It splits each vector into smaller subvectors and replaces each subvector with the ID of its nearest codebook entry. Searches use these compact codes instead of full-precision vectors.
PQ can greatly reduce memory and improve cache performance, but quantization error can lower recall. Many systems use IVF and PQ together.
| Index choice | Main trade-off |
|---|---|
| HNSW | High recall and fast queries, but more memory |
| IVF | Tunable search cost, but depends on cluster quality |
| PQ | Low memory use, but adds approximation error |
What a Vector Database Adds Beyond an Index
A vector index is only one component. A vector database also provides storage, metadata, filtering, persistence, updates, replication, and query APIs. It can combine nearest-neighbor search with conditions such as tenant, language, or access level.
Exercise
What happens when you increase HNSW efSearch or IVF nprobe?
Show answer
The search usually examines more candidates, which improves recall but increases query latency and resource use.