RUNLOCALAIv38
->Will it run?Best GPUCompareTroubleshootStartLearnPulseModelsHardwareToolsBench
Run check
RUNLOCALAI

Independently operated catalog for local-AI hardware and software. Hand-written verdicts. Source-cited claims. Reproducible commands when we have them.

OP·Fredoline Eruo
DIR
  • Models
  • Hardware
  • Tools
  • Benchmarks
TOOLS
  • Will it run?
  • Compare hardware
  • Cost vs cloud
  • Choose my GPU
  • Prompting kits
  • Quick answers
REF
  • All buyer guides
  • Learn local AI
  • Methodology
  • Glossary
  • Errors KB
  • Trust
EDITOR
  • About
  • Author
  • How we make money
  • Editorial policy
  • Contact
LEGAL
  • Privacy
  • Terms
  • Sitemap
MAIL · MONTHLY DIGEST
Get monthly local AI changes
Monthly recap. No spam.
DISCLOSURE

Some links on this site are affiliate links (Amazon Associates and other first-class retailers). When you buy through them, we earn a small commission at no extra cost to you. Affiliate links do not influence our verdicts — there are cards we rate highly that we don't have affiliate relationships with, and cards that sell well that we refuse to recommend. Read more →

© 2026 runlocalai.coIndependently operated
RUNLOCALAI · v38
  1. >
  2. Home
  3. /Learn
  4. /Courses
  5. /Advanced RAG — Chunking, Retrieval, Re-ranking
  6. /Ch. 8
Advanced RAG — Chunking, Retrieval, Re-ranking

08. Weighted Hybrid Strategies

Chapter 8 of 24 · 15 min
KEY INSIGHT

Weights should be tuned on task-specific benchmarks; generic weights rarely optimize for specific retrieval domains.

RRF treats all strategies equally, but domain knowledge often suggests certain strategies should contribute more.

Adaptive weighting adjusts strategy weights based on query characteristics. Queries with rare terms benefit from sparse retrieval dominance; conceptual queries benefit from dense retrieval.

Manual weighting assigns fixed weights based on offline evaluation. If dense retrieval consistently outperforms sparse on your benchmark, assign higher weight.

from typing import List, Dict

def weighted_hybrid_fusion(
    results_by_strategy: Dict[str, List[dict]],
    weights: Dict[str, float],
    k: int = 60
) -> List[dict]:
    """
    Weighted RRF fusion across strategies.
    
    Args:
        results_by_strategy: {strategy_name: [results]}
        weights: {strategy_name: weight} - weights sum to 1
        k: RRF constant
    """
    # Normalize weights
    total_weight = sum(weights.values())
    normalized_weights = {k: v / total_weight for k, v in weights.items()}
    
    doc_scores = defaultdict(float)
    doc_metadata = {}
    
    for strategy_name, strategy_results in results_by_strategy.items():
        weight = normalized_weights.get(strategy_name, 0)
        
        for rank, result in enumerate(strategy_results, start=1):
            doc_id = result.get('doc_id', result.get('index', rank))
            # Weighted RRF contribution
            doc_scores[doc_id] += weight * (1 / (k + rank))
            
            if doc_id not in doc_metadata:
                doc_metadata[doc_id] = result.copy()
    
    sorted_docs = sorted(doc_scores.items(), key=lambda x: x[1], reverse=True)
    
    return [
        {**doc_metadata[doc_id], 'weighted_rrf_score': score, 'doc_id': doc_id}
        for doc_id, score in sorted_docs
    ]

# Example: weight dense retrieval higher
fused_results = weighted_hybrid_fusion(
    results_by_strategy={
        'dense': dense_search(query, 100),
        'sparse': bm25_search(query, 100),
        'keyword': keyword_search(query, 50)
    },
    weights={
        'dense': 0.5,
        'sparse': 0.35,
        'keyword': 0.15
    }
)

Local verification checkpoint

Run the smallest example from this chapter in a local workspace and record the package version, runtime, data path, and observed output. If the result depends on model size, vector count, CPU/GPU backend, or available memory, note that constraint beside the exercise so the lesson remains reproducible.

Local verification checkpoint

Run the smallest example from this chapter in a local workspace and record the package version, runtime, data path, and observed output. If the result depends on model size, vector count, CPU/GPU backend, or available memory, note that constraint beside the exercise so the lesson remains reproducible.

EXERCISE

Run a grid search over weight combinations on your benchmark. Report the weights that maximize MRR@10.

← Chapter 7
Hybrid Search with RRF
Chapter 9 →
Cross-Encoder Setup