Solr Search, Multiple range filter with float or integer columns for joins with sunspot




I have struggled too much for finding out the appropriate solution but still looking for the best one. Below you can find out an alternative to make that work for at least one column that belongs to other model.

Product Model :

class Product < ActiveRecord::Base
searchable do
    integer :id
    # we are indexing mrp_per_unit column of pricings table
    float :pricing do
      pricing.mrp_per_unit
    end
    # I was trying to make multiple range filter work with join but not succeed so I have raised an issue on github sunspot repo https://github.com/sunspot/sunspot/issues/770
    # join(:mrp_per_unit, :target => Pricing, :type => :float, :join => { :from => :product_id, :to => :id })
  end
end

Pricing Model :
class Pricing < ActiveRecord::Base
  belongs_to :product
  searchable do
    integer :product_id
    float :mrp_per_unit
  end
end

Search Controller :

@search = Product.search do      
    any_of do
        # You can write more or can make it dynamic as per your requirement
        # It is only for example that will help you in understanding that
        # In that way you will see the whole data filtered as per the specified ranges
        with(:pricing, 1..100)
        with(:pricing, 101..200) 
        with(:pricing, 201..300) 
        with(:pricing, 301..400) 
        with(:pricing, 401..500) 
end
end
@search.results


You can find out the case in brief here at my github issue

https://github.com/sunspot/sunspot/issues/770

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.