Distance Metrics: Cosine vs Euclidean vs Dot Product
You'll learn: How to choose between cosine similarity, Euclidean distance, and dot product based on your embedding characteristics and search requirements.
The Core Difference
When retrieving similar vectors, the distance metric determines what "similar" means mathematically. Each metric emphasizes different properties:
Cosine Similarity
Measures the angle between vectors, ignoring magnitude. Perfect when you care only about direction—whether two documents discuss similar topics, regardless of length.
Use when: Your embeddings aren't normalized, or magnitude is irrelevant (most text embeddings).
Euclidean Distance
Measures the straight-line distance between points in space. Treats all dimensions equally and considers both direction and magnitude.
Use when: Magnitude matters (e.g., images where brightness levels are significant), or you need geometric properties.
Dot Product
The raw inner product of vectors. When vectors are normalized (length = 1), dot product equals cosine similarity but computes faster.
Use when: Your embeddings are pre-normalized (like OpenAI's) and you want maximum speed.
Practical Decision Tree
- Are your vectors normalized? → Use dot product (fastest)
- Does magnitude carry meaning? → Use Euclidean distance
- Only direction matters? → Use cosine similarity
Think of it like comparing arrows: cosine checks if they point the same way, Euclidean checks how far apart their tips are, and dot product gives you both—but only works reliably when arrows have the same length.
Key Takeaway: For most text embedding use cases (OpenAI, Cohere, Sentence Transformers), use cosine similarity or dot product on normalized vectors—they're equivalent but dot product is faster. Choose Euclidean only when vector magnitude has semantic meaning in your domain.