我有一个归结为以下内容的映射(删除了不相关的字段):
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
目前我正在通过 postgres 计算排名,因此给出以下条目:
| first_name | votes |
----------------------
| Andy | 5 |
| Barry | 8 |
| Carl | 5 |
| Derek | 1 |
使用 postgres,我可以获得以下信息:
| first_name | votes | rank |
-----------------------------
| Barry | 8 | 1 |
| Andy | 5 | 2 |
| Carl | 5 | 2 |
| Derek | 1 | 4 |
是否可以通过 elasticsearch 以某种方式计算此排名?
最佳答案
我认为 ElasticSearch 不适合这样做,因为更新单个文档需要重新计算所有排名值。据我所知,这是不可能的。
相反,一旦您获得结果,您可以使用 Ruby 来计算排名,如下所示:
scores = {:a=>5, :b=>8, :c=>5, :d=>1}
scores.values.sort{|a,b| a <=> b}.tap do |sorted_scores|
sorted_scores.each{|vote| puts sorted_scores.index(vote)+1 }
end
关于ruby-on-rails - 通过 elasticsearch 进行排行榜排名(带轮胎),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18496655/