Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sunspot Solr Rails - Search Multiple Models using 'with'

I'm having some success sutting up Solr in my App. I've managed to search across two models but am now trying to fine tune my search using 'with' clauses.

My event model has:

class Event < ActiveRecord::Base
  searchable do
   text :headline, :info
   time :event_date
  end
end

My venue model has:

class Venue < ActiveRecord::Base
  searchable do
   text :name, :address_1, :address_2, :town, :postcode
  end
end

And then I have a search controller where I previously managed to return both events and venues based my params[:search]. I'm now trying to return forthcoming events and venues but as there's no 'event_date' in venues I now only get the events back. In short, how do I make with(:event_date) apply to the Event model only?

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  
like image 675
Raoot Avatar asked Nov 12 '22 22:11

Raoot


1 Answers

One thing you could do is to add this field to the other "searchable" like this:

class Venue < ActiveRecord::Base
  searchable do
   text :name, :address_1, :address_2, :town, :postcode
   time :event_date { Time.zone.now }
  end
end

you can put whatever you want in the { #here }

Hope it helps

like image 176
Jorge Silveira Avatar answered Nov 14 '22 22:11

Jorge Silveira