Sunspot Solr Rails - 使用'with'搜索多个模型

Rao*_*oot 5 search solr ruby-on-rails sunspot ruby-on-rails-3

我在我的应用程序中找到了Solr的成功.我已经设法搜索了两个模型,但我现在正试图使用​​'with'子句来微调我的搜索.

我的活动模型有:

class Event < ActiveRecord::Base
  searchable do
   text :headline, :info
   time :event_date
  end
end
Run Code Online (Sandbox Code Playgroud)

我的场地模型有:

class Venue < ActiveRecord::Base
  searchable do
   text :name, :address_1, :address_2, :town, :postcode
  end
end
Run Code Online (Sandbox Code Playgroud)

然后我有一个搜索控制器,我之前设法返回基于我的事件和场地params[:search].我现在正试图返回即将举办的活动和场地,但由于场地中没有'event_date',我现在只能回收事件.简而言之,我如何with(:event_date)仅应用于事件模型?

class SearchController < ApplicationController
  def search
    @search = Sunspot.search [Event, Venue] do
      fulltext params[:search]
      with(:event_date).greater_than(Time.zone.now)
    end
    @results = @search.results
    respond_to do |format|
      format.json { render json: @results }
    end
  end  
end  
Run Code Online (Sandbox Code Playgroud)

小智 1

您可以做的一件事是将此字段添加到另一个“可搜索”字段,如下所示:

class Venue < ActiveRecord::Base
  searchable do
   text :name, :address_1, :address_2, :town, :postcode
   time :event_date { Time.zone.now }
  end
end
Run Code Online (Sandbox Code Playgroud)

你可以在 { #here } 中放入任何你想要的内容

希望能帮助到你