Rank two dense neighbors by cosine

Read identifiers and quoted passages in short groups. · 155 words

Dense retrieval turns a query and each candidate passage into a list of numbers called a vector. A nearest neighbor is a candidate close to the query vector. Cosine similarity compares direction, not size: it divides the dot product by the product of vector lengths. The dot product is the sum of matching coordinate products. In a museum app, q=[1,0] means 'red paintings.' Candidate A=[1,0] is about red paintings; B=[0,1] is about blue paintings. Their dot products are 1 and 0, and both lengths are 1. Thus cos(A)=1 and cos(B)=0, so A ranks first.

A second check uses decimals. A search for 'quiet reading room' uses q=[0.8,0.6]. Candidate C=[0.8,0.6] has the same direction: dot=0.64+0.36=1.00 and length=sqrt(0.64+0.36)=1. Candidate D=[0.6,0.8] has dot=0.48+0.48=0.96 and length 1. The cosines are 1.00 and 0.96, so C ranks first, but D is close. Cosine compares direction while ignoring vector magnitude. Magnitude is the vector's overall size, represented here by its length.

Check your recall

For q=[1,0], A=[1,0], and B=[0,1], what are the two cosine values, and which candidate ranks first?

Check your answer

A has cosine 1 and B has cosine 0. A ranks first because its direction matches the query exactly.

Sources and credits

Original KeyTouch lesson. Background reading · Lewis et al. · Retrieval-Augmented Generation.