elasticsearch,tire,Ruby On Rails,elasticsearch,Tire" /> elasticsearch,tire,Ruby On Rails,elasticsearch,Tire" />

Ruby on rails 轮胎-按相关对象的编号排序

Ruby on rails 轮胎-按相关对象的编号排序,ruby-on-rails,elasticsearch,tire,Ruby On Rails,elasticsearch,Tire,我的模型中有这样的东西: class User < ActiveRecord::Model has_many :followers, :through => :as_followed_followings, :class_name => "User", :foreign_key => "follower_id", :uniq => true ... def self.search(params) tire.search(load: true

我的模型中有这样的东西:

class User < ActiveRecord::Model

  has_many :followers, :through => :as_followed_followings, :class_name => "User", :foreign_key => "follower_id", :uniq => true

  ...

  def self.search(params)
    tire.search(load: true, page: params[:page], per_page: params[:per]) do
    end
  end

end
class用户:as_followers,:class_name=>“User”,:foreign_key=>“followers_id”,:uniq=>true
...
定义自搜索(参数)
搜索(加载:true,第页:参数[:page],每页:参数[:per])do
结束
结束
结束
我想返回一个按用户拥有的追随者数量排序的用户数组

那么,定义映射和索引以搜索具有count属性的嵌套对象的正确方法是什么?

感谢阅读。

尝试使用

AsFollowedFollowing.count(:group => :follower_id, :order => "count_all DESC")
这是包含
user\u id=>count

如果要循环此哈希,请尝试以下操作

# in controller
@counts = AsFollowedFollowing.count(:group => :follower_id, :order => "count_all DESC")
@users = User.where(:id => @counts.map{|k,_| k})

#in view
<% @counts.each do |k, v| %>
  <% user = @users.find{|u| u.id == k} %>
  <div><%= user.name %> | <%= v %></div>
<% end %>
控制器中的
#
@counts=asfollowdfollowing.count(:group=>:follower\u id,:order=>“count\u all DESC”)
@users=User.where(:id=>@counts.map{| k,124; k})
#鉴于
| 

我通过在
用户
表上创建另一个字段并索引该值,而不是在运行时执行嵌套搜索,解决了这个问题


这可能不是最好的解决方案,但我有最后期限。欢迎提供更好的解决方案:)

是elasticsearch的DSL吗。count是ActiveRecord::Model的方法。谢谢您的回答,但这不是主题,因为我的问题是关于elasticsearch和tire gem:)