Module: Searchkick::Reranking

Defined in:
lib/searchkick/reranking.rb

Class Method Summary collapse

Class Method Details

.rrf(first_ranking, *rankings, k: 60) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/searchkick/reranking.rb', line 3

def self.rrf(first_ranking, *rankings, k: 60)
  rankings.unshift(first_ranking)
  rankings.map!(&:to_ary)

  ranks = []
  results = []
  rankings.each do |ranking|
    ranks << ranking.map.with_index.to_h { |v, i| [v, i + 1] }
    results.concat(ranking)
  end

  results =
    results.uniq.map do |result|
      score =
        ranks.sum do |rank|
          r = rank[result]
          r ? 1.0 / (k + r) : 0.0
        end

      {result: result, score: score}
    end

  results.sort_by { |v| -v[:score] }
end