Notes

Choose cosine, dot product or L2 for embeddings

For unit-normalized embeddings, cosine similarity, dot product and squared L2 produce the same ranking. Match your index operator and ordering to the embedding model.

Use the metric recommended by your embedding model. If both stored vectors and queries have unit length, maximizing cosine similarity, maximizing dot product and minimizing L2 distance produce the same ranking. Without normalization they generally differ, because dot product and L2 also respond to vector magnitude.

See the equivalence numerically

For unit vectors x and y, cosine similarity equals their dot product, and squared L2 distance is 2 - 2 * dot(x, y). Squaring a nonnegative distance preserves its ranking. This dependency-free Python example also shows how an unnormalized dot product changes the result:

from math import sqrt

def dot(a, b):
    return sum(x * y for x, y in zip(a, b, strict=True))

def unit(v):
    norm = sqrt(dot(v, v))
    if norm == 0:
        raise ValueError("zero vectors have no cosine direction")
    return [x / norm for x in v]

query = unit([1, 0])
candidates = {"aligned": [1, 0], "large": [10, 1], "tilted": [1, 1]}
for name, raw in candidates.items():
    v = unit(raw)
    ip = dot(query, v)
    l2_squared = sum((a - b)**2 for a, b in zip(query, v))
    assert abs(l2_squared - (2 - 2 * ip)) < 1e-12
    print(f"{name}: raw-dot={dot(query, raw):.1f}, "
          f"cosine={ip:.4f}, squared-L2={l2_squared:.4f}")

Output:

aligned: raw-dot=1.0, cosine=1.0000, squared-L2=0.0000
large: raw-dot=10.0, cosine=0.9950, squared-L2=0.0099
tilted: raw-dot=1.0, cosine=0.7071, squared-L2=0.5858

Raw dot product prefers large; all three normalized comparisons prefer aligned. Reject zero vectors when using cosine. Normalize both sides consistently, and preserve any model-specific query/document prefixes before encoding.

Match the pgvector operator class

The operator class used to build an approximate index must agree with the query’s distance operator. The pgvector reference defines these pairs:

Desired comparison HNSW operator class Query expression, ascending
Cosine distance vector_cosine_ops embedding <=> query
Maximum inner product vector_ip_ops embedding <#> query
L2 distance vector_l2_ops embedding <-> query

<#> returns the negative inner product so ascending index scans return the largest dot products first. Cosine distance is 1 - cosine_similarity; it is not the similarity itself. For an existing three-dimensional items.embedding column, the cosine pair looks like this:

CREATE INDEX items_embedding_cosine ON items
USING hnsw (embedding vector_cosine_ops);

SELECT id, embedding <=> '[1,0,0]'::vector AS distance
FROM items
ORDER BY embedding <=> '[1,0,0]'::vector
LIMIT 10;

Keep the bare distance expression in ascending ORDER BY with a LIMIT when you want the approximate index path. Converting distance into a similarity expression inside the ordering can prevent the intended plan.

Match FAISS semantics too

FAISS IndexFlatIP performs exhaustive maximum-inner-product search; normalize database and query vectors for cosine search. IndexFlatL2 performs exhaustive search and returns squared L2 distances, avoiding the square root. See the official metric guidance.

Equivalent mathematical rankings do not promise identical approximate-index recall or tie handling. Rebuild and evaluate after changing normalization, model or metric, and recalibrate any score threshold. A cosine threshold cannot be copied directly into an L2 filter without conversion.

Go deeper into embedding geometry.