Measure recall at k of an approximate index
Why it matters FAISS, HNSW and every vector DB benchmark report recall at 10 against brute force.
To tune a vector index you compare its results to exact search.
Write ann_recall(approx, exact, k). Both are lists of result-id lists per query. Return the mean over queries of |set(approx[:k]) n set(exact[:k])| / k.
def ann_recall(approx, exact, k)Example
ann_recall([[1, 2, 9]], [[1, 2, 3]], 3) is 2/3.
Understand the idea
Approximate nearest neighbour indexes trade a little accuracy for speed. To tune one, you compare its top k results with the true top k from exact search and ask what fraction it found. Order inside the top k does not matter, only membership. Worked example: approx = [[4, 7, 1], [2, 3, 5]], exact = [[1, 4, 9], [2, 3, 5]], k = 3. Query 1: {4, 7, 1} and {1, 4, 9} share 2 ids, so 2/3. Query 2: all 3 shared, so 1. Mean = (0.667 + 1) / 2 = 0.833. With k = 2, query 1 compares {4, 7} with {1, 4}, 1 shared, so 0.5; query 2 compares {2, 3} with {2, 3}, so 1; mean 0.75. Rule: for each query, size of (set(approx[:k]) intersect set(exact[:k])) divided by k, then average over queries. Cut both lists to k before comparing. Used in: tuning HNSW, IVF and other vector index settings such as ef_search or nprobe in FAISS, Milvus or pgvector, balancing recall against latency.