Rails search with sunspot

I have one to many association between jobs and companies and i have implemented a search form for jobs using sunspot gem but i want when i search a company_name i have all jobs results of the company searched how can i do this

this is my job model

class Job < ActiveRecord::Base

belongs_to :company




searchable do
  text :job_title, boost: 4
  text :profile_recherche
end 

this is my company model

class Company < ActiveRecord::Base

has_many :jobs

this is my jobs search controller

def search

@jobs = Sunspot.search(Job) do
  keywords params[:query]
  fulltext params[:query]
  paginate(page: params[:page], per_page: 1)
end.results

respond_to do |format|
  format.html { render :action => "index" }
end

end

Answers


It sounds like you want to search Jobs by job_title, profile_reserche, and company.name. If I'm guessing what you want correctly, changing your searchable block like so should do it:

class Job < ActiveRecord::Base

  belongs_to :company

  searchable do
    text :job_title, boost: 4
    text :profile_recherche
    text :company_name do
      company.name
    end
  end
end

One caveat here is that if you have overlap in tokens, stems, n-grams, whatever between these various fields, you could have some interesting search results.


Need Your Help

Laravel routing: /seo-path-here to LandingPageController@getPage(3)

php laravel laravel-4 routing

I have a website (using Laravel 4) where I want to build landing pages for ad campaigns. At first, I want to have just a single landing page view. But its H1 tag and some other content needs to m...

knockout.js unwrapObservable function

javascript knockout.js

I find todomvc examples - http://todomvc.com/