Demonstrates a fruit recommendation example showing how different embedding similarity metrics like Euclidean, cosine, and dot product affect nearest neighbor rankings
Welcome back. In this lesson we move from theory to practice by building a tiny fruit recommendation example and experimenting with different similarity metrics on embeddings. The central question is: someone picks mango — which other fruits are most similar?This is the exact kind of query used by product recommendation engines, semantic search systems, and retrieval-augmented generation (RAG) pipelines. We use a compact, concrete example to illustrate how similarity search works and how metric choice affects ranking.
We store the following items in a vector database:
Query fruit: mango
Other fruits in the database: pineapple, banana, orange, papaya, lemon
Each fruit is encoded as a 3-dimensional vector with components: sweetness, sourness, and tropicalness. The image below visualizes those vectors and highlights some high-level observations.
Each fruit vector locates the item in a 3D feature space (sweetness, sourness, tropicalness). When comparing fruits you compare vectors — not isolated features. Which dimensions matter depends on the question you ask:
“Find a fruit with the same sweetness as mango” — prioritize the sweetness component.
“Find a fruit like mango but less tropical” — prioritize tropicalness.
“Find similar fruits to mango” — compare the full vector using a similarity or distance metric.
Important: a tabular listing of vectors (like the image above) does not imply geometric proximity — proximity must be computed using a metric.
Projecting the vectors into two dimensions (for example, sweetness vs tropicalness) helps build visual intuition. Below, mango is the query point and other fruits are plotted relative to it. Note: the sourness axis is not shown, so the 2D projection only approximates true 3D proximity.
From this projection you can typically observe:
Banana appears very close to mango (both sweet and tropical).
Orange is moderately close.
Lemon is far away (low sweetness, low tropicalness).
Pineapple and papaya are tropical and may appear near mango depending on their sweetness and sourness.
All three metrics often agree on the top match when one neighbor is clearly closest and vector magnitudes are similar. Differences typically appear in the top-k ranking (rank 2, rank 3, etc.), which matters for recommendation systems that return multiple candidates.
When building recommendations, check ranking differences across metrics. The top result may be the same, but the ordering of subsequent candidates can change significantly depending on whether you use Euclidean distance, cosine similarity, or dot product.
Cosine similarity: prioritizes direction (relative proportions of sweetness, sourness, tropicalness). Good when you want size-invariant similarity (e.g., proportional taste profile).
Euclidean distance: prioritizes absolute differences across features. Use this if exact feature magnitudes matter (you need the same level of sweetness).
Dot product: favors vectors with larger magnitudes and good alignment. Use when strong feature values should dominate the ranking.
In practice, you would compute the three metrics for each candidate fruit and then compare the top-3 results to see how they differ. For this mango example you can expect banana to often be top-ranked, while the order of pineapple, papaya, and orange may shift depending on the metric.
If your embeddings are normalized (unit length), cosine similarity and dot product yield equivalent rankings — consider normalization if you care about direction only.
If feature scales vary a lot, consider standardizing or normalizing features before computing Euclidean distance.
For production systems, benchmark top-k precision across metrics using held-out queries to pick the metric that matches your business objective (user satisfaction, click-through, conversion).
Use case
Recommended metric
Match by taste proportions (size-invariant)
Cosine similarity
Match by exact taste intensity
Euclidean distance
Favor items with strong signals (magnitude matters)