When you search for something and get a result that feels right even though it doesn't share any words with your query, something interesting happened. The system didn't match keywords. It measured similarity. The question worth asking is: what does "similar" actually mean to a computer, and how does it measure it?
The answer starts with embeddings. Every piece of content — a sentence, a paragraph, a document — gets converted into a vector, a point in a high-dimensional mathematical space. Two pieces of content that mean similar things end up close together in that space. Two pieces that mean different things end up far apart. Similarity becomes a geometry problem: find the points that are closest to the query point.
The interesting part is that "closest" isn't a single definition. Different distance formulas measure different things, and the choice between them reflects a genuine assumption about what kind of similarity matters.
Cosine similarity measures the angle between two vectors, not the distance between their endpoints. This makes it sensitive to the direction of meaning rather than the magnitude, which is why it's the most common choice for text: a short document and a long document about the same topic will have vectors pointing in similar directions even if their lengths differ. Dot product similarity is related but also accounts for magnitude, which makes it useful when the strength of a signal matters, not just its direction. Euclidean distance measures the straight-line distance between two points, the way you'd measure distance on a map. Manhattan distance measures the same thing but only along the axes, like navigating a city grid.
Once you have a way to measure similarity, the next problem is speed. Comparing a query vector to millions of stored vectors one by one is too slow to be useful. Approximate nearest neighbors (ANN) algorithms solve this by trading a small amount of accuracy for a large gain in speed: instead of finding the exact closest match, they find something very close to it, fast enough to work at scale. HNSW, FAISS, and Annoy are three widely used implementations of this idea, each with different tradeoffs in speed, memory, and accuracy.
The distance formula you choose and the search algorithm you use are two separate decisions. Together, they determine how your retrieval system defines "similar" and how fast it can find it.


