ruby-on-rails – 通过elasticsearch(带轮胎)的排行榜排名
发布时间:2020-12-17 03:05:48 所属栏目:百科 来源:网络整理
导读:我有一个映射,归结为以下(不相关的字段已删除): mapping indexes :id,type: 'integer',index: :not_analyze indexes :first_name,boost: 5,type: 'string',analyzer: 'snowball' indexes :votes,index: :not_analyzedend 目前我正在通过postgres计算排名,因
我有一个映射,归结为以下(不相关的字段已删除):
mapping indexes :id,type: 'integer',index: :not_analyze indexes :first_name,boost: 5,type: 'string',analyzer: 'snowball' indexes :votes,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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |