Overview
TheSelector 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
The query type that contains request context and parametersConstraints:
Clone + Send + Sync + 'staticThe candidate type to select fromConstraints:
Clone + Send + Sync + 'staticMethods
select
Main selection method that sorts and truncates candidatesThe default implementation calls
Reference to the query object
Vector of candidates to select from (takes ownership)
Selected and sorted candidates, truncated to
size() if specifiedsort() and then truncates to size() if provided.enable
score
sort
size
Optionally specifies how many candidates to select
The number of top candidates to select, or
None for no truncation. Default implementation returns None.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 selects the top K candidates by score:- Sort all candidates by their
scorefield in descending order - Truncate to the top 100 candidates
- 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
selectmethod can be overridden for complex selection logic - The default
selectimplementation handles most simple cases (sort + truncate) - The
sortmethod 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
- Score Extraction: Implement
score()to extract the appropriate score field - Handle Missing Scores: Use
f64::NEG_INFINITYfor candidates without scores - Efficient Sorting: The default
sort()is efficient for most cases - Overflow Safety: Use
Vec::truncate()which handles cases where size > length - Deterministic Ordering: Ensure stable sorting for reproducible results
- 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
- Scorer - Assigns scores used by the selector
- Filter - Reduces candidate pool before selection
- Candidate Pipeline Architecture - Pipeline execution flow