0Pricing
Vector Databases: Pinecone, Weaviate & pgvector · Lesson

Filtered Search Optimization

Optimize pgvector queries that combine vector similarity with metadata filters using partial and composite indexing strategies.

Filtered Search Optimization 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.

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.

Frequently asked questions

Is the “Filtered Search Optimization” lesson free?

Yes — the full text of “Filtered Search Optimization” 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 “Filtered Search Optimization”?

Optimize pgvector queries that combine vector similarity with metadata filters using partial and composite indexing strategies. 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 “Filtered Search Optimization” 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

  1. IVFFlat Indexing for Speed
  2. HNSW Indexing for Recall
  3. Query Performance Tuning
  4. Filtered Search Optimization
← Back to Vector Databases: Pinecone, Weaviate & pgvector