Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 3.1 — Exact Nearest-Neighbour Search
RAG and Vector Search · Example 2 of 2

Keeping only the best 3, and the cost at real sizes

The 2,000 vectors are random numbers from the course's seeded generator. The cost table assumes one billion multiply-adds per second per core: a round number to reason with, not a benchmark.

The simplest correct vector search compares the question with every stored vector and keeps the best k. It costs vectors times dimensions multiplications per question, which is fine for a surprisingly large collection and is the ground truth every faster method is judged against.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 6 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Write top_k(query, vectors, k) that returns the ids (positions) of the k vectors with the highest dot product, ties broken by lower id. Print the top 2 for the query [1, 1] and the top 3 for [1, 0] over the vectors [1, 0], [0, 1], [0.6, 0.8], [0.8, 0.6].

02

A collection has 50,000 vectors of 768 numbers. Print the number of multiply-adds one exact query needs and the memory the float32 vectors take in megabytes (1 MB = 1,000,000 bytes).

Explain it without notes

01

What does exact nearest-neighbour search do, and what does one query cost in terms of n (vectors) and d (dimensions)?

02

Why keep only a short list of the best k instead of sorting every score? Roughly how often does that list change?

03

A team has 80,000 chunks of 768 numbers and about 3 questions per second. Is exact search enough? Show your reasoning.

04

Why does exact search matter even if you will use an approximate index in production?

Keeping only the best 3, and the cost at real sizes
Sign in to run this example in your browser.

Expected output

scanned 2000 vectors, the top-3 list changed 20 times
  id 651  squared distance 0.1382
  id 1134  squared distance 0.1503
  id 1162  squared distance 0.1515
cost of one exact query (1 billion multiply-adds per second is an assumed round number)
  10000 vectors x 384: 3 million multiply-adds, about 3 ms, 15 MB of vectors
  1000000 vectors x 384: 384 million multiply-adds, about 384 ms, 1536 MB of vectors
  100000000 vectors x 1024: 102400 million multiply-adds, about 102400 ms, 409600 MB of vectors