Python exercise · about 5 minutes · free
Hybrid Search with Rank Fusion
Keyword search nails 'KL-1001'. Vector search nails 'housing loan'. Advanced RAG uses both and merges the rankings.
Reciprocal rank fusion (RRF) merges several ranked lists without comparing their raw scores. Each document earns 1 / (k + rank) from every list it appears in (rank starts at 1, k is usually 60). Add them up and sort.
📚 Go deeper: LLM Course: Advanced RAG
Your quest: Write
rrf(rankings, k=60): rankings is a list of ranked id lists. Return all ids sorted by fused score, highest first.Try it now Runs in your browser. No sign-in needed. Ctrl+Enter to run.
Hint
scores = {}; for ranking in rankings: for rank, doc in enumerate(ranking, 1): scores[doc] = scores.get(doc, 0) + 1 / (k + rank). Return sorted(scores, key=scores.get, reverse=True).Output appears here.