Skip to content

feat(search): Add RRF (Reciprocal Rank Fusion) search mode #205

Closed

Description

@hurttlocker

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) + 1 as 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 is rrf:
    • Run BM25 and semantic searches independently (already done for hybrid)
    • Pass both result lists to FuseRRF() instead of FuseWSF()
    • Return merged results

3. CLI flag (cmd/search.go)

  • --mode rrf already fits the existing --mode flag pattern (hybrid, bm25, semantic)
  • Add rrf as a valid option
  • Display [RRF] tag in output header when active

4. MCP integration

  • cortex mcp search tool already accepts a mode parameter — rrf should 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 rrf returns scored results
  • RRF score displayed in output (e.g., [RRF: 0.0312])
  • Default hybrid mode unchanged (WSF remains default)
  • cortex mcp search tool accepts mode: "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

  1. added
    enhancement
    v0.8.0
    search
    on Feb 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementsearchv0.8.0

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions