Recherche en texte intégral et représentations vectorielles avec pgvector
Améliorez les fonctionnalités de recherche grâce à la recherche en texte intégral de Postgres et à la recherche de similarité sémantique avec l’extension pgvector dans Supabase.
Recherche en texte intégral et représentations vectorielles avec pgvector est une leçon Supabase Backend as a Service gratuite sur CoddyKit. Ceci est la leçon 3 sur 3. Tu peux lire la leçon complète ci-dessous gratuitement — puis la pratiquer en direct dans le navigateur avec un éditeur de code intégré et un tuteur IA 24/7. Elle fait partie du parcours d'apprentissage Supabase Backend as a Service, et ta progression se synchronise sur le web et l'application CoddyKit. Le cours Supabase Backend as a Service comprend 3 leçons au total.
Certaines parties de cette leçon n'ont pas encore été traduites et s'affichent en anglais.
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.
Questions Fréquemment Posées
La leçon « Recherche en texte intégral et représentations vectorielles avec pgvector » est-elle gratuite ?
Oui — le texte complet de « Recherche en texte intégral et représentations vectorielles avec pgvector » est gratuit à lire ici sur le web. Pour la pratiquer de manière interactive (un éditeur de code intégré et un tuteur IA 24/7) et déverrouiller le reste du cours Supabase Backend as a Service, passe à CoddyKit PRO. Le cours Supabase Backend as a Service comprend 3 leçons au total.
Qu'est-ce que j'apprendrai dans « Recherche en texte intégral et représentations vectorielles avec pgvector » ?
Améliorez les fonctionnalités de recherche grâce à la recherche en texte intégral de Postgres et à la recherche de similarité sémantique avec l’extension pgvector dans Supabase. Tu pratiques Supabase Backend as a Service avec du code pratique que tu exécutes directement dans le navigateur, et un tuteur IA 24/7 répond à tes questions au fur et à mesure que tu avances dans la leçon.
Dois-je avoir de l'expérience pour commencer Supabase Backend as a Service ?
Aucune expérience préalable n'est requise. Supabase Backend as a Service sur CoddyKit est structuré pour les débutants jusqu'aux apprenants avancés, donc tu peux commencer ici ou depuis le début et avancer à ton rythme. Ceci est la leçon 3 sur 3.
Combien de temps prend la leçon « Recherche en texte intégral et représentations vectorielles avec pgvector » ?
La plupart des leçons CoddyKit prennent environ 5–10 minutes. Chacune est courte et interactive, tu progresses régulièrement et tu repiques exactement où tu t'es arrêté sur le web et l'app.
Peux-tu écrire et exécuter du code dans cette leçon Supabase Backend as a Service ?
Oui. Chaque leçon Supabase Backend as a Service inclut un éditeur de code intégré, tu écris et exécutes du vrai code directement dans ton navigateur et tu reçois des retours IA instantanés — aucune configuration locale requise.
Toutes les leçons de ce cours
- Utiliser les extensions de PostgreSQL
- Données géospatiales avancées (PostGIS)
- Recherche en texte intégral et représentations vectorielles avec pgvector