ruby on rails - Leaderboard ranking via elasticsearch (with tire) -
i have mapping boils down following (unrelated fields removed):
mapping indexes :id, type: 'integer', index: :not_analyze indexes :first_name, boost: 5, type: 'string', analyzer: 'snowball' indexes :votes, type: 'integer', index: :not_analyzed end
at moment i'm calculating ranking via postgres, given following entries:
| first_name | votes | ---------------------- | andy | 5 | | barry | 8 | | carl | 5 | | derek | 1 |
using postgres, can following:
| first_name | votes | rank | ----------------------------- | barry | 8 | 1 | | andy | 5 | 2 | | carl | 5 | 2 | | derek | 1 | 4 |
is possible somehow calculate ranking via elasticsearch?
i don't believe elasticsearch place it, since updating single document require recalculation of ranking values. not possible, far can tell.
instead, once results can use ruby calculate ranking this:
scores = {:a=>5, :b=>8, :c=>5, :d=>1} scores.values.sort{|a,b| <=> b}.tap |sorted_scores| sorted_scores.each{|vote| puts sorted_scores.index(vote)+1 } end
Comments
Post a Comment