Sunspot Solr Search example with all important features

Below you will find all the practical information about that sunspot solr search:

Points those are covered below :

1. Solr search for same model
2. Solr search in relationships
  a. has_many  ( Using Join )
  b. has_one ( Using Join and without join )
3. Search products by price ranges
  a. Follow that for more specific information
4. Order all the products by location ( lat lng ) as per end user location
  a. In model
    latlon(:location) {, store.lng) }
  b. In controller
    order_by_geodist(:location, params[:latitude], params[:longitude])
5. Find nearest products in some distance as per my location ( lat, long )
  with(:location).in_radius(params[:latitude], params[:longitude], params[:distance]) if !params[:distance].blank? && params[:distance].to_i > 0
6. Search by tags as per acts-as-taggable-on gem or normal array of tags
  a. In Model
    string :tags, :multiple => true do
  b. In Controller
     with(:tags, params[:tags].split(“,”)) if !params[:tags].blank?

We are setting up the objects inside the model like that :

  # Setting Up Objects  
searchable do
integer :id
integer :store_id
text :title
join(:city, :target => Store, :type => :text, :join => { :from => :id, :to => :store_id })
join(:state, :target => Store, :type => :text, :join => { :from => :id, :to => :store_id })
join(:landmark, :target => Store, :type => :text, :join => { :from => :id, :to => :store_id })
join(:address, :target => Store, :type => :text, :join => { :from => :id, :to => :store_id })
join(:pin_code, :target => Store, :type => :text, :join => { :from => :id, :to => :store_id })
latlon(:location) {, store.lng)
string :tags, :multiple => true do
float :pricing do
# join(:mrp_per_unit, :target => Pricing, :type => :float, :join => { :from => :product_id, :to => :id })
text :category do
text :sub_category do
text :child_sub_category do

It is the code that we can write in controller for performing the search based on the added objects in model: do 
fulltext params[:q] if !params[:q].blank?
order_by_geodist(:location, params[:latitude], params[:longitude])
fulltext params[:pin_code], {fields: :pin_code} if !params[:pin_code].blank?
with(:tags, params[:tags].split(",")) if !params[:tags].blank?
with(:location).in_radius(params[:latitude], params[:longitude], params[:distance]) if !params[:distance].blank? && params[:distance].to_i > 0
fulltext params[:location], {
fields: [:city, :landmark, :address, :state]
} if !params[:location].blank?
any_of do
eval(range).is_a?(Range) ? with(:pricing).between(eval(range)) : with(:pricing).greater_than(eval(range))
end if !params[:pricing].blank?
paginate :page => params[:page], per_page: 15

For more simple example please visit that

About the author

Being the CEO and Founder of ClecoTech International, Mr. Ashish Prajapati is dedicated towards his aim of mentoring young startups into a full-fledged businesses. He is helping startups from America, Europe, India, and various other countries through proper guidance and the use of latest technologies to develop their innovation and ideas into definite realities.

Leave a Reply

Your email address will not be published. Required fields are marked *