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.

📄Chunkstext +metadata🔢Embedturn intovectors🗂️IndexHNSW / IVF /PQ🔍Searchnearest bymeaning🏷️Filtere.g. HR, 2026🤖Top-K toLLManswer withsources
A vector database stores meaning as numbers, indexes them for speed and returns the top-K matches.

What happens inside, step by step

  1. Embed: each chunk of text is turned into a vector by an embedding model.
  2. Store: the vector is saved with its metadata (id, text, source, date).
  3. Index: a special structure is built so the search doesn't have to compare against every vector.
  4. Search: the question is embedded too, and the nearest vectors are found using a similarity measure such as cosine similarity.
  5. Filter: metadata rules narrow the results (e.g. only HR documents, only this year).
  6. 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

IndexThe idea in plain wordsTrade-off
HNSW (Hierarchical Navigable Small World)A graph of connected points with "express" layers on top, like taking a highway first and then local streetsVery fast and accurate; uses more memory
IVF (Inverted File)Vectors are grouped into clusters; only the closest few clusters are searchedFast and memory-friendly; can miss items in neighbouring clusters
PQ (Product Quantization)Vectors are compressed into short codes so millions fit in memorySaves 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 min

Be the vector database

  1. 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.
  2. Sort the cards into piles by meaning (your "index").
  3. 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.

Build it for real · guided projectBuild a document Q&A assistant (RAG, no code)About 60 min · No coding needed · step by step
Test what you learned

Take a quick quiz, or look up any word in the glossary.