К содержанию
Статья ещё не опубликована на языке Russian. Показана версия на English.

Vector Embeddings and Semantic Search Fundamentals for Production Systems

3 мин на чтение

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.

MetricFormula IntuitionBest Used WhenWatch Out For
Cosine SimilarityAngle between vectors, ignoring magnitudeGeneral text embeddings of varying lengthRequires consistent normalization across index and query
Dot ProductMagnitude-weighted alignmentNormalized embeddings or MIPS modelsUnnormalized vectors bias toward longer magnitudes
Euclidean (L2)Straight-line distance in vector spaceNormalized 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

  • m controls the maximum number of bidirectional links per node (higher m improves recall at the cost of memory and index build time).
  • ef_construction controls graph quality during index writes.
  • ef_search controls how many candidate neighbors are explored at query time and can often be tuned per query.
sql
-- 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

  1. Never mix embedding models in the same index: different models inhabit incompatible vector spaces. Store embedding_model and embedding_dimensions metadata alongside each vector.
  2. Use task-specific prefixes when supported: asymmetric models distinguish RETRIEVAL_DOCUMENT from RETRIEVAL_QUERY embeddings, yielding measurable recall gains on short user questions.
  3. 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.

Было ли это полезно?