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
    # I was trying to make multiple range filter work with join but not succeed so I have raised an issue on github sunspot repo
    # join(:mrp_per_unit, :target => Pricing, :type => :float, :join => { :from => :product_id, :to => :id })

Pricing Model :

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

Search Controller :

@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)  

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

No comments:

Post a Comment