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 1 of 2

Exact search over twelve vectors

The vectors are a toy: four directions chosen by hand so you can check the scores in your head. A real embedding model has hundreds of learned directions.

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?

Exact search over twelve vectors
Sign in to run this example in your browser.

Expected output

compared with 12 vectors of 4 numbers
0.985  D6  Late returns
0.957  D1  Refunds
0.895  D8  Monthly pass