2

3种型号:

Class Product
 include Mongoid::Document
 has_many :orders, dependent: :destroy, :autosave => true
 #search
 searchable do
  text :title, :description, :boost => 2.0
  time :created_at
 end
end

Class Order
 include Mongoid::Document
 belongs_to :product
 has_one :dispute, dependent: :destroy, :autosave => true
end

Class Dispute
 include Mongoid::Document
 belongs_to :order
 field :buyer_has_requested_refund, :type => Boolean, :default => "false"
end

我需要在内部products_controller.rb采取index行动,从高到低排序所有与订单有争议的产品buyer_has_requested_refund = true

就像是:

def index
 @search = Product.solr_search do |s|
 s.fulltext params[:search]
 s.keywords params[:search]
 s.order_by :disputes_where_buyer_has_requested_refund, :desc
 end
 @products = @search.results
    respond_to do |format|
    format.html # index.html.erb
  end 
end

谢谢!

4

1 回答 1

0

您应该告诉 solr 索引排序搜索所需的信息:

class Product
 include Mongoid::Document
 has_many :orders, dependent: :destroy, :autosave => true
 has_many :disputes, :through => :orders
 #search
 searchable do
  text :title, :description, :boost => 2.0
  integer :disputes_where_buyer_has_requested_refund { |product| product.disputes.where(:buyer_has_requested_refund => true).size }
  time :created_at
 end
end

请注意在创建/销毁新争议时相应地重新索引索引中的记录值。您可以在延迟作业中使用 after_create/after_destroy 挂钩来执行此操作,也可以根据您的需要和索引的大小每天重新索引一次。

于 2012-12-11T19:49:25.900 回答