$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. * * @return array * Node IDs, each once. */ public static function fuse(array $exact, array $rankings, int $k = self::K): array { $merged = []; foreach ($exact as $nid) { $merged[$nid] ??= $nid; } $scores = []; $first = []; $position = 0; foreach ($rankings as $ranking) { $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] += 1 / ($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); } }