Ottimizzare la ricerca filtrata
Ottimizzate le query pgvector che combinano similarità vettoriale e filtri sui metadati usando strategie di indicizzazione parziale e composita.
Ottimizzare la ricerca filtrata è una lezione Vector Databases: Pinecone, Weaviate & pgvector gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Vector Databases: Pinecone, Weaviate & pgvector, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Vector Databases: Pinecone, Weaviate & pgvector include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
The Filtered Search Problem
Real queries often combine a vector search with a metadata WHERE filter. Naive combinations can defeat the vector index and become slow.
SELECT id FROM docs
WHERE category = 'news'
ORDER BY embedding <=> '[...]'::vector
LIMIT 10;Why Filters Hurt Recall
ANN indexes return approximate nearest neighbors before the filter applies. If most candidates are filtered out, you may get fewer than LIMIT results — this is over-filtering.
Increasing Search Scope
Raise hnsw.ef_search (or IVFFlat probes) so the index scans more candidates before filtering, improving recall on selective filters.
SET hnsw.ef_search = 100;Partial Indexes
For a common, low-cardinality filter value, build a partial index covering only those rows. The index then perfectly matches the filtered subset.
CREATE INDEX ON docs
USING hnsw (embedding vector_cosine_ops)
WHERE category = 'news';When Partial Indexes Win
Partial indexes shine when:
- Filter values are few and known
- Each subset is large enough to matter
- Queries almost always include that filter
B-tree Support Indexes
For high-cardinality filters, add a regular B-tree index on the metadata column. The planner can combine it with the vector scan.
CREATE INDEX ON docs (category);
CREATE INDEX ON docs (published_at);Iterative Index Scans
Newer pgvector supports iterative scans, which keep fetching from the index until enough rows pass the filter. Enable it to avoid under-filling results.
SET hnsw.iterative_scan = 'relaxed_order';Pre-filtering vs Post-filtering
Pre-filter: narrow rows first, then vector search (good for very selective filters). Post-filter: vector search first, then filter (good for broad filters). Test both.
Measuring with EXPLAIN
Always confirm the plan. Look for Index Scan using ... hnsw rather than a sequential scan.
EXPLAIN ANALYZE
SELECT id FROM docs
WHERE category = 'news'
ORDER BY embedding <=> '[...]'::vector
LIMIT 10;Combining Strategies
A robust setup often uses partial indexes for hot filters, B-tree indexes for the rest, and a tuned ef_search with iterative scans as a safety net.
Checklist
Before shipping a filtered vector query:
- Verify index usage with EXPLAIN
- Confirm you reliably get LIMIT results
- Benchmark latency at p95
Quick Check
Test filtered search knowledge.
Recap
You learned why filters challenge ANN indexes and how partial indexes, B-tree support indexes, ef_search tuning, and iterative scans keep filtered vector queries both fast and accurate.
Domande Frequenti
La lezione «Ottimizzare la ricerca filtrata» è gratuita?
Sì — il testo completo di «Ottimizzare la ricerca filtrata» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Vector Databases: Pinecone, Weaviate & pgvector, passa a CoddyKit PRO. Il corso Vector Databases: Pinecone, Weaviate & pgvector include 4 lezioni in totale.
Cosa imparerò in «Ottimizzare la ricerca filtrata»?
Ottimizzate le query pgvector che combinano similarità vettoriale e filtri sui metadati usando strategie di indicizzazione parziale e composita. Eserciti Vector Databases: Pinecone, Weaviate & pgvector con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Vector Databases: Pinecone, Weaviate & pgvector?
Non è richiesta alcuna esperienza precedente. Vector Databases: Pinecone, Weaviate & pgvector su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.
Quanto tempo richiede la lezione «Ottimizzare la ricerca filtrata»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Vector Databases: Pinecone, Weaviate & pgvector?
Sì. Ogni lezione Vector Databases: Pinecone, Weaviate & pgvector include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Indicizzazione IVFFlat per la velocità
- Indicizzazione HNSW per il recall
- Ottimizzare le prestazioni delle query
- Ottimizzare la ricerca filtrata