如何为多个模型的查询订购太阳黑子搜索结果?

时间:2014-09-27 13:07:10

标签: ruby-on-rails solr sunspot

我有两个期刊模型,ClientJournalInstitutionJournal我正在使用太阳黑子运行查询,该查询抓取记录以在期刊索引页面上一起显示:

search = Sunspot.search ClientJournal, InstitutionJournal do
  paginate page: params[:page], per_page: 30
  order_by :created_at, :desc
end

这几乎可以工作,但是在排序之前它会将结果分成两个类,所以search.results会返回如下内容:

created_at  class
09:30       ClientJournal
09:12       ClientJournal
08:57       ClientJournal
07:32       ClientJournal
09:45       InstitutionJournal
09:22       InstitutionJournal
09:07       InstitutionJournal

当我真正想要的是这个:

created_at  class
09:45       InstitutionJournal
09:30       ClientJournal
09:22       InstitutionJournal
09:12       ClientJournal
09:07       InstitutionJournal
08:57       ClientJournal
07:32       ClientJournal

有没有办法告诉太阳黑子一起订购结果,好像它们是同一个型号?

1 个答案:

答案 0 :(得分:0)

你可以做这样的事情来搜索多个模型,然后首先获得单个模型数组,然后像我一样对它们进行排序: -

search = Sunspot.search ClientJournal,InstitutionJournal  do
           keywords(params[:search])
          #fulltext params[:search]
          order_by(:created_at, :desc)
          paginate :page => params[:page], :per_page => 10
         end 
##group them by class
 @results = search.results.group_by(&:class)
##get all clientjournals in created_at order
@clientjournals= @results[ClientJournal]
##OR do anything that you want with the array of cilentjournals
@clientjournals= @clientjournals.sort_by { |a| a.created_at }