ruby-on-rails - 如何为多个模型的查询排序 Sunspot 搜索结果?

标签 ruby-on-rails solr sunspot

我有两个期刊模型,ClientJournalInstitutionJournal,我正在使用 Sunspot 运行一个查询,该查询获取记录以将它们一起显示在期刊索引页上:

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

有什么方法可以告诉 Sunspot 将结果排序在一起,就好像它们是同一模型一样?

最佳答案

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

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 }

关于ruby-on-rails - 如何为多个模型的查询排序 Sunspot 搜索结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26074985/

相关文章:

python - 如何在 PySolr 中使用分面 - 似乎无法显示分面结果

ruby-on-rails - 使用 Geocoder 和 Sunspot 进行搜索?

ruby-on-rails - sunspot solr 按多个术语搜索

javascript - Rails 3.1 http streaming - js 在头部或 body 底部?

ruby-on-rails - Activerecord 对象数组到逗号分隔的字符串

ruby-on-rails - RestKit 0.10.1 with Core Data 陡峭的学习曲线

mysql - 无法使用数据库中的 SOLR DataImportHandler 获取任何行

php - mysql+php 中匹配相似列表

ruby-on-rails - 在 Sunspot/Solr 中同时搜索多个模型以获取 Rails

ruby-on-rails - 在保留 Model.new({attrib : 'blah' }) format