使用 pgvector 进行全文搜索与向量嵌入
在 Supabase 中使用 Postgres 全文搜索和 pgvector 扩展提供的语义相似度搜索,增强搜索功能。
使用 pgvector 进行全文搜索与向量嵌入 是 CoddyKit 上的免费 Supabase Backend as a Service 课时。 这是第 3 节课,共 3 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Supabase Backend as a Service 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Supabase Backend as a Service 课程共包含 3 节课。
本课时的部分内容尚未翻译,以英文显示。
Two Kinds of Search
Postgres supports full-text search for keyword matching and, via the pgvector extension, semantic search over AI embeddings, both inside Supabase.
Full-Text Search Basics
Postgres turns text into a searchable tsvector and queries it with a tsquery, handling stemming and ranking.
A Full-Text Query
Match documents whose body contains the search terms.
select id, title
from articles
where to_tsvector('english', body)
@@ plainto_tsquery('english', 'database scaling');Indexing for Speed
A GIN index on the tsvector makes full-text search fast at scale.
create index articles_fts
on articles
using gin (to_tsvector('english', body));What Are Embeddings?
An embedding is a list of numbers (a vector) that captures meaning. Similar texts have vectors that are close together, enabling search by concept, not just keywords.
Enabling pgvector
Turn on the extension, then add a vector column sized to your model's dimensions.
create extension if not exists vector;
alter table docs add column embedding vector(1536);Storing an Embedding
Generate the vector with an embedding model, then insert it alongside the text.
const { error } = await supabase
.from('docs')
.insert({ content: text, embedding: vectorArray });Similarity Search
Find the nearest vectors using a distance operator. <=> is cosine distance.
select content
from docs
order by embedding <=> query_embedding
limit 5;Indexing Vectors
An HNSW or IVFFlat index speeds up nearest-neighbor search on large vector tables.
create index on docs
using hnsw (embedding vector_cosine_ops);Choosing the Right Approach
Use full-text search for exact keyword needs, and vector search for meaning-based retrieval like Q&A or recommendations. Many apps combine both (hybrid search).
function pickSearch(needsMeaning) {
return needsMeaning ? 'vector' : 'full-text';
}
console.log(pickSearch(true));Putting It Together
Postgres gives you both keyword and semantic search natively. With GIN indexes for text and HNSW indexes for vectors, search stays fast as data grows.
Quick Check
Test your understanding of search extensions.
Recap
You learned full-text search with tsvector and GIN indexes, plus semantic search with pgvector, vector columns, distance operators, and HNSW indexes, and when to use each or combine them.
常见问题解答
「使用 pgvector 进行全文搜索与向量嵌入」课时是免费的吗?
是的 — 「使用 pgvector 进行全文搜索与向量嵌入」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Supabase Backend as a Service 课程的其余内容,请升级到 CoddyKit PRO。 Supabase Backend as a Service 课程共包含 3 节课。
「使用 pgvector 进行全文搜索与向量嵌入」这节课中我会学到什么?
在 Supabase 中使用 Postgres 全文搜索和 pgvector 扩展提供的语义相似度搜索,增强搜索功能。 你通过在浏览器中直接运行的动手代码来练习 Supabase Backend as a Service,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Supabase Backend as a Service 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Supabase Backend as a Service 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 3 节。
「使用 pgvector 进行全文搜索与向量嵌入」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Supabase Backend as a Service 课中编写并运行代码吗?
能。每节 Supabase Backend as a Service 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 使用 PostgreSQL 扩展
- 高级地理空间数据(PostGIS)
- 使用 pgvector 进行全文搜索与向量嵌入