Cosine similarity is a metric that measures the similarity between two non-zero vectors by calculating the cosine of the angle between them in a multi-dimensional space. It is a core technique in entity resolution for comparing high-dimensional data representations, such as text embeddings, where it evaluates directional alignment rather than magnitude. The resulting value ranges from -1 (perfectly opposite) to 1 (identical direction), with 0 indicating orthogonality. It is computationally efficient and widely used in semantic search and record linkage.
Glossary
Cosine Similarity

What is Cosine Similarity?
A fundamental metric for quantifying the similarity between two vectors, crucial for comparing text embeddings and entity records.
In practice, cosine similarity is favored for textual data because it is robust to document length, allowing a short document and a long document on the same topic to be recognized as similar. It is a foundational component in vector database retrieval, clustering algorithms, and recommendation systems. For entity resolution, it is often used alongside other similarity measures like Jaccard similarity and Levenshtein distance to create a composite similarity score for probabilistic matching.
Key Features of Cosine Similarity
Cosine similarity is a fundamental metric for measuring the directional alignment between two vectors, widely used in information retrieval and machine learning. Its key properties make it uniquely suited for comparing high-dimensional data like text embeddings.
Angle-Based Measurement
Cosine similarity measures the cosine of the angle between two vectors in a multi-dimensional space. It is defined as the dot product of the vectors divided by the product of their magnitudes (L2 norms).
- Formula:
cos(θ) = (A · B) / (||A|| * ||B||) - Range: Outputs a value between -1 and 1.
- Interpretation: A value of 1 indicates identical direction (angle of 0°), 0 indicates orthogonality (90°), and -1 indicates opposite direction (180°).
This focus on orientation, not magnitude, is its defining characteristic.
Magnitude Invariance
A core feature is its insensitivity to vector magnitude. It assesses similarity based purely on the direction of the vectors, not their length.
- Example: In text analysis, the documents "The cat sat." and "The cat sat on the mat." will have similar embeddings. Even though the second vector has a larger magnitude (more words), their cosine similarity can be high because the direction of their semantic meaning is aligned.
- Benefit: This makes it robust for comparing documents of different lengths or data points with varied scales, a common scenario in real-world datasets.
High-Dimensional Efficiency
Cosine similarity is computationally efficient and well-behaved in high-dimensional spaces, such as those created by modern embedding models (e.g., 384, 768, or 1536 dimensions).
- Curse of Dimensionality: In high dimensions, Euclidean distance becomes less meaningful as most points become equidistant. Cosine similarity, focusing on angle, remains a stable metric.
- Use Case: It is the standard similarity measure for nearest neighbor search in vector databases when using dense embeddings from models like sentence-transformers or OpenAI's embeddings API.
Core Use in Text & Embeddings
It is the de facto metric for comparing text embeddings generated by transformer models, forming the backbone of semantic search and retrieval-augmented generation (RAG).
- Semantic Search: Queries and documents are converted to vectors. The system retrieves documents with the highest cosine similarity to the query vector.
- Clustering & Recommendations: Used in algorithms like spherical k-means (which clusters based on vector direction) and for building content-based recommendation systems.
- Practical Note: For purely positive-valued vectors (like TF-IDF or many deep learning embeddings), the similarity range is typically between 0 and 1.
Relationship to Other Metrics
Cosine similarity is mathematically related to, but distinct from, other common distance and similarity measures.
- Euclidean Distance: Measures the straight-line distance between vector points. For L2-normalized vectors (unit vectors), Euclidean distance and cosine similarity are inversely related:
Euclidean Distance² = 2 * (1 - Cosine Similarity). - Dot Product: The numerator of the cosine formula. Without normalization by magnitude, the dot product is sensitive to both direction and length.
- Choosing a Metric: Use cosine similarity when direction matters most; use Euclidean distance when absolute magnitude differences are critical.
Implementation & Normalization
Effective use requires understanding its implementation, often involving a pre-processing step of L2 normalization.
- L2 Normalization: Scaling each vector to have a unit norm (magnitude of 1). After this step,
cosine_similarity(A, B) = dot_product(A_normalized, B_normalized). This simplifies computation and storage. - Computational Form: For large-scale comparisons, such as in vector databases, the similarity is often calculated using optimized linear algebra libraries that compute normalized dot products.
- Thresholding: In entity resolution, a similarity threshold (e.g., 0.8 or 0.9) is applied to decide if two vectorized records represent a match.
Cosine Similarity vs. Other Similarity Metrics
A comparison of common similarity and distance metrics used in entity resolution for comparing records, vectors, and text embeddings.
| Metric / Feature | Cosine Similarity | Euclidean Distance | Jaccard Index | Manhattan Distance |
|---|---|---|---|---|
Primary Use Case | Comparing direction/orientation of vectors (e.g., text embeddings). | Measuring straight-line distance between points in space. | Comparing similarity of finite sample sets. | Measuring distance along grid-like (axis-aligned) paths. |
Range of Values | [-1, 1] or [0, 1] for non-negative vectors. | [0, ∞) | [0, 1] | [0, ∞) |
Sensitivity to Magnitude | ||||
Interpretation | 1 = identical direction, 0 = orthogonal, -1 = opposite direction. | 0 = identical points. Larger values = greater dissimilarity. | 1 = identical sets, 0 = no overlap. | 0 = identical points. Sum of absolute differences. |
Common Data Type | Dense vectors (e.g., word/document embeddings). | Numeric feature vectors (coordinates). | Sets, binary vectors, tokenized text. | Numeric feature vectors. |
Impact of High Dimensionality | Often remains stable; focuses on angle. | Suffers from the "curse of dimensionality"; distances become less meaningful. | Applicable but requires set representation. | Suffers from the "curse of dimensionality". |
Typical Entity Resolution Application | Comparing TF-IDF or embedding vectors for text fields. | Matching on scaled, continuous numerical attributes (e.g., age, price). | Comparing tokenized text (e.g., product descriptions, names). | Matching on categorical or ordinal counts. |
Computational Complexity (pairwise) | O(d) for d-dimensional vectors. | O(d) for d-dimensional vectors. | O(|A|+|B|) for sets A and B. | O(d) for d-dimensional vectors. |
Frequently Asked Questions
Cosine similarity is a fundamental metric for quantifying the likeness between two vectors, widely used in entity resolution to compare text embeddings and determine if records refer to the same real-world entity.
Cosine similarity is a metric that measures the similarity between two non-zero vectors in an inner product space by calculating the cosine of the angle between them. It is defined as the dot product of the vectors divided by the product of their magnitudes (L2 norms). The resulting value ranges from -1 to 1, where 1 indicates identical orientation (maximum similarity), 0 indicates orthogonality (no correlation), and -1 indicates opposite orientation (maximum dissimilarity). In practice, for normalized vectors (e.g., text embeddings), the range is typically between 0 and 1, as vectors are often constrained to a positive space.
Mathematically, for vectors A and B, it is calculated as:
codecosine_similarity(A, B) = (A · B) / (||A|| * ||B||)
Its primary advantage in machine learning is its invariance to vector magnitude, making it ideal for comparing documents or entities where the focus is on directional alignment in a high-dimensional space rather than raw magnitude.
Enabling Efficiency, Speed & Accuracy
Intelligent Analysis, Decision & Execution
We build AI systems for teams that need search across company data, workflow automation across tools, or AI features inside products and internal software.
Talk to Us
Search across company data
Give teams answers from docs, tickets, runbooks, and product data with sources and permissions.
Useful when people spend too long searching or get different answers from different systems.

