Summary
Add support for prefiltering in SearchV to filter vectors during HNSW traversal rather than post-filtering results.
Problem
Currently, filtering vector search results requires post-filtering:
searched <- SearchV<MyType>(Embed(query), limit)
results <- searched::WHERE(_::{category}::EQ("electronics"))
This is inefficient because:
- HNSW retrieves
limit vectors first
- Filter is applied after, potentially returning fewer than
limit results
- For highly selective filters, most retrieved vectors are discarded
Proposed Solution
Add filter support directly to SearchV:
// Option A: WHERE clause syntax
results <- SearchV<MyType>(Embed(query), limit, WHERE category::EQ("electronics"))
// Option B: Filter function syntax
results <- SearchV<MyType>(Embed(query), limit, filter: _::{category}::EQ("electronics"))
Implementation Notes
The HNSW Rust code already supports filters:
fn search<F>(
...
filter: Option<&'arena [F]>,
) where F: Fn(&HVector<'arena>, &RoTxn<'db>) -> bool
Changes needed:
- Extend query DSL parser to accept filter expressions in
SearchV
- Modify query compiler to translate filter expressions to Rust closures
- Pass filters to HNSW
search function
Prefilter Considerations
Pure prefiltering can break HNSW's greedy navigation if filter nodes are skipped entirely. Two approaches:
- Filtered search with bridge nodes: Allow non-matching nodes during traversal but only return matching ones
- Over-fetch and filter: Retrieve
limit * multiplier candidates, filter, return top limit
Use Cases
- Search products by category
- Search documents by date range
- Search codes by chapter/level (ICD-10, etc.)
- Any schema with filterable properties
🤖 Generated with Claude Code
Summary
Add support for prefiltering in
SearchVto filter vectors during HNSW traversal rather than post-filtering results.Problem
Currently, filtering vector search results requires post-filtering:
This is inefficient because:
limitvectors firstlimitresultsProposed Solution
Add filter support directly to
SearchV:Implementation Notes
The HNSW Rust code already supports filters:
Changes needed:
SearchVsearchfunctionPrefilter Considerations
Pure prefiltering can break HNSW's greedy navigation if filter nodes are skipped entirely. Two approaches:
limit * multipliercandidates, filter, return toplimitUse Cases
🤖 Generated with Claude Code