フィルタリング検索の最適化
部分インデックスと複合インデックスの戦略を使い、ベクトル類似度とメタデータフィルターを組み合わせたpgvectorクエリを最適化します。
「フィルタリング検索の最適化」はCoddyKit上の無料Vector Databases: Pinecone, Weaviate & pgvectorレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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.
よくある質問
「フィルタリング検索の最適化」レッスンは無料ですか?
はい。「フィルタリング検索の最適化」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Vector Databases: Pinecone, Weaviate & pgvectorコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Vector Databases: Pinecone, Weaviate & pgvectorコースには全4レッスンが含まれています。
「フィルタリング検索の最適化」で何を学びますか?
部分インデックスと複合インデックスの戦略を使い、ベクトル類似度とメタデータフィルターを組み合わせたpgvectorクエリを最適化します。 ブラウザで直接実行するハンズオンコードでVector Databases: Pinecone, Weaviate & pgvectorを演習し、24時間対応の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フィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- 高速化のためのIVFFlatインデックス
- 再現率向上のためのHNSWインデックス
- クエリ性能のチューニング
- フィルタリング検索の最適化