在太阳黑子中按多列排序?

时间:2011-11-19 20:23:27

标签: ruby-on-rails ruby ruby-on-rails-3 will-paginate sunspot

解决

我希望我的:price:purchase_date字段可以在我的搜索索引中一起订购。因此,它将提供最新的当前日期,然后是下一个日期的最低价格。这是怎么做到的?

我认为在执行操作时存在错误:

order_by :purchase_date, :desc
order_by :price,:asc

与之相比:

order_by(:purchase_date, :desc)
order_by(:price,:asc)

我完成的代码是这样的:

class SearchController < ApplicationController

  def index
    @search = UserPrice.search do
      fulltext params[:search]
      paginate(:per_page => 5, :page => params[:page])
      facet :business_retail_store_id, :business_online_store_id
      order_by(:purchase_date, :desc) 
      order_by(:price,:asc)
    end
    @user_prices = @search.results
  end
end

1 个答案:

答案 0 :(得分:8)

UserPrice.order(:by => [:price,:purchase_date])

UserPrice.order("price DESC, purchase_date DESC")

请参阅:

http://guides.rubyonrails.org/active_record_querying.html

http://m.onkey.org/active-record-query-interface


以下是太阳黑子网站的一个例子:

Post.search do
  fulltext 'best pizza'
  with :blog_id, 1
  with(:published_at).less_than Time.now
  order_by :price, :desc           # descending order , check Documentation link below
  order_by :published_at, :desc    # descending order , check Documentation link below
  paginate :page => 2, :per_page => 15
  facet :category_ids, :author_id
end

Sunspot文档说:

order_by(field_name,direction = nil)指定应返回结果的顺序。此方法可多次调用;优先顺序将是给定的顺序。

请参阅:

http://outoftime.github.com/sunspot/

http://outoftime.github.com/sunspot/docs/index.html