🔀 RAG Fusion:多路检索融合技术

发布时间:2026-06-09 | 分类:RAG技术 | 难度:⭐⭐⭐
"只用一种检索方式,就像只用一只眼睛看世界。RAG Fusion让你同时用向量检索、关键词检索、混合检索,然后把结果融合起来——就像开了天眼。"

📖 一句话定义

RAG Fusion是一种将多种检索策略(向量检索、关键词检索、语义检索等)的结果进行融合排序的技术,通过Reciprocal Rank Fusion (RRF)等算法,综合各路检索的优势,找到最相关的文档。

🏗️ 工作原理

用户查询: "OpenClaw的MCP协议怎么配置?"
         ↓
    ┌────┴────┐
    ↓         ↓         ↓
向量检索   关键词检索   语义检索
[doc1,3,5] [doc2,3,5] [doc1,3,4]
    ↓         ↓         ↓
    └────┬────┘
         ↓
   RRF 融合排序
         ↓
   [doc3, doc5, doc1, doc4, doc2]
         ↓
   取Top-K作为上下文

🔧 OpenClaw实战:实现RAG Fusion

// RAG Fusion实现
async function ragFusion(query, retrievers, topK = 5) {
    // 并行执行多种检索
    const results = await Promise.all(
        retrievers.map(r => r.search(query, { topK: topK * 2 }))
    );
    
    // Reciprocal Rank Fusion
    const scoreMap = new Map();
    const k = 60; // RRF常数
    
    results.forEach(retrieverResults => {
        retrieverResults.forEach((doc, rank) => {
            const score = 1 / (k + rank + 1);
            scoreMap.set(doc.id, (scoreMap.get(doc.id) || 0) + score);
        });
    });
    
    // 按融合分数排序
    return Array.from(scoreMap.entries())
        .sort((a, b) => b[1] - a[1])
        .slice(0, topK)
        .map(([id, score]) => ({ id, score }));
}
💡 妙趣提示:RRF的k值通常设为60,这是论文推荐的默认值。但在实际应用中,你可以根据数据特点微调。

⚠️ 注意事项

⚠️ 踩坑提醒:
1. 检索器数量越多,延迟越高,注意并行优化
2. 不同检索器的分数尺度不同,RRF能自动处理
3. 融合不一定总是优于单路检索,需要A/B测试

🔗 相关术语

RAG Hybrid Search Graph RAG Agentic RAG