Lesson 9.3core3 h 20 min

Vector search and indexes

Finding nearest neighbours among millions of vectors

Once documents are vectors, retrieval is nearest-neighbour search (3.4 k-Nearest Neighbors) — exact and trivial at ten thousand vectors, a real systems problem at a hundred million. Knowing how IVF, product quantization and HNSW trade recall for speed and memory, and when a plain Postgres table beats a dedicated vector database, saves you from both over-engineering and silent recall loss. Metadata filtering here is also where access control is enforced (9.9 Production: latency, cost, caching, guardrails, security and privacy).

You'll be able to

  • Estimate the cost of exact search and decide when approximate search is necessary
  • Explain IVF, product quantization and HNSW, including their key parameters and the recall/latency/memory trade-offs
  • Compute index memory for a corpus with and without product quantization
  • Choose between a library, a database extension and a dedicated vector database for a given scale and feature set
  • Describe how metadata filtering, hybrid search and updates interact with ANN indexes

Key ideas

  1. Exact (flat) search costs N·d per query and is 100% accurate; it is the right default up to a few million vectors.
  2. IVF partitions vectors into nlist k-means cells and scans only nprobe of them: cost ≈ nlist·d + nprobe·(N/nlist)·d.
  3. Product quantization stores m one-byte codes instead of 4d bytes: d = 1024, m = 64 is a 64× memory reduction, with distances computed by m table lookups.
Show 4 more key ideas
  1. HNSW is a layered proximity graph searched greedily; M sets links per node (memory), efConstruction build quality, efSearch the recall/latency dial at query time.
  2. Below a few million vectors a Postgres table with pgvector is usually enough; dedicated vector databases pay off at tens of millions of vectors, high QPS or rich filtered search.
  3. Metadata filters are hard for ANN (post-filtering starves, pre-filtering breaks graphs) — and they are your access-control boundary, so verify how your engine implements them.
  4. Changing the embedding model invalidates the whole index; store the model version with every vector and plan migrations.

Suggested path through this lesson