Overview
TheFilter trait defines how candidates are evaluated and partitioned into kept and removed sets. Filters run sequentially and reduce the candidate pool based on various criteria.
Trait Definition
Type Parameters
The query type that contains request context and parametersConstraints:
Clone + Send + Sync + 'staticThe candidate type to filterConstraints:
Clone + Send + Sync + 'staticFilterResult
Candidates that passed the filter criteria and continue to the next stage
Candidates that were filtered out and are excluded from further processing
Methods
enable
filter
Evaluates candidates and partitions them into kept and removed sets
Reference to the query object containing filtering parameters
Vector of candidates to evaluate (takes ownership)
Returns a
FilterResult with kept and removed candidates, or an error message on failurename
Returns a stable name for logging and metrics
A short type name derived from the implementing struct
Example Implementation
Here’s a real example that filters tweets based on their age:Usage Notes
- Filters run sequentially in the order they are configured
- Each filter receives the
keptcandidates from the previous filter - The
removedcandidates can be tracked for metrics and debugging - Filters take ownership of the candidate vector for efficient partitioning
- Use
partitionfor simple boolean criteria, or build custom logic for complex rules
Common Filter Types
Quality Filters
- Remove low-quality or spam content
- Filter by content safety scores
- Remove candidates missing required fields
Business Logic Filters
- Apply user preferences and settings
- Enforce diversity constraints
- Remove already-seen content
Performance Filters
- Limit candidate pool size before expensive operations
- Remove candidates that would fail downstream validation
Best Practices
- Sequential Ordering: Place cheap filters before expensive ones
- Descriptive Names: Override
name()to provide meaningful filter identifiers - Track Removed: Log removed candidate counts for monitoring
- Error Handling: Return descriptive errors for debugging
- Conditional Execution: Use
enable()to skip filters based on query parameters - Efficient Partitioning: Use
Iterator::partitionfor simple boolean criteria
Performance Considerations
- Filters run sequentially, so order matters for performance
- Place filters that remove many candidates early in the chain
- Avoid expensive async operations if possible; defer to scorers
- Consider batch operations when accessing external services
See Also
- Hydrator - Enriches candidates before filtering
- Scorer - Assigns scores to remaining candidates
- Selector - Selects top candidates after filtering
- Candidate Pipeline Architecture - Pipeline execution flow