Skip to main content

Overview

The Selector trait defines how the final set of candidates is chosen from the scored pool. Selectors typically sort candidates by their scores and truncate to a desired size.

Trait Definition

Type Parameters

generic
The query type that contains request context and parametersConstraints: Clone + Send + Sync + 'static
generic
The candidate type to select fromConstraints: Clone + Send + Sync + 'static

Methods

select

fn
Main selection method that sorts and truncates candidates
&Q
Reference to the query object
Vec<C>
Vector of candidates to select from (takes ownership)
Vec<C>
Selected and sorted candidates, truncated to size() if specified
The default implementation calls sort() and then truncates to size() if provided.

enable

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

score

fn
required
Extracts the score to use for sorting from a candidate
&C
Reference to a candidate
f64
The numeric score to use for ranking. Higher scores rank higher.
This method must be implemented by the selector. Use f64::NEG_INFINITY for candidates that should rank lowest.

sort

fn
Sorts candidates by their scores in descending order
Vec<C>
Vector of candidates to sort
Vec<C>
Sorted candidates with highest scores first
The default implementation sorts by calling score() for each candidate and comparing in descending order.

size

fn
Optionally specifies how many candidates to select
Option<usize>
The number of top candidates to select, or None for no truncation. Default implementation returns None.

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 selects the top K candidates by score:
With the default implementations, this selector will:
  1. Sort all candidates by their score field in descending order
  2. Truncate to the top 100 candidates
  3. Return the selected candidates

Advanced Example

Here’s a more complex selector that implements custom selection logic:

Usage Notes

  • Selectors typically run once at the end of the pipeline
  • The select method can be overridden for complex selection logic
  • The default select implementation handles most simple cases (sort + truncate)
  • The sort method can be overridden for custom sorting algorithms
  • Use enable() to conditionally apply different selectors based on query parameters

Common Selector Types

Score-Based Selectors

  • Top-K by final score
  • Top-K by specific score field (e.g., engagement score)
  • Threshold-based selection

Diversity Selectors

  • Limit candidates per author
  • Ensure content type diversity
  • Balance different candidate sources

Context-Aware Selectors

  • Time-of-day dependent selection
  • User preference-based selection
  • A/B test variant selection

Best Practices

  1. Score Extraction: Implement score() to extract the appropriate score field
  2. Handle Missing Scores: Use f64::NEG_INFINITY for candidates without scores
  3. Efficient Sorting: The default sort() is efficient for most cases
  4. Overflow Safety: Use Vec::truncate() which handles cases where size > length
  5. Deterministic Ordering: Ensure stable sorting for reproducible results
  6. Override Selectively: Override select() only when you need custom logic beyond sort+truncate

Performance Considerations

  • Sorting is O(n log n) where n is the candidate count
  • Consider using partial sorting (like select_nth_unstable) for large candidate pools
  • Truncation after sorting is O(1) using Vec::truncate()
  • The selector is typically not on the critical path since it runs on already-reduced candidate sets

See Also