Problem. Given a corpus of n embeddings E (n × d) and m query embeddings Q (m × d), return for each query the indices of the k
corpus vectors with the highest cosine similarity, in descending order of similarity.
cos(q, e) = (q · e) / (‖q‖ · ‖e‖) ranges from −1 (opposite) to 1 (same direction)
E = [[1, 0], [0, 1], [1, 1], [−1, 0]], Q = [[2, 1]], k = 2 → [[2, 0]]
cos: 0.894, 0.447, 0.949, −0.894
Cosine similarity ignores length and compares direction, which is what embedding models encode meaning in. The computation is a dot product divided by two norms. The optimisations: compute the corpus norms once (not once per query), do all dot products as one matrix product, and only sort the k scores you return.
ML context. This is the retrieval step of RAG, semantic search, recommendation by item embeddings and few-shot example selection: embed the query, find the nearest corpus vectors. Vector databases implement exactly this, with approximate indexes once n is large (Change C). Run on the corpus against itself, it finds near-duplicates in a training set (Change D).