0Pricing
Vector Databases: Pinecone, Weaviate & pgvector · 课时

过滤搜索优化

使用部分索引和复合索引策略,优化同时结合向量相似度和元数据过滤条件的 pgvector 查询。

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

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

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.

常见问题解答

「过滤搜索优化」课时是免费的吗?

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

「过滤搜索优化」这节课中我会学到什么?

使用部分索引和复合索引策略,优化同时结合向量相似度和元数据过滤条件的 pgvector 查询。 你通过在浏览器中直接运行的动手代码来练习 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. 使用 IVFFlat 索引提升速度
  2. 使用 HNSW 索引提升召回率
  3. 查询性能调优
  4. 过滤搜索优化
← 返回 Vector Databases: Pinecone, Weaviate & pgvector