Repository navigation
Description
Summary
Add Reciprocal Rank Fusion (RRF) as a new hybrid search ranking strategy alongside the existing WSF (Weighted Score Fusion). RRF ranks results by their position across BM25 and semantic result lists rather than raw scores, making it more robust when score distributions differ between retrieval methods.
Parent epic: #204
Background
Cortex currently uses WSF for hybrid search:
WSF_score = (bm25_weight × bm25_score) + (semantic_weight × semantic_score) + confidence_bonus
WSF works well but is sensitive to score normalization — BM25 scores can range 0–20+ while semantic cosine similarity is 0–1. RRF sidesteps this by ranking purely on position:
RRF_score = Σ 1 / (k + rank_i)
Where k is a constant (typically 60) and rank_i is the result's position in each retriever's ranked list.
Reference: Cormack, Clarke, Buettcher (2009)
Implementation Plan
1. Add RRF scorer (internal/search/rrf.go)
// RRFConfig holds parameters for Reciprocal Rank Fusion.
type RRFConfig struct {
K int // Ranking constant (default: 60)
BM25Weight float64 // Optional weight multiplier for BM25 ranks (default: 1.0)
SemanticWeight float64 // Optional weight multiplier for semantic ranks (default: 1.0)
}
// FuseRRF takes two ranked result lists and returns a merged list scored by RRF.
func FuseRRF(bm25Results, semanticResults []ScoredMemory, cfg RRFConfig) []ScoredMemory- Accept two pre-sorted result lists (BM25 by BM25 score, semantic by cosine similarity)
- For each unique memory ID, compute
rrf_score = w_bm25/(k + rank_bm25) + w_sem/(k + rank_sem) - If a result only appears in one list, use
rank = len(list) + 1as penalty rank - Apply existing confidence decay bonus after RRF scoring
- Sort by final RRF score descending
2. Wire into search engine (internal/search/engine.go)
- Add
ModeRRF = "rrf"to the mode constants - In
HybridSearch(), when mode isrrf:- Run BM25 and semantic searches independently (already done for hybrid)
- Pass both result lists to
FuseRRF()instead ofFuseWSF() - Return merged results
3. CLI flag (cmd/search.go)
--mode rrfalready fits the existing--modeflag pattern (hybrid,bm25,semantic)- Add
rrfas a valid option - Display
[RRF]tag in output header when active
4. MCP integration
cortex mcpsearch tool already accepts amodeparameter —rrfshould work automatically once the engine supports it
Testing
Unit tests (internal/search/rrf_test.go)
| Test | Description |
|---|---|
TestRRFBasicFusion |
Two lists with overlapping results merge correctly |
TestRRFDisjointLists |
Results only in one list get penalty rank |
TestRRFSingleList |
One empty list still returns results from the other |
TestRRFKParameter |
Different K values change relative ordering |
TestRRFConfidenceDecay |
Confidence bonus applies after RRF scoring |
TestRRFDeterministic |
Same inputs always produce same output |
Integration test
- Search the same query with
--mode hybrid(WSF) and--mode rrf— both should return results, potentially in different order - Verify RRF scores are in (0, 1] range
Acceptance Criteria
-
cortex search "trading" --mode rrfreturns scored results - RRF score displayed in output (e.g.,
[RRF: 0.0312]) - Default hybrid mode unchanged (WSF remains default)
-
cortex mcpsearch tool acceptsmode: "rrf" - 6+ new unit tests passing
- Existing search tests unaffected
Files to Modify
| File | Change |
|---|---|
internal/search/rrf.go |
New — RRF scorer |
internal/search/rrf_test.go |
New — RRF tests |
internal/search/engine.go |
Add ModeRRF, wire into hybrid path |
cmd/search.go |
Accept rrf in --mode validation |
README.md |
Document --mode rrf option |
Estimated Effort
Small — isolated scorer function + wiring. No schema changes. No new dependencies.
Activity
feat(search): add rrf search mode (#205)