【问题标题】:custom query with sunspot solr on rails 3.2在 Rails 3.2 上使用太阳黑子 solr 进行自定义查询
【发布时间】:2012-11-10 11:59:14
【问题描述】:

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

谢谢!

【问题讨论】:

  • 您在哪个模型中添加了用于索引这些对象的可搜索块。
  • 在模型Product.rb上。我已经编辑了问题并添加了可搜索块。非常感谢!

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


【解决方案1】:

您应该告诉 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 挂钩来执行此操作,也可以根据您的需要和索引的大小每天重新索引一次。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-17
    • 1970-01-01
    相关资源
    最近更新 更多