Search code examples
ruby-on-rails-4solrgeospatialsunspot-solr

Model.solr_search not working for geospatial filter


I have configured sunspot-2.1.1.

Problem

  • I want to search All products within 20 miles radius and below is my code to do that for Product model

             Product.search do
                fulltext(params[:search][:keyword])
                order_by_geodist(:location, current_user.lat, current_user.lng)
                with(:location).in_radius(current_user.lat, current_user.lng, mile_to_km)
    
                paginate page: params[:page], per_page: GlobalConstant::SEARCH_RESULT_PER_PAGE
              end
    

NOTE: above code is working properly in development environment it is not workingin production envionment. I have tried reindexing solr also, but no luck :(

And I have added below code in other than search page where I am displaying radius based products lists and it is working:

             Sunspot.search [Product] do
                without(:sale_status, 'draft')
                with(:user_id, current_user.id)
                unless current_user.location.blank?
                  order_by_geodist(:location, current_user.lat, current_user.lng)
                  with(:location).in_radius(current_user.lat, current_user.lng, (mile_to_km))
                end

                paginate page: params[:page], per_page: GlobalConstant::TIMELINE_PER_PAGE
              end

What am I missing, why is it not working in production environment and is working in development environment.?

Thanks


Solution

  • I am really sorry to open this issue, I am stupid who have created searches_controller_backup.rb which had old code that was not configured with geospatial filter functionality.

    The problem was everything was working but because rails was loading searches_controller codes from backup file, it was suppose to looks like it was not working.

    With that note, above both codes are working perfectly without any issue....so Model.solr_search have nothing to do with geospatial filter not working.

    Removing backup file or changing extension to other than .rb fixed my problem