What is cosine similarity and when do you use it?easy

Type
applied
Topic
similarity
Frequency
very common
Tags
similarity
Answer

Cosine similarity = (a dot b) / (|a| × |b|) - the dot product divided by the product of magnitudes. It measures directional similarity regardless of vector magnitude, ranging from -1 (opposite) to 1 (identical direction).

Explanation

Use cosine similarity when magnitude should not matter - text documents of different lengths, embedding vectors where direction encodes meaning. A long document and a short document with the same vocabulary distribution are cosine-similar despite different word count vectors. In RAG and semantic search, the retrieval step finds the top-k chunks by cosine similarity to the query embedding. FAISS and vector databases optimize this search at scale.

Follow-upWhen would you use Euclidean distance instead of cosine similarity?