Vector similarity

On this page 7

Vector similarity is a numerical measure of how close two vectors are in a high-dimensional space, used to decide how related the items they represent are. In retrieval systems built on artificial intelligence, texts, images, audio or products are converted into embeddings, and a similarity function such as cosine similarity, dot product or Euclidean distance turns the geometric closeness of those embeddings into a ranking of semantic relatedness.

Vector similarity is the core operation behind semantic search, retrieval-augmented generation (RAG), recommendation, deduplication and clustering. Its usefulness depends on two things: an embedding model that places related items near each other, and a metric that matches how that model was trained.

How vector similarity works

An embedding model maps each item to a vector of fixed length, commonly 384 to 3,072 dimensions for text. Training pushes related items, such as a question and the passage that answers it, toward nearby positions and unrelated items apart. Similarity is then a simple arithmetic computation on two vectors: their angle, their inner product or the distance between their endpoints.

For a query, the system computes the query embedding and finds the stored vectors with the highest similarity or lowest distance, called the nearest neighbors. The result is a ranked list, usually the top k items, where k is often between 5 and 100 depending on whether the results go straight to a user or to a reranking step.

Similarity is only as meaningful as the embedding space behind it. A general-purpose text model may place two legal clauses close together because they share vocabulary, while a domain-tuned model separates them by their actual obligations. For this reason, teams evaluate retrieval quality on their own queries before choosing a model, rather than relying on general benchmark rankings.

Similarity and distance metrics

Several metrics are in common use. They differ in whether they consider vector length (magnitude) or only direction, and in their value ranges. The table below summarizes the main options.

MetricComputationRangeInterpretation
Cosine similarityDot product divided by both vector lengths-1 to 1Higher means more similar direction
Dot product (inner product)Sum of element-wise productsUnboundedHigher means more similar, magnitude counts
Euclidean distance (L2)Straight-line distance between points0 to infinityLower means more similar
Manhattan distance (L1)Sum of absolute differences0 to infinityLower means more similar
Hamming distanceNumber of differing bits0 to number of bitsUsed for binary vectors
Jaccard similarityOverlap of sets divided by union0 to 1Used for sparse sets of features
Common vector similarity metrics

When vectors are normalized to unit length, cosine similarity, dot product and Euclidean distance produce the same ranking: the squared Euclidean distance equals 2 minus twice the cosine similarity. Many embedding models output normalized vectors, which is why vector databases often use the cheaper dot product internally.

Absolute scores are not comparable across models. A cosine similarity of 0.8 may indicate a near-duplicate in one model and a loose topical match in another, so fixed thresholds must be calibrated on labeled examples for each model.

Vector similarity compared with lexical matching

Vector similarity captures meaning rather than shared words, so it matches “car insurance cost” with “price of auto coverage”. Lexical methods such as BM25 score exact term overlap, which makes them better at rare identifiers, product codes and names. The two approaches fail in complementary ways.

AspectVector similarityLexical matching (BM25)
Matches synonyms and paraphrasesYesOnly with expansion
Matches exact codes and namesOften weakStrong
Requires an embedding modelYesNo
Explainability of matchesLowHigh, shared terms are visible
Cross-language retrievalPossible with multilingual modelsNot without translation
Index typeApproximate nearest neighbor indexInverted index
Dense vector similarity and lexical retrieval

Hybrid search runs both and merges the rankings, often with reciprocal rank fusion, and a reranking model then rescores the top candidates by reading the query and each passage together. This combination is a common default for production retrieval.

Computing similarity at scale

Comparing a query with every stored vector, called exact or brute-force search, is precise but scales linearly with the collection. For millions or billions of vectors, systems use approximate nearest neighbor search (ANN), which accepts a small loss in recall for large gains in speed.

MethodPrincipleTrade-off
Exact (flat) searchCompare with every vectorPerfect recall, slow at large scale
HNSWNavigable multilayer proximity graphHigh recall and speed, high memory use
IVFSearch only the nearest clustersLower memory, recall depends on clusters probed
Product quantizationCompress vectors into short codesLarge memory savings, some accuracy loss
Binary quantizationReduce each dimension to one bitVery fast, usually needs rescoring
Search methods for vector similarity

ANN quality is measured as recall at k against exact search; well-tuned indexes commonly reach recall well above 0.9. Parameters such as the HNSW search breadth or the number of IVF clusters probed let operators trade latency for recall per query.

Practical choices and common mistakes

  • Use the metric the embedding model was trained with; mixing metrics silently degrades ranking.
  • Normalize vectors consistently at indexing and query time when using cosine similarity.
  • Never compare vectors from different models or model versions; reindex after upgrading.
  • Calibrate thresholds per model and domain instead of reusing a universal cutoff.
  • Watch chunk size: very long chunks blur meaning, very short ones lose context.
  • Beware the hubness effect, where a few vectors appear as neighbors of many queries in high dimensions.

Stripped to the essentials

Vector similarity is a way for computers to measure how alike two things are in meaning. Each sentence or picture is turned into a list of numbers, like coordinates on a huge map.

Things with similar meaning land close together on that map. To find related material, the computer simply looks for the nearest points. Closeness on the map stands for closeness in meaning, even when no words are shared.

Where it is applied

A software company runs a support assistant over about 20,000 help articles and resolved tickets. Each document is split into passages of a few hundred words, and each passage is embedded into 1,024-dimensional normalized vectors stored in an HNSW index.

When a customer writes that their invoices show the wrong tax for another country, the system retrieves passages about VAT settings for cross-border billing, although the customer never used the word VAT. A BM25 search runs in parallel to catch exact error codes, and a reranker selects the best five passages for the language model. First-contact resolution improved noticeably, and agents spend less time searching for the right article.