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
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.
Related
I am trying to set search on a model that has a lot of different associations. I am starting with the belongs_to associations. I am able to search on the name field of the Product model successfully but the when I perform a search on what would be in the associated models I just get the default results.
What am I doing wrong?
Any help would be much appreciated.
#Product Model
Class Product < ActiveRecord::Base
searchable do
text :name
integer :store_id, :references => Store.name
text :store do
Store.all.map { |store| store.name }
end
end
end
#product controler
def search
#search = Sunspot.search(Product) do
fulltext params[:search] do
fields(:name, :store)
end
end
#products = #search.results
end
#Store Model
searchable do
text :name
end
Class Product < ActiveRecord::Base
belongs_to :store
searchable do
text :name
index :store do
index :name
end
integer :store_id # do you really need this? I think not.
end
end
Don't forget to reindex after each change in your models.
EDIT: You don't need to index the Store class by itself, unless you plan to search on it.
I have models such as
category(mobile,computer)
product(nokia,dell,apple)
models(c5)
posts
If i search for a mobile it should fetch from category model and match with posts and i should get a result.
Using Railcast
I created a text search but how do i search based on above criteria
I tried something like this in my category
This is my post model
belongs_to :product
searchable do
text :title,:description
text :products do
products.map(&:name)
end
end
Whether above searchable is correct but i didn't get any result
Edit 1
I found a SO link
And i tried something like this
My controller
def index
#search = Post.search do
fulltext params[:search] do
fields(:product)
end
end
#posts = #search.results
end
And My model
belongs_to :product
searchable do
text :title,:description,:name
text :product do
product.name
end
end
It shows nothing
I am implementing a basic tagging feature to my app. New to rails.
I have a listings model and I am working in the listings_controller # index. When the user goes to the listing page and sets the :tag param I want #users only to hold the users which match the tag.
So if they goto www.example.com/listings?tag=foo only pages which have been tagged with foo are loaded. This is what I have come up with so far but there is several problems with it.
def index
if params[:tag]
#id = Tag.where(:name => params[:tag]).first.id
#listingid = Tagging.where(:tag_id => #id)
#listingid.each do |l|
#users = User.find(l.listing_id)
end
else
#users = User.all
end
end
I am not sure how to loop and add each found user to #users. I think I may be going about this whole thing the wrong way.. My tag/tagging models look as follows:
tag.rb
class Tag < ActiveRecord::Base
has_many :taggings
has_many :listings, through: :taggings
end
tagging.rb
class Tagging < ActiveRecord::Base
belongs_to :tag
belongs_to :listing
end
Taggings has the following colums:
id, tag_id, listing_id
Tags has the following columns:
id, name
Any guidance would be appreciated, been trying to fix this for a while with no luck.
Trying with
def index
#tag = Tag.where(:name => params[:tag]).first
if #tag
#listings = #tag.listings.includes(:user)
#users = #listings.map{|l| l.user}
else
#users = User.all
end
end
In my app I am using Sunspot for a fulltext search. The problem is that I want to have sorting by association model field. In my case:
class Movie < ActiveRecord::Base
attr_accessible :description, :genre, :name
has_many :premieres
end
and my Premier model has:
belongs_to :movie
Searching by movie name is done by defining the method:
def movie_name
movie.name
end
but when I try to do:
order_by :movie_name, :asc
It says:
No field configured for Premiere with name 'movie_name'
How do I make this sorting available?
you can do it like this
searchable auto_index: true do
text :movie_name do
if self.movie.present?
self.movie.name
end
end
end
then you can use
order_by :movie_name, :asc
I have a model User and Listing in my rails app. User has many listings and listing belongs to user. I also have a attribute name rating in user table. What I want is to search the keyword in Listing model and order it based on rating attribute of User model.
This is what I have in Listing model
searchable do
text :title, :default_boost => 3
text :description, :default_boost => 2
integer :category_id, :references => Category
integer :subcategory_id, :references => Subcategory
string :zipcode
time :created_at
double :user do
user.rating
end
end
And this is how I am trying to search
#search = Sunspot.search(Listing) do
keywords params[:q] do
fields :title
end
order_by THIS IS WHERE I NEED HELP
paginate :page => params[:page], :per_page => 20
end
You will need to add the keyword and rating attributes to the listing searachable method.
class Listing < ActiveRecord::Base
belongs_to :user
searchable do
text :keyword
integer :rating { user.rating }
end
end
Then in your search action in your controller
Listing.search do
fulltext params[:q]
order_by :rating, :desc
end
See http://sunspot.github.com/ for more examples.
Looking at your code, you need to change in your searchable method
double :user do
user.rating
end
to
double :rating do
user.rating
end