Is the nearest answer worth the wait?
Approximate nearest neighbour search finds vectors close to a question without comparing it with every vector, and may miss a few. Exact search compares it with all of them and never misses.
With a million vectors, exact search is too slow for a chat window. Every vector database makes this trade, and you choose where it sits.
By the end of this lesson
Note
I can measure the recall of an approximate search against an exact one.
Key words
| Word | What it means | Example |
|---|---|---|
| kNN | the k nearest vectors to a question | |
| Exact search | comparing the question with every vector | |
| ANN | approximate nearest neighbour search, which may miss some | |
| Recall@k | the share of the true top k a search found | 8 of 10 = 0.8 |
| Latency | how long one search takes | |
| Index | a structure that avoids comparing every vector |
The cells students fill in are left blank in this preview.
Starter
Have a go
Starter
4 questionsExact search compares with every vector
An exact kNN search works out the distance from the question to every vector, then keeps the k best. It never misses.
Its cost is the number of vectors times their length. The lab's 20,000 vectors of 48 numbers need 960,000 multiply-adds for one question. A million vectors of 768 numbers need 768 million, for every question.
An exact search in plain Python
docker exec tools python3 steps/exact.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
The cost of one exact search
| Vectors | Numbers each | Multiply-adds a question |
|---|---|---|
| 20,000 (the lab) | 48 | 960,000 |
| 1,000,000 | 768 | 768,000,000 |
| 100,000,000 | 1,536 | 153,600,000,000 |
The cells students fill in are left blank in this preview.
Check
Have a go
Check
3 questionsTry it: the work of exact search
# An exact search does one multiply-add per number of every vector.
vectors = 20000
numbers = 48
questions_a_second = 100
work = vectors * numbers
print("multiply-adds for one question:", work)
print("for", questions_a_second, "questions a second:", work * questions_a_second)
# Try 1,000,000 vectors of 768 numbers. How many times more work is that?
Make a free account to save your work and track your progress.
Recall@k measures what an index missed
An index compares the question with only a small part of the data, so ANN search can miss a true neighbour. You measure how much with recall@k.
Take the exact top 10 as the right answer. Count how many of them the index also returned, and divide by 10. Do it for many questions and take the mean, because one question can be lucky.
Worked example: recall@10 for one question
| List | Ids returned |
|---|---|
| Exact top 10 | 4, 9, 2, 7, 1, 8, 3, 6, 5, 10 |
| Index top 10 | 4, 9, 7, 2, 1, 8, 3, 11, 12, 6 |
| In both | 4, 9, 2, 7, 1, 8, 3, 6: 8 of 10 |
| Recall@10 | 0.8 |
The cells students fill in are left blank in this preview.
Check
Have a go
Check
3 questionsA common mistake
Warning
Many people think an approximate search returns the right items with slightly wrong scores. The scores it returns are exact. What it can do is miss an item altogether, so measure it with recall, not by checking scores.
More effort, more recall, more time
An HNSW index has a search setting, called hnsw_ef in Qdrant: how many candidates it keeps while it searches. A larger value finds more true neighbours and takes longer.
Choose the smallest setting that reaches the recall you need, measured on questions like your users' questions. In the lab the times are tiny because 20,000 vectors is small. The gap grows with the data.
Measure recall at five settings
docker exec tools python3 steps/recall.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
What recall.py printed in the lab
| Search | Recall@10, about | Time |
|---|---|---|
| Exact | 1.0 | The most |
| hnsw_ef 10 | 0.85 | Less than exact |
| hnsw_ef 16 | 0.92 | Less than exact |
| hnsw_ef 32 | 0.99 | Less than exact |
| hnsw_ef 64 and 128 | 1.0 | Grows with the setting |
The cells students fill in are left blank in this preview.
See PostgreSQL read every row
docker exec pg psql -U lab -d shop -c "EXPLAIN (COSTS OFF) SELECT id FROM docs ORDER BY embedding <=> (SELECT embedding FROM questions WHERE id = 1) LIMIT 10"Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsAssignment: measure recall and choose
Write three functions for measuring an index.
recall_at_k(exact, found, k) is the share of the first k ids of exact that are among the first k ids of found, as a fraction. mean_recall(pairs, k) takes a list of [exact, found] pairs, one per question, and gives the mean recall@k rounded to 2 places. cheapest(results, target) takes a list of [setting, recall, ms] rows and gives the setting with the smallest ms among those whose recall is at least target. If two rows have the same ms, give the earlier one. If none reaches the target, give None.
def recall_at_k(exact, found, k):
return 0.0
def mean_recall(pairs, k):
return 0.0
def cheapest(results, target):
return None
Make a free account to save your work and track your progress.
Exit quiz
Have a go