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 http://www.clecotech.in/2016/02/multiple-range-filter-with-float-or.html
4. Order all the products by location ( lat lng ) as per end user location
  a. In model
    latlon(:location) { Sunspot::Util::Coordinates.new(store.lat, 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
      tags.collect(&:name)
    end
  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) { 
      Sunspot::Util::Coordinates.new(store.lat, store.lng)
    }
    string :tags, :multiple => true do
      tags.collect(&:name)
    end
    float :pricing do
      pricing.mrp_per_unit
    end
    # join(:mrp_per_unit, :target => Pricing, :type => :float, :join => { :from => :product_id, :to => :id })
    text :category do
      category.title
    end
    text :sub_category do
      sub_category.title
    end
    text :child_sub_category do
      child_sub_category.title
    end
  end

It is the code that we can write in controller for performing the search based on the added objects in model:

  Product.search 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 
      params[:pricing].collect{|range| 
        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
  end.results

For more simple example please visit that

http://www.clecotech.in/2016/02/sunspot-solr-search-in-ruby-on-rails.html
http://www.clecotech.in/2016/02/multiple-range-filter-with-float-or.html
https://github.com/sunspot/sunspot

No comments:

Post a Comment