Metadata Filtering and Hybrid Search
Harry
· 13 Sep 2026
· 1 views
Pre-Filter vs Post-Filter
Pre-filtering narrows the candidate set before the vector search runs, which keeps memory usage low and results consistent. Post-filtering runs the search first and drops rows afterwards, which wastes effort when the filter is selective.
Filtering in ChromaDB
results = movies.query(
query_texts=["ocean documentary"],
n_results=5,
where={"genre": {"$in": ["adventure", "nature"]}},
)Filtering in pgvector
SELECT title
FROM docs
WHERE category = 'ai'
ORDER BY embedding <=> '[0.01, 0.22, ...]'
LIMIT 5;Hybrid Search
Semantic search misses exact terms like product codes. Hybrid search combines vector ranking with keyword ranking (for example BM25). Merge the two ranked lists with reciprocal rank fusion so both signals contribute.
Simple Rank Fusion
def rrf_scores(ranks_a, ranks_b, k=60):
scores = {}
for rank, doc in enumerate(ranks_a + ranks_b):
scores[doc] = scores.get(doc, 0.0) + 1.0 / (k + rank + 1)
return sorted(scores, key=scores.get, reverse=True)Key Points
- Pre-filter before search; post-filter when filters are broad.
- Metadata queries combine naturally with vector search.
- Hybrid search adds keyword ranking to semantic ranking.
- RRF is a simple way to merge two ranked lists.