LLM Learning Hub

workspace/llm-course/home

Nearest Neighbor Search

Nearest-neighbour search finds the k vectors in a store closest to a query vector under a distance metric; exact search compares against all vectors, costing O(n*d) per query.

What is Nearest Neighbor Search?

Nearest-neighbour search finds the k vectors in a store closest to a query vector under a distance metric; exact search compares against all vectors, costing O(n*d) per query.

Nearest-neighbour search finds the k vectors in a store closest to a query vector under a distance metric; exact search compares against all vectors, costing O(n*d) per query.

Where is it used?

Exact search via FAISS `IndexFlatL2` is used for small corpora and ground-truth evaluation; production systems with millions of vectors switch to approximate indexes for latency.

How to build it

Create `faiss.IndexFlatL2(d)`, add 10k random vectors, call `.search(query, k=5)`, and time it; then compare against HNSW to see the latency improvement at a small recall cost.