Inside a vector database: indexes, filters and top-K
A vector database stores embeddings (lists of numbers that capture meaning) together with metadata such as the source, date and author. Its job is to find the items closest in meaning to a question in milliseconds, even among millions of items. It does that with a clever index, then returns the top-K best matches.
Everyday analogy
A huge library. Every book gets a shelf position by topic (the embedding), a label on the spine with its author and year (metadata), and a catalogue that takes you straight to the right aisle instead of walking past every shelf (the index).
Example
An HR chatbot gets the question "How many sick days do I get?". The vector database turns it into numbers, jumps through its index to the nearest policy chunks, keeps only documents tagged country = UAE and year = 2026, and returns the top 3 chunks. The LLM writes the answer from those 3 chunks.
What happens inside, step by step
- Embed: each chunk of text is turned into a vector by an embedding model.
- Store: the vector is saved with its metadata (id, text, source, date).
- Index: a special structure is built so the search doesn't have to compare against every vector.
- Search: the question is embedded too, and the nearest vectors are found using a similarity measure such as cosine similarity.
- Filter: metadata rules narrow the results (e.g. only HR documents, only this year).
- Return top-K: the K best matches, each with a score, go to the LLM (this is the R in RAG).
Three common index types
| Index | The idea in plain words | Trade-off |
|---|---|---|
| HNSW (Hierarchical Navigable Small World) | A graph of connected points with "express" layers on top, like taking a highway first and then local streets | Very fast and accurate; uses more memory |
| IVF (Inverted File) | Vectors are grouped into clusters; only the closest few clusters are searched | Fast and memory-friendly; can miss items in neighbouring clusters |
| PQ (Product Quantization) | Vectors are compressed into short codes so millions fit in memory | Saves a lot of space; slightly less accurate |
These are approximate nearest neighbour (ANN) methods: they trade a tiny bit of accuracy for a huge gain in speed. Checking every vector exactly is fine for a few thousand items, but too slow for millions.
Popular vector databases
- Pinecone: fully managed cloud service
- Weaviate, Milvus, Qdrant: open source, self-hosted or cloud
- Chroma: lightweight, popular for prototypes
- pgvector: adds vector search to the PostgreSQL database many companies already use
- FAISS: a library (not a full database) from Meta for fast similarity search
Two upgrades that make search better: hybrid search combines meaning-based search with old-fashioned keyword search (great for product codes and names), and a reranker re-scores the top results so the very best ones come first.
Try it yourself
About 15 minBe the vector database
- Write 8 short notes on cards, e.g. leave policy, sick days, laptop request, expense claims, and give each a tag (HR or IT) and a year.
- Sort the cards into piles by meaning (your "index").
- Answer the question How many sick days do I get in 2026?: go to the nearest pile, keep only HR cards from 2026, and pick the top 2.
What you should see: Grouping by meaning, then filtering by tags, finds the answer fast: exactly what HNSW-style indexes and metadata filters do.
Use any free AI chatbot (ChatGPT, Claude, Gemini or Copilot). Never paste private or confidential information.