Cloud Tech by Victor
BackendIntermediate

Embeddings & Vector Search

How embeddings turn text into vectors that cosine similarity can compare mathematically, why shortening an embedding's dimensions trades some accuracy for real storage and speed savings, and when semantic search actually beats keyword search.

Updated 2026-07-243 min read

Overview

An embedding maps a piece of text to a vector of floating-point numbers positioned in a high-dimensional space such that semantically similar texts end up close together, and cosine similarity is the standard way to measure that closeness mathematically. This is what powers semantic search, clustering, recommendations, and classification: comparison happens on meaning rather than literal word overlap, so a query and a relevant document can match even when they share no words at all. Embedding dimensionality is a real, tunable trade-off, modern models support shortening a full-length embedding to fewer dimensions for less storage and faster comparison, at some cost to precision, and the right point on that trade-off depends entirely on how much accuracy a specific use case can afford to give up.

Quick Reference

ConceptWhat it is
EmbeddingA vector representing a text's meaning numerically
Cosine similarityA similarity score between two embeddings; higher similarity (closer to 1) = more related
DimensionalityVector length; fewer dimensions = less storage/compute, some accuracy cost
Semantic searchRanking results by embedding similarity, not literal keyword overlap
Use caseWhat embeddings enable
Semantic searchRank documents by meaning-relatedness to a query
ClusteringGroup related texts automatically, no manual labels
RecommendationsSuggest items with similar textual descriptions
ClassificationAssign a category based on embedding proximity to labeled examples

Syntax

python
response = client.embeddings.create(
    model="text-embedding-3-large",
    input="The user's password reset email never arrived.",
)
vector = response.data[0].embedding

Examples

python
# Cosine similarity between two embeddings - closer to 1 means
# more semantically related, regardless of shared vocabulary.
import numpy as np

def cosine_similarity(a, b):
    return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))

score = cosine_similarity(query_embedding, document_embedding)

Shortened embeddings can still beat an older full-length model

A newer embedding model shortened to fewer dimensions can outperform an older model's unshortened embedding. Dimension count is a real trade-off against a specific model's own accuracy ceiling, not a simple "smaller is always worse" rule.

Visual Diagram

Common Mistakes

  • Expecting embeddings to handle exact-match needs (error codes, SKUs, proper nouns) as well as keyword search, when literal string matching is what those actually require.
  • Storing full-dimensional embeddings everywhere by default, without evaluating whether a shortened dimension count would meet the use case's accuracy bar at meaningfully lower storage/compute cost.
  • Comparing embeddings produced by two different models, or two different dimension settings, and expecting the distances to be meaningfully comparable.
  • Treating semantic search as a strict replacement for keyword search instead of combining both (hybrid search) where each is actually stronger.

Performance

  • Similarity computation cost scales with both the number of stored vectors and their dimensionality; shortening dimensions is a direct, deliberate lever on query latency and storage at scale.
  • Cosine similarity is computationally cheap, effectively a dot product for normalized vectors, so the practical bottleneck at scale is usually the vector index/search structure, not the similarity math itself.

Best Practices

  • Use cosine similarity as the default distance metric for comparing embeddings unless a specific model's documentation recommends otherwise.
  • Evaluate a shortened embedding dimension against the actual accuracy bar for the use case before defaulting to full length everywhere.
  • Combine semantic (embedding) search with keyword search for queries that need both meaning-matching and exact-match precision.
  • Never compare embeddings generated by different models or different dimension settings as if their distances were on the same scale.

Interview questions

What does an embedding actually represent, and what does the distance between two embeddings measure?

An embedding is a vector, a list of floating-point numbers, produced by a model that maps a piece of text into that vector space such that texts with similar meaning end up positioned close together. The distance between two embeddings, typically measured with cosine similarity, quantifies how related the two original texts are: a small distance (high cosine similarity) means the model judged them semantically close, even if they don't share any of the same words, and a large distance means they're unrelated. This is the property that makes embeddings useful for search and clustering: comparison happens on meaning, encoded numerically, rather than on literal text overlap.

Why would you shorten an embedding from 1536 dimensions to 256, and what do you give up by doing it?

Fewer dimensions means less storage per vector and faster similarity computation at query time, which matters directly at scale, a vector database holding millions of embeddings pays that storage and compute cost on every one of them. Modern embedding models are specifically trained to support this trade-off gracefully: a newer model shortened to 256 dimensions can still outperform an older, unshortened model at 1536 dimensions, so shortening isn't simply "less accurate," it's a deliberate trade against a specific model's own accuracy ceiling. What you give up is some of that ceiling, the shortened embedding is measurably less precise than the same model's full-length embedding, which is why the right dimension count depends on how much accuracy a specific use case can actually trade away.

When does semantic search (via embeddings) actually outperform traditional keyword search, and when might keyword search still win?

Semantic search wins when a query and the relevant document use different words for the same idea, "car won't start" matching a document about "vehicle fails to ignite", since embeddings compare meaning rather than literal tokens, which keyword search cannot do at all. Keyword search still wins, or at least remains necessary, for exact-match needs, an error code, a product SKU, a specific proper noun, where the literal string matters and a semantically "close" but textually different result is actually the wrong answer. This is why production search systems commonly combine both (hybrid search) rather than treating embeddings as a strict replacement for keyword matching.

References

ShareXLinkedInReddit
Was this page helpful?
Suggest an improvement