在轨道3.2上使用太阳黑子solr进行自定义查询

时间:2012-11-10 11:59:14

标签: ruby-on-rails ruby ruby-on-rails-3 solr sunspot

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

有争议

类似的东西:

{{1}}

谢谢!

1 个答案:

答案 0 :(得分: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挂钩来执行此操作,也可以每天重新索引一次,具体取决于您的需求和索引的大小。