$exact * Node IDs of the exact matches (references, phrase, all words), in order. * Always first, whatever the other lists say. * @param array> $rankings * Ranked lists of node IDs (keyword OR query, kNN neighbours…). * @param int $k * RRF constant. * @param array $weights * Weight of each ranking, same keys; 1 when missing (Q8). * * @return array * Node IDs, each once. */ public static function fuse(array $exact, array $rankings, int $k = self::K, array $weights = []): array { $merged = []; foreach ($exact as $nid) { $merged[$nid] ??= $nid; } $scores = []; $first = []; $position = 0; foreach ($rankings as $key => $ranking) { $weight = $weights[$key] ?? 1.0; $rank = 0; foreach ($ranking as $nid) { $rank++; if (isset($merged[$nid])) { continue; } if (!isset($scores[$nid])) { $scores[$nid] = 0.0; $first[$nid] = [$position++, $nid]; } $scores[$nid] += $weight / ($k + $rank); } } uksort($scores, static fn ($a, $b): int => [$scores[$b], $first[$a][0]] <=> [$scores[$a], $first[$b][0]]); foreach (array_keys($scores) as $nid) { $merged[$nid] = $first[$nid][1]; } return array_values($merged); } }