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
The query type that contains request context and parametersConstraints:
Clone + Send + Sync + 'staticThe candidate type that will be scoredConstraints:
Clone + Send + Sync + 'staticMethods
enable
score
Performs scoring operations and returns candidates with populated score fields
Reference to the query object
Slice of candidates to score
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
update_all
name
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 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
| Aspect | Hydrator | Scorer |
|---|---|---|
| Execution | Parallel | Sequential |
| Purpose | Enrich with data | Assign scores |
| Typical operations | Fetch from services | ML inference, ranking |
| Performance | Multiple run concurrently | Each adds latency |
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