▶ Sign up free for the video, slides & prompts
The lesson below is open to all. Enter your email once to unlock every video, slide deck, prompt and code example on this device.
Checking…
How LLMs Really Work · Lesson 9 of 15 · 2 min read · 5 min video
Vector databases and semantic search
Search by meaning, not keywords, and how vector indexes stay fast.
📄 Download slides (PDF)· free sign-upRAG needs a fast way to answer one question: which of my million chunks are closest in meaning to this query? That's the job of a vector database.
Keyword search vs semantic search
- Keyword search matches words. Search "cheap flights to Vizag" and you'll miss a page titled "Low-cost air tickets to Visakhapatnam"
- Semantic search compares embeddings, so it finds that page because the meaning matches
Keyword search is still great for exact things: product codes, names, error messages. That's why many systems use both (hybrid search).
What a vector database stores
For each chunk:
- The vector (its embedding)
- The original text
- Metadata: source file, page number, date, department, language, access level
Metadata lets you filter, for example "only HR documents updated this year that this user is allowed to see", before or during the similarity search.
Why it can't just compare everything
Comparing a query with every vector (brute force) is exact but slow at scale: a million vectors × 1,536 numbers per query adds up. So vector databases build an approximate nearest neighbour (ANN) index:
- HNSW: a layered graph. Start at a coarse level, hop towards closer neighbours, then refine at finer levels, like zooming in on a map
- IVF: group vectors into clusters, then search only the most promising clusters
You trade a tiny bit of accuracy for huge speed. Settings let you tune that trade-off.
Choosing a tool
- Already using PostgreSQL? The pgvector extension may be all you need
- Learning or prototyping: Chroma or FAISS run locally with a few lines of code
- Managed services: Pinecone, Weaviate, Qdrant, Milvus, and the vector search built into many cloud databases
For thousands or even hundreds of thousands of chunks, the simple option is usually fine.
Practical rules
- Use the same embedding model for documents and queries, and re-embed everything if you change it
- Store enough metadata to filter and cite
- Enforce permissions in the filter, never by trusting the model to hide things
- Evaluate retrieval on its own: for real questions, is the right chunk in the top 5?
In one line
A vector database stores embeddings with their text and metadata, and uses approximate nearest-neighbour indexes to find the closest meanings fast.
Share with a friend