Base de Datos Vectorial
Índice vectorial · Búsqueda semántica
Escrito por Darjus Kazakevic · revisado el
Find items by meaning: store embeddings and search for the nearest ones.
An embedding model turns a piece of text, an image or a product into a vector: a list of a few hundred to a few thousand numbers. Items with similar meaning land close together, so "low-cost airfare" sits near "cheap flights" although they share no word. A vector database stores these vectors and answers one question fast: which stored vectors are nearest to this one? That is what "semantic search" means.
Comparing a query with every stored vector is exact, but too slow past a few million rows. So vector stores build an approximate nearest-neighbour (ANN) index, such as HNSW or IVF, that checks only a small part of the data. Dedicated products such as Qdrant, Milvus or Pinecone do this, and so do pgvector in Postgres and the vector fields in Elasticsearch and OpenSearch.
En esencia — A vector database finds the stored embeddings closest to a query embedding, using an approximate index that gives up a little recall for a lot of speed. Measure that recall, and pair it with keyword search.
Cuándo recurrir a él
Recurre a él cuando
- Users describe what they want in their own words, not in your exact terms
- Retrieval for RAG: finding the passages a language model should read
- Similar items: related products, duplicate tickets, near-identical images
Reconsidéralo cuando
- Exact lookups by id, SKU, email or error code. A normal index does that
- A few thousand vectors: an exact scan answers in milliseconds with no ANN index
- Search that must be complete, such as legal discovery, where a missed match is a failure
Conceptos clave
- Embedding
- A fixed-length vector produced by a model. The distance between two vectors stands for how different their meanings are. Vectors are only comparable when the same model made them.
- Distance metric (Cosine · Dot product · L2)
- How closeness is measured. Use the metric the model was trained for. When vectors are normalized to length 1, cosine, dot product and Euclidean distance all rank results in the same order.
- HNSW (Hierarchical Navigable Small World)
- A layered graph in which each vector links to its near neighbours. A search enters at the sparse top layer and walks greedily toward the query, layer by layer. It gives high recall at low latency, but the graph needs memory and is slow to build.
- IVF (Inverted file index)
- Group the vectors into clusters with k-means. A query compares itself with the cluster centres and searches only the closest few lists (nprobe). It builds faster and uses less memory than HNSW. Product quantization (IVF-PQ) shrinks it further at some cost in accuracy.
- Recall
- The share of the true nearest neighbours that an approximate search returns. A recall@10 of 0.95 means it finds, on average, 9.5 of the real top 10. You measure it by running an exact search on a sample of queries and comparing.
- Metadata filtering
- Restricting results by ordinary fields, such as tenant, language or date. Post-filtering takes the top k and then drops rows that fail the filter, so a selective filter can leave almost nothing. Filtering during the search needs support from the index.
- Hybrid search
- Run a keyword search (BM25) and a vector search, then merge the two ranked lists. Reciprocal rank fusion is the usual merge: each result scores 1 / (k + rank) in each list it appears in, with k around 60, and the scores add up.
Reglas de diseño
- Store the model with every vector
- Keep the model name and version beside each embedding. When you change models, build a new index next to the old one, re-embed everything into it, and switch reads over when it is full.
- Measure recall before you tune
- Take a few hundred real queries, find their true neighbours with an exact scan, and compare. Raise ef_search or nprobe until recall meets your target, then check that latency still does.
- Use hybrid search for anything people type
- Vectors match meaning and miss exact tokens: product codes, names, error numbers. A keyword search catches those. Merging both lists beats either alone on most real queries.
- Keep it derived from the database
- The source text lives in the database of record. The vectors are a derived copy you can rebuild, fed by the same outbox or change feed as any other index.
Errores habituales
- Mixing vectors from two embedding models in one index, so distances mean nothing and results look random.
- A selective filter applied after the ANN search, so a small tenant's query returns two results instead of ten.
- Relying on vectors alone, so a search for an exact product code returns similar-looking wrong codes.
- Building an IVF index on an almost empty table. Its clusters come from the rows present at build time, so recall drops as data arrives.