Vector Similarity Search
Similarity search is the core operation of a vector database. You provide a query vector, and the database returns the vectors most similar to it — along with their original data. This topic explains exactly how that works.
The Neighborhood Analogy
Imagine dropping a pin on a map and asking "what restaurants are within 1 kilometer of here?" The map doesn't care about restaurant names — it measures physical distance and returns everything nearby.
Similarity search does the same thing, but in meaning space. Drop a query vector and ask "what stored vectors are mathematically closest to this?" The database measures the distance between vectors and returns the nearest ones.
QUERY: "affordable smartphone"
VECTOR SPACE (simplified 2D)
─────────────────────────────────────
● "budget phone deals" ← VERY CLOSE (returned)
● "cheap Android handset" ← CLOSE (returned)
● Query Vector
● "low cost mobile" ← CLOSE (returned)
● "laptop computers" ← FAR (not returned)
● "car insurance" ← FAR (not returned)
Exact vs. Approximate Nearest Neighbor Search
Two approaches exist for finding similar vectors:
Exact Nearest Neighbor (KNN)
The database compares your query vector against every single stored vector and ranks them by distance. This guarantees the most accurate results but gets very slow with large datasets.
1 million vectors × compare each one = slow (seconds to minutes)
Approximate Nearest Neighbor (ANN)
The database uses smart shortcuts (indexes) to skip most vectors and only check the likely candidates. Results are 95–99% accurate but arrive in milliseconds.
1 million vectors → index narrows to 10,000 candidates → compare those = fast (milliseconds)
Production vector databases almost always use ANN search. The small accuracy trade-off is worth the enormous speed gain.
The k Parameter
When you run a similarity search, you specify k — the number of results to return. This is called a k-Nearest Neighbors (kNN) query.
| Use Case | Typical k Value | Why |
|---|---|---|
| Semantic search | 5–10 | Show top matching results |
| Recommendations | 10–50 | Offer a variety of options |
| RAG (AI context) | 3–5 | Feed focused context to AI |
| Duplicate detection | 1–3 | Find near-identical items |
How a Search Query Flows
STEP 1: User Input "Show me hiking boots under $100" STEP 2: Embed the Query Embedding Model → [0.34, 0.87, 0.12, 0.65, ...] STEP 3: Search the Vector Database Query vector compared against stored product vectors STEP 4: Rank by Similarity Product A: distance = 0.12 ← very similar Product B: distance = 0.18 ← similar Product C: distance = 0.31 ← somewhat similar Product D: distance = 0.92 ← not similar STEP 5: Return Top k Results k=3 → returns Products A, B, C with their data
Similarity Score vs. Distance
Different databases report similarity in different ways. Knowing which format your database uses prevents confusion.
| Format | Perfect Match | No Match | Example System |
|---|---|---|---|
| Distance (lower = better) | 0.0 | 2.0+ | Euclidean, Manhattan |
| Score (higher = better) | 1.0 | 0.0 | Cosine similarity |
What Makes Similarity Search Powerful
Traditional keyword search fails when users phrase things differently. A user searching "affordable footwear for hiking" finds nothing if the database only has "budget trekking boots." Similarity search handles this naturally because both phrases produce similar vectors.
- Handles synonyms automatically
- Works across languages (with multilingual embedding models)
- Finds conceptually related items, not just word matches
- Scales to billions of vectors with the right index
Similarity search is the engine that powers modern semantic search, AI assistants, recommendation systems, and image recognition. Every topic in this course builds on this core concept.
