Distance Metrics and Indexing Basics
Learn the distance metrics that define vector similarity and the approximate nearest neighbor indexes that make vector search fast at scale.
Distance Metrics and Indexing Basics is a free Vector Databases: Pinecone, Weaviate & pgvector lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Vector Databases: Pinecone, Weaviate & pgvector learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Measuring Closeness
A vector DB finds vectors close to your query, but 'close' is defined by a distance metric — and the metric you pick shapes what counts as similar.
Euclidean Distance
Euclidean (L2) distance is the straight-line distance between two points: smaller means closer. Run the example to see it.
import math
def l2(a, b):
return math.sqrt(sum((x-y)**2 for x, y in zip(a, b)))
print(round(l2([0,0],[3,4]), 1))Cosine Similarity
Cosine similarity measures the angle between vectors, ignoring magnitude. It's the go-to for text embeddings, since direction carries the meaning.
import math
def cosine(a, b):
dot = sum(x*y for x, y in zip(a, b))
na = math.sqrt(sum(x*x for x in a))
nb = math.sqrt(sum(y*y for y in b))
return dot / (na*nb)
print(round(cosine([1,0],[1,1]), 3))Dot Product
The dot product blends angle and magnitude. With normalized vectors it equals cosine similarity — which is why many systems normalize, then use dot product for speed.
Choosing a Metric
Match the metric to what your model was trained for: text usually cosine, normalized vectors dot product, some image or geo data Euclidean. A mismatch silently hurts results.
The Brute-Force Problem
Brute force — comparing the query to every stored vector — is exact but slow: millions of vectors mean millions of comparisons per query. It doesn't scale.
Approximate Nearest Neighbor
ANN indexes trade a sliver of accuracy for massive speed by cleverly skipping most candidates. Recall stays high while latency drops by orders of magnitude.
HNSW Indexes
HNSW builds a layered graph you navigate coarse to fine for excellent recall and speed — the default in many vector DBs, tunable via ef and M.
IVF Indexes
IVF clusters vectors into buckets and only searches the ones nearest your query. Fewer probes mean faster search, at a small accuracy cost.
The Recall-Speed Trade-off
Every ANN index exposes knobs that trade recall for speed. Searching more candidates raises both recall and latency. Tune to your accuracy target, then push speed.
Putting It Together
Putting it together: pick the right distance metric for your embeddings, then use an ANN index (HNSW or IVF) to query fast at scale, tuning the recall-speed knobs.
Quick Check
Test your understanding of metrics and indexes.
Recap
Recap: the core distance metrics (Euclidean, cosine, dot product) — text usually cosine — plus ANN indexes like HNSW and IVF that trade a little recall for big speed.
Frequently asked questions
Is the “Distance Metrics and Indexing Basics” lesson free?
Yes — the full text of “Distance Metrics and Indexing Basics” is free to read here on the web, and the Vector Databases: Pinecone, Weaviate & pgvector course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Vector Databases: Pinecone, Weaviate & pgvector course, upgrade to CoddyKit PRO.
What will I learn in “Distance Metrics and Indexing Basics”?
Learn the distance metrics that define vector similarity and the approximate nearest neighbor indexes that make vector search fast at scale. You practise Vector Databases: Pinecone, Weaviate & pgvector with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Vector Databases: Pinecone, Weaviate & pgvector?
No prior experience is required. Vector Databases: Pinecone, Weaviate & pgvector on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Distance Metrics and Indexing Basics” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Vector Databases: Pinecone, Weaviate & pgvector lesson?
Yes. Every Vector Databases: Pinecone, Weaviate & pgvector lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- What are Vector Databases?
- Embeddings: The Core Concept
- Similarity Search Explained
- Distance Metrics and Indexing Basics