Module 3: Similarity and Distance

6. Metrics in Semantic Search

Overview

Here you see how the metrics are used in real semantic search systems: what OpenAI, Pinecone and Weaviate use, and why.


The typical semantic search stack

1. Generate embeddings (the OpenAI API)
   → 1536D vectors

2. Normalize (optional but recommended)
   → Magnitude = 1

3. Store in a vector database (Pinecone)
   → An optimized index

4. Query → embedding → search by cosine
   → Top-K results

Configuration in vector databases

Pinecone:

Default metric: "cosine"
Alternatives: "euclidean", "dotproduct"

Recommendation: Use "cosine" for normalized embeddings

Weaviate:

Default metric: "cosine"
Alternatives: "l2-squared" (squared Euclidean)

Recommendation: "cosine" for semantic search

Qdrant:

Default metric: "cosine"
Alternatives: "euclidean", "dot"

Recommendation: "cosine" with normalization

Why everyone uses cosine

Reason 1: Pre-trained embeddings (OpenAI, BERT) work better with cosine.

Reason 2: Normalization + cosine = a simple dot product (more efficient).

Reason 3: Interpretable values (0.95 = very similar).


Normalization: Always necessary?

With cosine: Optional but recommended.

  • Normalize → cosine = the dot product (faster)
  • Without normalizing → it works the same, but the computation is more complex

With Euclidean: Don't normalize (magnitude matters).

Best practice: Normalize embeddings before storing them in a vector database.


A production example

A typical setup:

# Illustrative (NOT real code for this guide)
import openai
import pinecone

# 1. Generate the embedding
text = "domestic animal"
embedding = openai.Embedding.create(
    input=text,
    model="text-embedding-3-small"
)["data"][0]["embedding"]

# 2. Normalize (optional)
normalized = normalize(embedding)

# 3. Search by cosine
results = pinecone.query(
    vector=normalized,
    top_k=5,
    metric="cosine"
)

Interpreting the results

Result 1: score 0.95 → Very relevant
Result 2: score 0.88 → Relevant
Result 3: score 0.72 → Moderately relevant
Result 4: score 0.45 → Barely relevant
Result 5: score 0.12 → Not relevant

A typical threshold: > 0.7 to consider it relevant.


Next capsule: 07-capstone-exercise-3.md — Compute metrics, compare, decide.