Overview
Filters remove unwanted candidates from the pipeline. They run sequentially after candidate hydration, allowing each filter to see the results of previous filters. Filters partition candidates into “kept” and “removed” sets.Filter Trait
TheFilter trait defines the interface for filtering candidates:
candidate-pipeline/filter.rs
fn(&self, &Q) -> bool
Optional gating logic to conditionally enable/disable the filter based on query parameters
async fn(&self, &Q, Vec<C>) -> Result<FilterResult<C>, String>
The main filtering method. Partitions candidates into kept and removed sets.
fn(&self) -> &'static str
Returns the filter name for logging and metrics. Auto-generated from the type name by default.
Age Filter
The Age filter removes tweets older than a specified duration:home-mixer/filters/age_filter.rs
The Age filter uses Twitter’s Snowflake ID format to extract the creation timestamp from the tweet ID itself, without requiring external service calls.
Self Tweet Filter
The Self Tweet filter removes tweets where the author is the viewer:home-mixer/filters/self_tweet_filter.rs
This filter prevents users from seeing their own tweets in their For You feed, which improves content diversity.
Author Socialgraph Filter
The Author Socialgraph filter removes candidates from blocked or muted authors:home-mixer/filters/author_socialgraph_filter.rs
Visibility Filtering (VF) Filter
The VF filter applies visibility filtering rules based on safety results:home-mixer/filters/vf_filter.rs
The VF filter applies content safety policies by removing tweets that violate safety rules. This relies on the
visibility_reason field being hydrated earlier in the pipeline.Available Filters
The home-mixer service includes these filters:AgeFilter
Removes tweets older than a specified duration
AuthorSocialgraphFilter
Removes tweets from blocked or muted authors
CoreDataHydrationFilter
Removes tweets missing core data
DedupConversationFilter
Removes duplicate conversation threads
DropDuplicatesFilter
Removes exact duplicate tweets
IneligibleSubscriptionFilter
Removes subscription content for non-subscribers
MutedKeywordFilter
Removes tweets matching muted keywords
PreviouslySeenPostsFilter
Removes tweets the user has already seen
PreviouslyServedPostsFilter
Removes tweets already served in recent requests
RetweetDeduplicationFilter
Deduplicates retweets of the same original tweet
SelfTweetFilter
Removes the user’s own tweets
VFFilter
Applies visibility filtering safety rules
Sequential Execution
Filters run sequentially, allowing each filter to see the results of previous filters:candidate-pipeline/candidate_pipeline.rs
If a filter fails, the error is logged and the original candidate list is restored. This ensures filter failures don’t accidentally remove valid candidates.
Filter Stages
Filters can run at two points in the pipeline:1
Pre-Selection Filtering
Runs after candidate hydration, before scoring. Used for removing invalid or unwanted candidates.
2
Post-Selection Filtering
Runs after selection, on candidates that will be returned. Used for final safety checks.
Error Handling
Best Practices
Use Partition
Use Partition
Use Rust’s
partition method for clean, functional filtering:Early Exit
Early Exit
Short-circuit when filtering isn’t needed:
Logging Context
Logging Context
Include meaningful context in error messages:
Stats Instrumentation
Stats Instrumentation
Use the stats macro for automatic metrics:
Partition Example
The partition pattern is idiomatic Rust for filtering:Performance Considerations
Conditional Enabling
Filters can be conditionally enabled based on query parameters:- User experiments/flags
- Query parameters
- A/B test assignments
- Feature flags
Related Topics
Pipeline Framework
Understand the overall pipeline execution flow
Hydrators
Hydrate features used by filters
Scorers
Score candidates after filtering
Visibility Filtering
Learn about content safety policies