What is a vector database?
A vector database is a system designed to store and search embeddings—numeric vectors (often hundreds to thousands of dimensions) that represent the meaning of text, images, audio, or other data. Instead of asking “does this record match exactly?”, vector search asks “which records are most similar to this query?” by comparing distances between vectors.
Embeddings are typically produced by machine-learning models (for example, OpenAI, Cohere, or open-source sentence transformers). If two pieces of content have similar meaning, their vectors tend to be close together in the embedding space.
Why not use a normal database?
Traditional databases excel at structured queries (filters, joins, exact matches). They struggle with semantic queries like “find documents about returning a product” when the text might say “refund” or “exchange.” Vector databases are optimized for this kind of fuzzy, meaning-based retrieval.
Keyword search finds literal terms; vector search finds similar meaning.
Vector indexes (e.g., HNSW, IVF) are built to search millions of vectors quickly.
Most vector DBs still support metadata filtering (e.g.,
category = 'support'and then do similarity search within that slice).
Core concepts (in plain language)
Embedding: a vector representation of an item (document, chunk, image).
Distance / similarity: how close two vectors are (cosine similarity and Euclidean distance are common).
Index: a data structure that makes nearest-neighbor search fast.
Top‑k: “return the k most similar items.”
Metadata: structured fields stored alongside vectors (ids, timestamps, tags).
Chunking: splitting long documents into smaller passages so retrieval is more precise.
Where vector databases show up (especially with LLMs)
A popular pattern is retrieval‑augmented generation (RAG): you store embeddings of your knowledge base in a vector DB, retrieve the most relevant chunks for a user question, then pass those chunks to a large language model as context. This reduces hallucinations and lets the model answer using your private or up-to-date data.
Internal search (“find the most relevant policy page”).
Support chatbots grounded in your docs.
Recommendation systems (“users who viewed similar items…”).
De-duplication and clustering of similar content.
Image and audio similarity search.
A minimal end-to-end code example (Python)
Below is a small example using FAISS (an in-process vector index). In production you might use a managed vector database, but the workflow is the same: embed → store/index → query → retrieve top‑k.
# pip install faiss-cpu numpy
import numpy as np
import faiss
# 1) Your "documents" (often you'd chunk long text first)
docs = [
"How to reset your password",
"Refund policy for returned items",
"Troubleshooting login issues",
"How to change your email address",
]
# 2) Pretend we already have embeddings for each doc.
# In real life, call an embedding model to produce these vectors.
# We'll use random vectors just to demonstrate the mechanics.
np.random.seed(0)
dim = 128
doc_vectors = np.random.normal(size=(len(docs), dim)).astype("float32")
# 3) Build an index (L2 = Euclidean distance)
index = faiss.IndexFlatL2(dim)
index.add(doc_vectors) # store vectors in the index
# 4) Embed a query the same way as the docs
query = "I can't sign in to my account"
query_vector = np.random.normal(size=(1, dim)).astype("float32")
# 5) Search for the top-k most similar vectors
k = 2
distances, indices = index.search(query_vector, k)
print("Query:", query)
print("Top matches:")
for rank, i in enumerate(indices[0], start=1):
print(f"{rank}. {docs[i]} (distance={distances[0][rank-1]:.3f})")
To make this meaningful, replace the random vectors with real embeddings (for example, using a sentence embedding model). The key rule is: documents and queries must be embedded with the same model and preprocessing.
Practical tips and common pitfalls
Choose your similarity metric carefully: many text embeddings work well with cosine similarity; some indexes convert cosine to inner product.
Chunk size matters: too big and retrieval gets vague; too small and you lose context. Start with ~200–500 tokens and iterate.
Store metadata: keep
doc_id,source,timestamp, and permissions so you can filter and audit results.Evaluate retrieval: measure whether the right passages appear in top‑k before blaming the LLM.
Plan for updates: if content changes often, understand how your system handles re-embedding and re-indexing.
Key takeaways: Vector databases enable fast semantic search over embeddings. The standard workflow is embed → index/store → similarity search → retrieve top‑k (optionally with metadata filters). They’re especially useful for RAG systems that ground LLM responses in your own documents, but quality depends heavily on good embeddings, sensible chunking, and retrieval evaluation.