Vector Databases: Pinecone, Weaviate & pgvector · 课时

距离指标与索引基础

学习定义向量相似度的距离指标,以及让向量搜索在大规模场景下保持快速的近似最近邻索引。

第 4 / 4 课13 个步骤

距离指标与索引基础 是 CoddyKit 上的免费 Vector Databases: Pinecone, Weaviate & pgvector 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Vector Databases: Pinecone, Weaviate & pgvector 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Vector Databases: Pinecone, Weaviate & pgvector 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

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.

免费开始

用 AI 导师学习 Vector Databases: Pinecone, Weaviate & pgvector — 免费

在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。

课程
12
课程
48

常见问题解答

「距离指标与索引基础」课时是免费的吗?

是的 — 「距离指标与索引基础」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Vector Databases: Pinecone, Weaviate & pgvector 课程的其余内容,请升级到 CoddyKit PRO。 Vector Databases: Pinecone, Weaviate & pgvector 课程共包含 4 节课。

「距离指标与索引基础」这节课中我会学到什么?

学习定义向量相似度的距离指标,以及让向量搜索在大规模场景下保持快速的近似最近邻索引。 你通过在浏览器中直接运行的动手代码来练习 Vector Databases: Pinecone, Weaviate & pgvector,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Vector Databases: Pinecone, Weaviate & pgvector 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Vector Databases: Pinecone, Weaviate & pgvector 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「距离指标与索引基础」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Vector Databases: Pinecone, Weaviate & pgvector 课中编写并运行代码吗?

能。每节 Vector Databases: Pinecone, Weaviate & pgvector 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 什么是向量数据库
  2. 嵌入:核心概念
  3. 详解相似度搜索
  4. 距离指标与索引基础
← 返回 Vector Databases: Pinecone, Weaviate & pgvector