Skip to main content

Overview

The Scorer 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.
Scorers MUST maintain the same candidate order and count. They cannot drop or reorder candidates - use Filter stages for removal.

Trait Definition

Type Parameters

generic
The query type that contains request context and parametersConstraints: Clone + Send + Sync + 'static
generic
The candidate type that will be scoredConstraints: Clone + Send + Sync + 'static

Methods

enable

fn
Determines whether this scorer should run for the given query
&Q
Reference to the query object
bool
Returns true if this scorer should run, false to skip. Default implementation returns true.

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.
The returned vector MUST have the same candidates in the same order as the input. Dropping or reordering candidates is not allowed.

update

fn
Copies score fields from a scored candidate into the target candidate
&mut C
Mutable reference to the candidate to update
C
The scored candidate containing new score values
Only copy the fields that this scorer is responsible for. Leave other fields unchanged.

update_all

fn
Updates all candidates with their corresponding scored fields
&mut [C]
Mutable slice of candidates to update
Vec<C>
Vector of scored candidates from the score method
The default implementation iterates and calls update for each pair. Override for custom batching logic.

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 update for 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

  1. Maintain Order: Always return candidates in the same order as received
  2. Batch Operations: Send all candidates to the model in a single request
  3. Error Handling: Handle model failures gracefully, consider fallback scores
  4. Field Isolation: Only update fields owned by this scorer in the update method
  5. Logging: Include request IDs for tracing predictions through the system
  6. 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