Vector Embeddings and Semantic Search Fundamentals for Production Systems
Vector embeddings map text into high-dimensional numeric arrays where semantic similarity corresponds to geometric proximity. Unlike keyword indexes that require overlapping stems, dense embeddings allow a query like "cancel my subscription" to match a help article titled "Ending your recurring plan."
Similarity Metrics and Normalization
Most modern embedding models are trained with cosine similarity. When vectors are L2-normalized at index time, cosine similarity reduces to a dot product, which is faster to compute and directly compatible with inner-product vector indexes.
| Metric | Formula Intuition | Best Used When | Watch Out For |
|---|---|---|---|
| Cosine Similarity | Angle between vectors, ignoring magnitude | General text embeddings of varying length | Requires consistent normalization across index and query |
| Dot Product | Magnitude-weighted alignment | Normalized embeddings or MIPS models | Unnormalized vectors bias toward longer magnitudes |
| Euclidean (L2) | Straight-line distance in vector space | Normalized vectors (monotonic with cosine) | Unintuitive raw thresholds across different models |
Approximate Nearest Neighbor (ANN) Indexes
Exact k-NN scan compares the query vector against every stored row. Beyond tens of thousands of vectors, production systems use ANN indexes such as HNSW (Hierarchical Navigable Small World) or IVFFlat to trade a tiny amount of recall for sub-20ms query latency.
Tuning HNSW Parameters
mcontrols the maximum number of bidirectional links per node (highermimproves recall at the cost of memory and index build time).ef_constructioncontrols graph quality during index writes.ef_searchcontrols how many candidate neighbors are explored at query time and can often be tuned per query.
-- Example PostgreSQL pgvector HNSW index with cosine distance
CREATE INDEX IF NOT EXISTS kb_chunks_embedding_hnsw_idx
ON document_chunks
USING hnsw (embedding vector_cosine_ops)
WITH (m = 16, ef_construction = 64);
-- Always pair ANN retrieval with tenant isolation and iterative scan awareness
SET LOCAL hnsw.ef_search = 64;
Multi-Tenant Pre-Filtering Pitfall
One of the sharpest failure modes in vector databases is post-filtering by tenant ID. If an ANN index returns the global top 20 vectors across all tenants and only 1 of them belongs to the active tenant, recall collapses. Always verify that your vector engine supports filtered ANN scans, partitioned indexes, or iterative HNSW scans with a strict tenant predicate.
Key Production Rules
- Never mix embedding models in the same index: different models inhabit incompatible vector spaces. Store
embedding_modelandembedding_dimensionsmetadata alongside each vector. - Use task-specific prefixes when supported: asymmetric models distinguish
RETRIEVAL_DOCUMENTfromRETRIEVAL_QUERYembeddings, yielding measurable recall gains on short user questions. - Avoid hard-coded universal similarity cutoffs: cosine score distributions shift across languages, query lengths, and models. Calibrate thresholds against a labeled evaluation set rather than guessing
0.75.
Next: learn how document splitting shapes what embeddings can represent in Document Chunking Strategies for RAG.