FrontierAI.Engineer
← All chapters

Vector Databases & Retrieval

Embeddings, vector indexes, similarity search, and hybrid retrieval.

25 terms

Approximate Nearest NeighborApproximate nearest neighbor search returns vectors that are very likely to be the closest to a query vector without exhaustively comparing every entry in the index. ANN algorithms sacrifice a small, configurable amount of recall for dramatic speed gains — typically achieving millisecond query latency on billion-scale indexes. HNSW, IVF, and PQ are all ANN techniques. The recall-vs-latency tradeoff is tuned through index construction parameters.Binary QuantizationBinary quantization compresses each embedding dimension down to a single bit, shrinking an index dramatically and making distance comparisons very fast with bitwise operations. The tradeoff is precision loss, so it is usually paired with a rerank step that rescores top candidates using the full-precision vectors. This two-stage approach keeps recall high while cutting memory and latency at billion-vector scale.Cosine SimilarityCosine similarity measures the angle between two vectors, returning a score between −1 and 1. A score of 1 means the vectors point in the same direction; 0 means they are orthogonal. Because cosine similarity ignores magnitude and focuses on orientation, it is robust to differences in vector length caused by document verbosity. Most vector databases use cosine similarity or inner product as the default distance metric for semantic retrieval.DimensionalityDimensionality refers to the number of dimensions in an embedding vector — for example, 384, 768, or 3072. Higher dimensionality allows the embedding model to encode finer-grained semantic distinctions but increases memory, storage, and compute costs proportionally. Models trained with Matryoshka representation learning can produce embeddings that remain useful after truncation, allowing operators to trade representation richness against resource constraints without re-embedding the corpus.Dot ProductThe dot product of two vectors is the sum of the element-wise products of their components. When both vectors are unit-normalized, the dot product equals the cosine similarity. Many approximate nearest neighbor algorithms and neural retrieval systems prefer dot product because it is fast to compute and naturally integrates with inner-product-optimized hardware. Some embedding models are trained with a dot-product objective rather than cosine normalization, so it is important to match the metric to the training protocol.efSearchefSearch is an HNSW query-time parameter controlling how many candidate nodes the graph traversal keeps in its dynamic list. Higher values explore more of the graph, raising recall at the cost of latency; lower values are faster but may miss true neighbors. Tuning efSearch against a labeled set is the main lever for trading accuracy against speed at query time.Embedding DriftEmbedding drift occurs when the embedding model used to encode new documents or queries changes — either through model updates or model replacement — while older vectors in the index were produced by a different model version. Because the two model versions map content to different regions of vector space, mixed-model indexes produce degraded retrieval accuracy. Preventing embedding drift requires versioning the embedding model, storing the model identifier alongside each vector, and re-embedding the entire corpus whenever the model changes.Embedding ModelAn embedding model is a neural network trained to convert raw inputs into fixed-dimensional vectors. Sentence transformers, CLIP, and text-embedding-3-large are common examples. The model's training objective — typically contrastive loss — ensures that semantically related inputs cluster together in the output space. Choosing the right embedding model matters significantly: its output dimensionality, vocabulary coverage, and domain alignment all affect downstream retrieval accuracy.Euclidean DistanceEuclidean distance is the straight-line distance between two points in vector space, computed as the square root of the sum of squared coordinate differences. Unlike cosine similarity, Euclidean distance is sensitive to vector magnitude, so it can rank differently when vectors are not normalized. It is the default metric in some libraries such as FAISS and is appropriate when absolute magnitude differences carry meaningful information about similarity.Exact Nearest NeighborExact nearest neighbor search computes the distance between the query vector and every vector in the index, returning the true closest neighbors with 100% recall. It is computationally infeasible for large indexes — latency grows linearly with corpus size — but is useful for small datasets, offline evaluation, and as the recall benchmark against which ANN algorithms are measured. FAISS's IndexFlatL2 is a widely used flat-index implementation for exact search.Hierarchical Navigable Small WorldHNSW is a graph-based approximate nearest neighbor index that organizes vectors into a layered navigable small-world graph. A search starts at the top layer — which has few, widely spaced nodes — and greedily descends through layers to progressively closer neighbors in the base layer. HNSW delivers excellent recall-vs-latency tradeoffs and supports incremental inserts without full index rebuilds, making it the default algorithm in most production vector databases including Pinecone, Weaviate, and Qdrant.Hybrid IndexA hybrid index maintains both a dense vector index for semantic similarity search and a sparse inverted index for exact keyword matching within the same data store. Queries fan out to both indexes simultaneously, and results are merged — typically via reciprocal rank fusion or a learned fusion layer — before being returned to the caller. Hybrid indexes eliminate the need to maintain two separate systems and are the retrieval architecture of choice for production search applications that serve diverse query types.Index RebuildAn index rebuild is the process of recomputing the entire ANN index structure from the current set of vectors, typically scheduled periodically to keep the index optimally organized after many incremental inserts and deletes. IVF indexes in particular require rebuilds when cluster centroids drift from the actual data distribution. Rebuilds are compute-intensive and require careful orchestration — often maintaining a hot copy of the old index while the new one warms up — to avoid serving degraded results during the rebuild window.Inverted File IndexThe inverted file index partitions the vector space into a fixed number of clusters — computed via k-means — and assigns each vector to its nearest cluster centroid. At query time, only the vectors in the nearest few clusters are compared, sharply reducing the search scope. IVF is memory-efficient and scales well to large corpora, but it requires all vectors to be present at index build time and benefits from periodic rebuilds as data distributions shift.Metadata FilteringMetadata filtering restricts ANN search to vectors whose associated structured attributes — such as document type, date range, user ID, or category — satisfy a predicate before or after the vector similarity comparison. Pre-filtering narrows the search space before distance computation; post-filtering removes results that fail attribute checks after ANN search. Pre-filtering risks degrading ANN recall on small result sets, so vector databases implement hybrid strategies to balance accuracy and speed.NamespaceA namespace is a logical partition within a vector index that isolates a subset of vectors so queries are scoped to only that partition. Namespaces enable multi-tenant deployments where different users, projects, or data sources share a single vector database instance without cross-pollinating results. Pinecone popularized the term; other databases use collections or tenants for the same concept. Namespace-level isolation avoids the cost of spinning up separate indexes per tenant.Product QuantizationProduct quantization compresses high-dimensional vectors by splitting them into subvectors and replacing each subvector with the index of its nearest centroid in a learned codebook. The result is a compact code that enables fast approximate distance computation using lookup tables. PQ can compress 768-dimensional float32 vectors by 8–32× with modest recall loss, allowing billion-scale indexes to fit in RAM that would otherwise require terabytes of storage.Quantization (Vectors)Vector quantization is the process of representing floating-point embedding vectors with lower-precision or discrete codes to reduce memory footprint and speed up distance computation. Scalar quantization maps float32 values to int8; binary quantization maps to single bits. Both reduce storage dramatically — scalar quantization by 4× and binary by 32× — at the cost of some recall accuracy. Quantization is often combined with HNSW or IVF to serve large-scale indexes efficiently.Recall-vs-Latency TradeoffThe recall-vs-latency tradeoff describes the fundamental tension in ANN search: higher recall — returning more of the true nearest neighbors — requires examining more candidates, which increases query time. Index construction parameters such as HNSW's ef_construction and ef_search, or IVF's nprobe, control where on this curve a system operates. Production teams typically measure recall at the target latency percentile and tune parameters to meet a minimum recall threshold within a latency budget.Recall@kRecall@k measures the fraction of the true nearest neighbors that appear in the top k results an approximate index returns. It is the standard way to quantify how much accuracy an ANN index sacrifices for speed. Teams pick an acceptable Recall@k target, then tune index parameters like efSearch or the number of probes to meet it within the latency budget.ShardingSharding horizontally partitions a vector index across multiple machines so that each shard holds a subset of the total vectors. At query time, the search fan-out hits all shards in parallel and results are merged by a coordinator. Sharding enables a vector database to scale beyond what a single machine's RAM can hold and to increase query throughput proportionally with the number of shards. The shard count must be chosen carefully because re-sharding after initial deployment is costly.UpsertAn upsert operation inserts a vector into the index if its ID does not already exist or replaces the existing vector if it does. Upsert semantics are essential in dynamic corpora where source documents are frequently revised: rather than deleting and re-inserting, the application can send a single upsert call that keeps the index consistent. Most vector databases expose upsert as their primary write operation, and HNSW indexes support upsert without full rebuilds.Vector DatabaseA vector database is a purpose-built data store designed to persist, index, and query high-dimensional embedding vectors at scale. Beyond basic ANN search, vector databases typically support metadata filtering, namespacing, upsert semantics, and hybrid search combining dense and sparse signals. Commercial examples include Pinecone, Weaviate, Qdrant, and Chroma. Managed vector databases offload index management, scaling, and replication from the application team.Vector EmbeddingA vector embedding is a dense, fixed-length numerical representation of a piece of content — text, image, audio, or code — in a high-dimensional space. Embedding models learn to place semantically similar inputs near each other so that distance in the vector space correlates with conceptual similarity. Embeddings are the foundational artifact on which all vector retrieval is built: without them, similarity-based search is impossible.Vector IndexA vector index is the data structure that organizes embedding vectors so that nearest-neighbor queries can be executed efficiently. Flat indexes scan every vector exhaustively for maximum accuracy; ANN indexes such as HNSW and IVF trade a small recall penalty for orders-of-magnitude faster search. Index construction parameters — number of clusters, graph connectivity, and search-beam width — control the recall-vs-latency curve and must be tuned for each deployment's requirements.