Vector DBs 🗄️🧠🔍
Learn how vector databases store text as coordinates in meaning space and find the nearest neighbors in milliseconds — the secret engine powering every serious AI system.
Day 92: Vector Databases — Store Meaning, Search by Similarity
Why Should I Care?
Ever notice how Spotify finds songs that feel exactly like the one you are playing — even if the titles are completely different? That is not keyword search. That is a vector database finding the nearest neighbors in musical meaning space. ChatGPT, Google, Netflix, and every serious AI product runs on this exact technology.
Core Concept
A vector database stores meaning as coordinates — called embeddings — and searches by distance, not by words. Think of it like a map. Every song, document, or product gets placed on that map based on what it means. When you search, your query also gets placed on the map. The database finds whatever is closest to you on that map. Those are your results. No word matching. Pure meaning matching.
How It Works
It works in two steps. Step 1 — every document gets converted into a vector (a list of numbers) by an embedding model. That vector is stored in the vector database. Step 2 — when a query arrives, it also gets converted into a vector using the same model. The database then calculates cosine similarity between the query vector and all stored vectors. The ones with the smallest angle — highest similarity score — are returned as results. This all happens in milliseconds, even across millions of records.
import numpy as np
def cosine_similarity(a, b):
return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))
# Step 1: Documents stored as vectors (embeddings)
docs = {
"Python basics course" : np.array([0.9, 0.1, 0.2]),
"AI Agent Bootcamp" : np.array([0.8, 0.3, 0.7]),
"SQL for beginners" : np.array([0.2, 0.9, 0.1]),
"Prompt engineering guide" : np.array([0.7, 0.2, 0.9])
}
# Step 2: Query arrives — converted to same vector space
query_vector = np.array([0.8, 0.2, 0.8])
scored = [(cosine_similarity(query_vector, vec), name)
for name, vec in docs.items()]
scored.sort(reverse=True)
for score, name in scored[:2]:
bar = "|||" * int(score * 20)
print(f"{score:.4f} {bar} -> {name}")Real World Connection
Spotify Song Radio is a live vector database demo. Every song is an embedding — its mood, energy, tempo, and vibe stored as coordinates. When you play a lo-fi study beat, Spotify converts it to a vector and finds the nearest neighbors in musical space. Chill Rainy Day and Focus Flow Ambient are close vectors. Pump Up Gym Track is far away. Vector DB plus a language model is exactly what makes ChatGPT able to answer questions about your own documents — that combination is called a RAG system, and it powers production AI search everywhere.
Examples
-- Vector Search Output Example
-- Query: "I want to learn about AI and prompts"
-- Query Vector: [0.8, 0.2, 0.8]
-- Results (sorted by similarity, highest first):
-- 0.9921 ||||||||||||||||||| -> Prompt engineering guide
-- 0.9654 |||||||||||||||||| -> AI Agent Bootcamp
-- 0.99 similarity means query and result
-- are nearly identical in meaning
-- Compare to keyword search for "AI course":
-- Keyword search: No exact match found
-- Vector search: Returns "Machine Learning Bootcamp"
-- because the MEANING matchesCommon Mistakes
Two mistakes kill most beginners with vector databases. First — using keyword search when you need meaning search. Second — storing raw text directly instead of converting it to embeddings first. Vector databases store numbers, not words. The embedding step is not optional.
-- WRONG: Using keyword search for meaning-based queries
SELECT * FROM courses WHERE title LIKE '%AI course%';
-- Returns: nothing (no exact match)
-- Misses: "Machine Learning Bootcamp", "Prompt Engineering Guide"
-- CORRECT: Convert to vector, search by distance
query_vec = embed_model.encode("AI course")
results = vector_db.search(query_vec, top_k=3)
-- Returns: closest meaning matches regardless of wording
-- WRONG: Storing raw text in a vector database
vector_db.insert("Python basics course") # raw text, wrong
-- CORRECT: Embed first, then store the vector
embedding = embed_model.encode("Python basics course")
vector_db.insert(embedding) # store numbers, not text
-- Rule: Embed first. Store vectors. Search by distance. Always.Mini Challenge
Mini Challenge
Build a tiny Spotify-style song recommender using Python and NumPy. Create 5 fake songs, each with a made-up 3-number vector representing mood, energy, and tempo. Pick one song as your "currently playing" query vector. Use cosine similarity to find the top 2 most similar songs. Print them with their scores. Bonus: can you make one song score above 0.95 similarity?
Quick Quiz
Q: What does a vector database store instead of raw text?
A: Embeddings — lists of numbers that represent the meaning of the text.
Q: What math formula measures how similar two vectors are?
A: Cosine similarity — it measures the angle between two vectors.
Q: What is a RAG system?
A: Vector DB plus a language model — retrieve similar documents, then generate an answer using them.
Bonus Knowledge
Real-world vector databases like Pinecone, Weaviate, Chroma, and Qdrant are built to handle millions of vectors at lightning speed. They use special algorithms like HNSW (Hierarchical Navigable Small World) to search massive vector spaces without checking every single record. This is why vector search returns results in milliseconds even with crores of stored embeddings. Every app you use that feels smart — PhonePe fraud detection, Amazon product recommendations, YouTube suggestions — has a vector database quietly working in the background.
Key Takeaways
Key Takeaways
- Vector databases store meaning as coordinates called embeddings — not raw text.
- Search works by finding nearest neighbors in meaning space, not by matching words.
- Cosine similarity measures the angle between two vectors — smaller angle means more similar.
- Always embed your text first using a model, then store the resulting vector.
- Always use the same embedding model for both documents and queries.
- Vector DB plus a language model equals a RAG system — the backbone of production AI.
- Spotify, Netflix, YouTube, and ChatGPT all rely on vector databases at their core.
Continue Learning with Rohi
You've used your 3 free Rohi questions. Create a free account to continue learning.