Vector DBs & RAG (50 Qs)•Part 3: Advanced Internals, Scale & Production Patterns (Senior / Lead)
Distance Metrics in Vector Space: Cosine Similarity, Dot Product, Euclidean Distance (L2), and Manhattan Distance (L1).
senior LevelMost AskedCLI
In Simple Words: Choosing the right metric depends on how the embedding model was trained:
- Cosine Similarity: Measures the cosine of the angle between two vectors:
$$cos( heta) = rac{A cdot B}{|A| |B|}$$
- Range from -1 to 1. Completely invariant to vector magnitude (length). Ideal for text documents of varying length.
- Dot Product (Inner Product):
$$A cdot B = sum A_i B_i$$
- Fastest to compute. If vectors are normalized to unit length ($|A|=1$), Dot Product is mathematically identical to Cosine Similarity!
- Euclidean Distance (L2): Measures straight-line geometric distance:
$$sqrt{sum (A_i - B_i)^2}$$
- Sensitive to vector magnitude. Common in image feature embeddings and clustering algorithms.