Skip to main content
Welcome back. In machine learning and vector databases, measuring similarity between vectors is a fundamental task. After you store embeddings in a vector store, the retrieval logic—how you compare an incoming query vector to stored vectors—determines the relevance of results. Should you require exact matches, or return nearest neighbors? Which similarity or distance metric best captures semantic similarity for your use case? Picking the right metric is essential for building effective semantic search, recommendation engines, and RAG (retrieval-augmented generation) systems. This article explains the three most common approaches to compare vectors: cosine similarity, Euclidean distance, and the dot product. For each method you’ll get the intuition, the formula, the numeric range, and practical guidance for when to use it. Why this choice matters
  • The metric determines which candidates are considered “close” and therefore returned to downstream systems.
  • Embedding generation (model architecture, normalization) often dictates which metric is appropriate.
  • Wrong choices can yield results that are numerically close but semantically irrelevant.
Cosine similarity
  • Intuition: measures the angle between two vectors and ignores their magnitudes. Vectors pointing in the same direction are similar regardless of length.
  • Formula: cosine(u, v) = (u · v) / (||u|| ||v||)
  • Range: -1 to 1 (1 = same direction, 0 = orthogonal/unrelated, -1 = opposite).
  • When to use: ideal for text embeddings and semantic search where topic/ direction matters more than length. Short and long texts about the same topic can still be similar.
  • Typical usage: commonly used with text embedding services such as OpenAI embeddings and supported by vector databases like Pinecone.
Euclidean distance
  • Intuition: the straight-line distance between two points in vector space (like measuring with a ruler). It depends on magnitude.
  • Formula: euclidean(u, v) = ||u - v||
  • Range: 0 to (0 = identical vectors; larger values = more distant).
  • When to use: appropriate when absolute magnitude encodes important information, for example in some image similarity tasks, sensor measurements, or anomaly detection where vector length represents intensity or scale.
  • Typical usage: supported by vector databases such as Weaviate and often used in computer vision scenarios.
Dot product (inner product)
  • Intuition: combines direction and magnitude — it measures alignment while scaling by vector lengths.
  • Formula: dot(u, v) = Σ u_i * v_i
  • Range: -∞ to +∞ (unbounded).
  • When to use: useful when both direction and magnitude matter, for example recommendation systems where raw model outputs (scores) are meaningful and should affect ranking.
Practical tip: if you normalize all vectors to unit length, the dot product becomes equivalent to cosine similarity. Normalization is common when you want to remove magnitude and compare only direction.
Quick comparison table
MetricIntuitionNumeric rangeUse casesCommon tools
Cosine similarityAngle between vectors; ignores length-1 to 1Text/semantic search, when topic matters over lengthOpenAI embeddings, Pinecone
Euclidean distanceStraight-line distance; uses magnitude0 to Vision tasks, sensor intensity, anomaly detectionWeaviate, FAISS
Dot productAlignment weighted by magnitude-∞ to +∞Recommenders, when magnitudes encode signalModel scoring, ANN libraries
How to choose between them
  • If you care only about semantic direction/topic and want to ignore length differences, use cosine similarity (or normalize vectors and use dot product).
  • If absolute magnitude encodes meaningful signals (intensity, confidence, energy), prefer Euclidean distance or unnormalized dot product.
  • Use dot product when the model’s raw outputs (magnitudes) should directly influence ranking or scoring.
The image describes three ways to measure similarity: Cosine Similarity (measures the angle between vectors), Euclidean Distance (measures straight-line distance), and Dot Product (combines direction and magnitude). Each method's range is also illustrated.
Before finalizing a metric, inspect how your embeddings are produced and whether vector magnitudes carry semantic meaning. If needed, experiment with normalization and test retrieval quality on held-out queries to evaluate real-world relevance rather than relying purely on numeric closeness. Further reading and references That’s it for this lesson.

Watch Video