Summary of fuseResults in Context

Where fuseResults is Called

const fusedResults = fuseResults(
    results.map(r => r.snippets),
    result => {
        if (typeof result.startLine === 'undefined' || typeof result.endLine === 'undefined') {
            return [result.uri.toString()];
        }

        const lineIds = [];
        for (let i = result.startLine; i <= result.endLine; i++) {
            lineIds.push(`${result.uri.toString()}:${i}`);
        }
        return lineIds;
    }
);

Where fuseResults is Defined

export function fuseResults<T>(
    retrievedSets: Set<T>[],
    rankingIdentities: (result: T) => string[]
): Set<T> {
    const resultsByDocument = new Map<string, Map<number, T[]>>();
    // Map results by their identifiers
    retrievedSets.forEach((results, retrieverIndex) => {
        for (const result of results) {
            for (const id of rankingIdentities(result)) {
                if (!resultsByDocument.has(id)) {
                    resultsByDocument.set(id, new Map());
                }
                const document = resultsByDocument.get(id)!;
                if (!document.has(retrieverIndex)) {
                    document.set(retrieverIndex, []);
                }
                document.get(retrieverIndex)!.push(result);
            }
        }
    });

    // Calculate reciprocal ranks
    const fusedDocumentScores: Map<string, number> = new Map();
    retrievedSets.forEach((results, retrieverIndex) => {
        results.forEach((result, rank) => {
            for (const id of rankingIdentities(result)) {
                const isBestRankForRetriever = resultsByDocument.get(id)?.get(retrieverIndex)?.[0] === result;
                if (!isBestRankForRetriever) continue;
                const reciprocalRank = 1 / (RRF_K + rank);
                const score = fusedDocumentScores.get(id) ?? 0;
                fusedDocumentScores.set(id, score + reciprocalRank);
            }
        });
    });

    // Sort and combine results
    const topDocuments = [...fusedDocumentScores.entries()].sort((a, b) => b[1] - a[1]).map(e => e[0]);
    const fusedResults: Set<T> = new Set();
    for (const id of topDocuments) {
        const resultByDocument = resultsByDocument.get(id);
        if (!resultByDocument) continue;

        const maxMatches = Math.max(...[...resultByDocument.values()].map(r => r.length));
        for (let i = 0; i < maxMatches; i++) {
            for (const snippets of resultByDocument.values()) {
                if (i < snippets.length) {
                    fusedResults.add(snippets[i]);
                }
            }
        }
    }
    return fusedResults;
}

Example

High-Level Example:

Identifiers:

Combining and Ranking: