Overview
TheScorer trait defines how candidates are assigned scores by machine learning models or ranking functions. Scorers run sequentially and populate score fields that are used for candidate selection and ranking.
Trait Definition
Type Parameters
generic
The query type that contains request context and parametersConstraints:
Clone + Send + Sync + 'staticgeneric
The candidate type that will be scoredConstraints:
Clone + Send + Sync + 'staticMethods
enable
fn
score
async fn
Performs scoring operations and returns candidates with populated score fields
&Q
Reference to the query object
&[C]
Slice of candidates to score
Result<Vec<C>, String>
Returns a vector with the same length and order as input, with only this scorer’s fields populated. Returns an error message on failure.
update
fn
update_all
fn
name
fn
Returns a stable name for logging and metrics
&'static str
A short type name derived from the implementing struct
Example Implementation
Here’s a real example that scores tweets using the Phoenix ML prediction service:Usage Notes
- Scorers run sequentially in the order they are configured
- Each scorer can update different score fields on the candidate
- Batch scoring operations for efficiency (send all candidates in one request)
- Only populate score fields relevant to this scorer in the returned candidates
- Use
Default::default()for other fields to avoid overwriting data - The pipeline merges results by calling
updatefor each scorer
Common Scorer Types
Machine Learning Scorers
- Neural network model predictions
- Engagement probability scores (likes, retweets, etc.)
- Multi-task prediction outputs
Rule-Based Scorers
- Recency scoring
- Diversity penalties
- Social graph affinity scores
Composite Scorers
- Weighted combinations of multiple signals
- A/B test score variants
- Context-dependent scoring functions
Best Practices
- Maintain Order: Always return candidates in the same order as received
- Batch Operations: Send all candidates to the model in a single request
- Error Handling: Handle model failures gracefully, consider fallback scores
- Field Isolation: Only update fields owned by this scorer in the
updatemethod - Logging: Include request IDs for tracing predictions through the system
- Performance: Monitor scoring latency; it’s often on the critical path
Performance Considerations
- Scorers run sequentially and often involve expensive ML inference
- Minimize the number of scorers on the critical path
- Consider caching scores for recently-scored candidates
- Use batch prediction APIs to score all candidates together
- Monitor P99 latency as scoring can be variable
Difference from Hydrator
See Also
- Hydrator - Enriches candidates with data needed for scoring
- Selector - Uses scores to select top candidates
- Filter - Removes candidates before scoring
- Candidate Pipeline Architecture - Pipeline execution flow