Automate internal workflows
Use AI to route work, draft outputs, trigger actions, and keep approvals and logs in place.
Useful when repetitive work moves across multiple tools and teams.

Add AI to products and internal tools
Build assistants, guided actions, or decision support into the software your team or customers already use.
Useful when AI needs to be part of the product, not a separate tool.
Related Terms
Cosine similarity is a core metric within entity resolution pipelines. These related concepts define the broader ecosystem of techniques for identifying and linking records that refer to the same real-world entity.
Similarity Score
A similarity score is a numerical value, typically normalized between 0 and 1, that quantifies the degree of likeness between two data points, records, or vectors. In entity resolution, it is the fundamental output of comparison functions.
- Purpose: Drives match/no-match decisions, often used as input to a downstream classification model or threshold rule.
- Calculation: Can be derived from various metrics like cosine similarity, Jaccard index, or Levenshtein distance.
- Aggregation: For multi-attribute records, individual attribute similarity scores are often combined (e.g., weighted average) to produce an overall record similarity score.
Jaccard Similarity
Jaccard similarity (or Jaccard index) measures the similarity between two finite sets. It is defined as the size of the intersection divided by the size of the union of the sets.
- Formula:
J(A, B) = |A ∩ B| / |A ∪ B| - Use Case: Ideal for comparing tokenized text (e.g., product descriptions, bag-of-words models) or categorical data represented as sets.
- Contrast with Cosine: While cosine similarity operates on vectors with magnitudes, Jaccard is purely set-based and ignores element frequency or weight. For binary vector representations, Jaccard and cosine similarity are related but not identical.
Vector Embeddings
A vector embedding is a dense, low-dimensional numerical representation of data (like text, an image, or an entity) in a continuous vector space. Cosine similarity is most meaningfully applied to these representations.
- Creation: Generated by models like Word2Vec, Sentence Transformers, or foundation model APIs.
- Property: Semantically similar items have embeddings that are close in the vector space, as measured by high cosine similarity.
- Role in ER: In modern entity resolution, records are often converted to embeddings (e.g., of a concatenated description field). Matching then becomes a nearest neighbor search in this embedding space using cosine similarity as the distance metric.
Locality-Sensitive Hashing (LSH)
Locality-Sensitive Hashing is a technique that hashes input items so that similar items map to the same hash 'buckets' with high probability. It is a critical performance optimization for similarity search.
- Purpose: Approximates nearest neighbor search in sublinear time, avoiding the quadratic cost of comparing all pairs. This is essential for blocking in large-scale entity resolution.
- Mechanism: Uses families of hash functions designed to preserve similarity (e.g., SimHash for cosine similarity).
- Trade-off: Introduces a tunable balance between recall (finding true matches) and computational efficiency.
Probabilistic Matching
Probabilistic matching is a statistical entity resolution method that calculates the likelihood that two records refer to the same entity. Cosine similarity often serves as a key feature within this framework.
- Foundation: Based on the Fellegi-Sunter model, which estimates probabilities for attribute agreements given a match or non-match.
- Process: Combines evidence from multiple, possibly imperfect, fields (e.g., name, address, date of birth) to compute a composite match probability.
- Integration: A cosine similarity score on a text field (like 'company name') can be converted into a likelihood ratio and incorporated into the overall probabilistic score.
Siamese Networks
A Siamese network is a neural architecture that uses two or more identical subnetworks to process different input samples. Its goal is to learn a representation where similarity can be computed directly (e.g., via cosine distance).
- Structure: The twin networks share the same weights and parameters. They output embeddings for their respective inputs.
- Training: Trained on pairs of records (match/non-match) using a contrastive or triplet loss function, which teaches the network to pull similar items closer and push dissimilar items apart in the embedding space.
- Output: The cosine similarity between the two output embeddings becomes a highly tuned, task-specific similarity score for entity resolution.

About the author
Prasad Kumkar
CEO & MD, Inference Systems
Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.
His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.
Partnered with leading AI, data, and software stack.
How We Work
Custom AI workflows for your Business
One-fit-all AI don't work for modern businesses. At Inferensys, we aim to understand your business & custom requirements; which we use to define most efficient agentic workflows, the data, and the tools for your business.
01
Review the use case
We understand the task, the users, and where AI can actually help.
Read more02
Pick the right approach
We define what needs search, automation, or product integration.
Read more03
Build the first useful version
We implement the part that proves the value first.
Read more04
Improve from there
We add the checks and visibility needed to keep it useful.
Read moreThe first call is a practical review of your use case and the right next step.
Talk to Us