Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 3.2 — Why Search Becomes Approximate
RAG and Vector Search · Example 1 of 2

Distances bunch together as dimensions grow

Uniform random points have no structure, so this exaggerates the effect for real embeddings. It shows the direction of the problem, not the exact numbers you will see on real data.

Sorting and tree structures that make ordinary database lookups fast stop helping when vectors have hundreds of numbers. Vector indexes therefore trade a little accuracy for a lot of speed, and the accuracy is measured as recall against the exact answer.

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 recall_at_k(found, truth) that returns the fraction of truth present in found. Print it with 3 decimals for found [3, 1, 7, 9] against truth [1, 2, 3, 4], for an empty found list, and for the same four ids in reverse order.

02

Three queries have (found, truth) pairs ([1, 2, 3], [1, 2, 3]), ([4, 5, 6], [4, 9, 6]) and ([7, 8, 9], [1, 2, 3]). Print each query's recall and then the average and the worst, to 3 decimals. What does the average hide?

Explain it without notes

01

Why can a database find one number among millions with about twenty steps, but not the nearest vector among millions?

02

What does the distance table in the first example show, and why does it hurt indexing?

03

Define recall@k and compute it for found = [3, 1, 7, 9] against truth = [1, 2, 3, 4].

04

Why must the ground truth for recall come from exact search over the whole collection?

05

A teammate says the index has recall 0.97, so it is fine. What would you check before agreeing?

Distances bunch together as dimensions grow
Sign in to run this example in your browser.

Expected output

300 random points; distance from one query to its nearest and its farthest point
    2 dims: nearest 0.024, farthest 1.093, nearest/farthest 0.022
   16 dims: nearest 0.932, farthest 2.248, nearest/farthest 0.415
  128 dims: nearest 4.011, farthest 5.274, nearest/farthest 0.760