k-nearest-neighbor search, or k-NN, takes a query vector and returns the k stored vectors closest to it under a distance metric. It ranks by closeness, it does not look for an exact match.
Exact search compares the query against every stored vector. It always returns the true top k, but the work grows linearly with the collection size.
scores = corpus @ query # touches every vector, every query
top_k = np.argsort(-scores)[:k]
Approximate nearest neighbor (ANN) search skips most of the collection by using an index built ahead of time. It is much faster, but it can miss some true neighbors.
| Aspect | Exact | Approximate |
|---|---|---|
| Result | always the true top k | usually close to it |
| Query cost | scales with collection size | roughly sub-linear |
| Extra memory | none | index structure |
| Tuning | nothing to tune | speed versus recall |
Exact search is fine for small collections. Once you reach millions of vectors, ANN is the practical default.
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
Why there's no diagram: “”
The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓
The diagram below the answer is the concept . Jump to it ↓