Glossary
Euclidean distance
The straight-line distance between two points or vectors, computed as the square root of the sum of squared differences between their coordinates.
Euclidean distance is the straight-line distance between two points. For two vectors, you subtract them coordinate by coordinate, square each difference, add the squares together and take the square root. It is the distance you would measure with a ruler in two or three dimensions, extended to any number of dimensions, and it is one of the three standard ways to compare embeddings in vector search, alongside cosine similarity and dot product. A smaller Euclidean distance means two vectors are more alike; zero means they are identical.
The formula and a worked example
For two vectors a and b with n dimensions, the Euclidean distance is the square root of (a1 minus b1) squared plus (a2 minus b2) squared, and so on up to (an minus bn) squared. It is also called the L2 distance, because it is the L2 norm (the length) of the difference vector a minus b.
Take a = (1, 2, 3) and b = (4, 6, 3). The differences are 3, 4 and 0. Squared, they are 9, 16 and 0, which add up to 25. The square root of 25 is 5, so the Euclidean distance between a and b is 5. Now take c = (1, 2, 4). Its only difference from a is 1 in the last coordinate, so the distance from a to c is 1, and c is much closer to a than b is.
The same arithmetic applies to an embedding with 768 or 1,536 dimensions. There are simply more terms in the sum. Each comparison costs one subtraction, one multiplication and one addition per dimension, which is why the number of dimensions has a direct effect on search speed and memory.
Euclidean distance in vector search
In vector search, every stored item has a vector, and a query is answered by finding the stored vectors closest to the query vector. That task is k-nearest neighbours search, and Euclidean distance is one of the measures it can use to decide what “closest” means. The engine computes the distance from the query to candidate vectors and returns those with the smallest values.
Most vector databases let you choose the measure when you create a collection, usually from Euclidean (often labelled L2), cosine and inner product. Postgres with pgvector exposes the same choice as different distance operators. The choice is not cosmetic: it changes which results rank first unless the vectors are normalised, and an index built for one measure cannot simply be queried with another.
Many indexes work with squared Euclidean distance instead of the distance itself. Skipping the square root saves time, and because the square root never changes which of two distances is smaller, the ranking is identical. If you see scores that look much larger than expected from an L2 index, they are probably squared.
Euclidean distance vs cosine similarity vs dot product
The three measures look at different things. Euclidean distance compares the positions of two vectors, so it is affected by both direction and length. Cosine similarity compares only the angle between them and ignores length. Dot product similarity multiplies matching coordinates and adds the results, which grows with both alignment and length.
Euclidean distance | Cosine similarity | Dot product | |
|---|---|---|---|
What it measures | Straight-line gap between points | Angle between vectors | Alignment scaled by length |
Better score | Smaller (0 is identical) | Larger (1 is identical direction) | Larger |
Sensitive to vector length | Yes | No | Yes |
Typical use | Image features, clustering, classic ML | Most text embeddings | Normalised embeddings, recommendation models |
Index label | L2 | cosine | inner product (IP) |
For vectors normalised to unit length, the three agree on ranking. In that case the squared Euclidean distance equals 2 minus 2 times the cosine similarity, so a smaller distance always means a higher cosine score, and the dot product equals the cosine similarity. Many embedding models return normalised vectors, which is why switching between these measures sometimes changes nothing. When vectors are not normalised, the measures can disagree. A long vector and a short vector pointing the same way have a cosine similarity of 1 but a large Euclidean distance.
The rule of thumb is to use the measure the embedding model was trained with. Text embedding models are usually trained with cosine similarity or dot product, and their documentation says which. Some image and audio feature extractors, and many classical machine-learning pipelines, are designed around Euclidean distance.
Why scale and normalisation matter
Because Euclidean distance adds up raw differences, any coordinate with a large range dominates the result. In classical data, if one feature is income in pounds and another is age in years, the income differences swamp the age differences, and the distance mostly measures income. The standard fix is to standardise each feature (subtract the mean and divide by the standard deviation) before measuring distance.
Embeddings have a related issue with overall vector length. If some documents produce longer vectors than others, perhaps because of their length or content, Euclidean search will treat them as further away from everything, even when they point in the right direction. Normalising every vector to unit length removes this effect and makes Euclidean ranking match cosine ranking. If you control the pipeline, normalise once at write time and once for each query, and keep both sides consistent.
Computing it efficiently
Search engines rarely compute Euclidean distance one pair at a time. The squared distance between a and b can be expanded as the squared length of a, plus the squared length of b, minus twice their dot product. The squared lengths of stored vectors can be computed once and cached, and the query’s length is computed once per query, so each comparison reduces to a single dot product. Dot products of many vectors at once are exactly what matrix multiplication hardware is built for, which is how CPUs with vector instructions and GPUs scan millions of vectors per second.
This expansion is also why L2 and inner product search are so closely related in practice. Some libraries convert one into the other by adding an extra dimension to each vector, so that a single index type can serve both. The ranking stays the same; only the bookkeeping changes.
Euclidean distance in high dimensions
In two or three dimensions, near and far are easy to tell apart. In hundreds of dimensions, distances behave differently. For many kinds of data, the distance from a query to its nearest neighbour and to its hundredth-nearest neighbour become relatively close, a pattern often described as distance concentration and part of the wider curse of dimensionality. Small differences in score then carry more of the ranking, and noise in the embedding has more influence.
This is one cause of semantic collapse, where results start to look equally plausible as a collection grows; our post on why vector search breaks at scale walks through what it looks like in production. Well-trained embeddings concentrate useful information in fewer effective dimensions, which keeps distances meaningful in practice, and techniques such as dimensionality reduction and Matryoshka embeddings shrink vectors while keeping most of the structure that matters. Keyword matching in hybrid search and a reranking stage also help when similarity scores alone are too close to separate.
Where Euclidean distance shows up in search infrastructure
Even when a system ranks by cosine similarity, Euclidean distance is often working underneath. The k-means clustering used to build an IVF index assigns each vector to its nearest centroid by Euclidean distance. Product quantisation splits vectors into sub-vectors and replaces each with the nearest entry in a small codebook, again measured by Euclidean distance, which is why it is a form of vector quantisation. Libraries such as FAISS are built around fast L2 and inner product computation, and graph indexes such as HNSW support it directly.
Other distances appear alongside it. Manhattan (L1) distance adds absolute differences instead of squared ones and is less dominated by a single large difference. Hamming distance counts differing bits and is used for binary quantisation, where each dimension is reduced to a single bit and comparisons become extremely cheap. Many systems use a cheap measure such as Hamming for a first pass and then re-score the shortlist with full-precision Euclidean or cosine scores.
Euclidean distance is particularly common with image embeddings, where tasks such as reverse image search and near-duplicate detection are often framed as finding the points closest to a query image in feature space.
Where Exabase fits
Exabase does not ask you to choose a distance measure or manage the vectors behind it. When you store files, notes, bookmarks, images, audio or video as Resources, Exabase extracts and indexes the content automatically, and Deep Search handles retrieval. Text queries are hybrid by default, combining semantic search with typo-tolerant keyword matching, so exact names and codes still match when similarity scores are close.
Deep Search also supports image queries that find visually similar images, colour search by dominant colour or palette, and resource similarity, where an existing resource is the query. These are the nearest-neighbour patterns that Euclidean distance is often used for, applied to asset search across a whole library without a separate pipeline per format. Rather than raw distances, results come with relevance scores and matched chunks, and the precision parameter (0 to 1, default 0.3) sets how close a result has to be. See the searching resources guide for request details, photo library search for a worked use case, and Exabase vs pgvector or best vector databases if you are weighing a self-managed setup where you would pick the metric yourself.
Frequently asked questions
What is Euclidean distance in simple terms?
Euclidean distance is the straight-line distance between two points. For vectors, it is the square root of the sum of the squared differences between their coordinates.
What is the formula for Euclidean distance?
The Euclidean distance between a and b is the square root of the sum, over every dimension i, of (ai minus bi) squared. For (1, 2, 3) and (4, 6, 3) that is the square root of 9 plus 16 plus 0, which is 5.
Is Euclidean distance the same as L2 distance?
Yes. Euclidean distance is the L2 norm of the difference between two vectors, so the terms are used interchangeably. Many vector databases label it L2, and some return the squared value.
Should I use Euclidean distance or cosine similarity for embeddings?
Use whichever measure your embedding model was trained with, which for most text models is cosine similarity or dot product. If your vectors are normalised to unit length, Euclidean distance and cosine similarity give the same ranking.
Why do my Euclidean distance scores look so large?
Your index is probably returning squared Euclidean distance, which skips the square root for speed. The ranking is the same as for the true distance.
Does Euclidean distance work in high dimensions?
It works, but distances between points tend to bunch together as dimensions grow, so small score differences decide the ranking. Good embeddings, normalisation, hybrid search and reranking keep results reliable.
Do I need to pick a distance metric in Exabase?
No. Exabase indexes stored content automatically and Deep Search handles similarity, so you set result strictness with the precision parameter instead of choosing a metric.
Related pages: Cosine similarity, Dot product similarity, K-nearest neighbours, Embedding dimensions, Vector search, Product quantisation, IVF index, Dimensionality reduction, Asset search