Vector DB & ANN · core

Top-k Vector Search

Returning the k nearest vectors to a query, exact or approximate.

vector-dbann

Mental model

Top-k is a bounded selection problem: keep a size-k heap whose root is the current worst survivor and evict that root when something better arrives. Ranking by similarity (higher is better) the root is the smallest, so it is a min-heap; ranking by distance (lower is better) the root is the largest, so it is a max-heap. Exact search scans everything; ANN scans a clever subset.

How to study Top-k Vector Search

Begin by restating the mental model in your own words, then connect it to a concrete system you have built or operated. Name the mechanism, the constraint it addresses, and the trade-off it introduces. Use Nearest neighbor search (Wikipedia) to check details, but close the source before writing your explanation. Retrieval is the learning step; rereading is only preparation.

Next, compare Top-k Vector Search with Brute-Force Vector DB, HNSW. Ask what changes in correctness, latency, resource use, operability, and failure recovery. Complete Top-k with a bounded heap and preserve the command, input, output, and one failed attempt as evidence. Finish by explaining the idea without jargon to someone who has not studied the track.

Proof of understanding

  • Explain the mechanism from first principles and identify the state it reads or changes.
  • Give one situation where the concept is the right choice and one where it is not.
  • Predict a realistic failure mode before running the drill, then compare the prediction with evidence.
  • Connect the result to a roadmap or build artifact instead of treating the concept as isolated trivia.

Common mistakes

  • Sorting all scores instead of using a k-sized heap
  • Confusing recall@k (quality) with k (result count)

Learn from primary sources

Practice and explain it back

Top-k with a bounded heap

Given a stream of (id, score) pairs, return the top-k by score using a size-k min-heap — without sorting everything.

Expected evidence: Correct top-k in O(n log k) time and O(k) space.

Open the interactive drill →

Review prompts

  • How do you return top-k without sorting all n scores?

Build evidence

Brute-force vector index

An exact nearest-neighbour index — the recall=1.0 baseline.

  • Insert and search vectors with a chosen metric
  • Return correct top-k via a k-sized heap
  • Benchmark query latency vs vector count

Prerequisites

Related concepts

Learning paths