ruby-on-railselasticsearchrails-geocoder

Rails scoping elasticsearch using geocoded location


I have an app that uses Geocoder to find Vendors within proximity of the User. I'm currently using ElasticSearch in another model, but I'd like to extend that to my Vendor model. The only thing is, I don't want users to search through all vendors, only those within x distance from them. Is something like this possible? I've looked through some of the elastic search documentation and haven't found anything yet. Here's my Vendor model and my devise User model:

class Vendor < ActiveRecord::Base


    geocoded_by :address
    after_validation :geocode,
        :if => lambda{ |obj| obj.address_changed? }
end

class User < ActiveRecord::Base

   devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

   validates :zip, presence: true

   geocoded_by :zip

   after_validation :geocode

end

Any thoughts on this? Open to any suggestions as I'm unsure of how to accomplish this. Thanks in advance!


Solution

  • I suppose that you would have indexed Vendor documents in your elasticsearch index. If not, then you need to do so along with storing the location co-ordinates.

    From your question, I understand that you want to search all the vendors in the nearby location of the user. If that is the case, there is a geo location query that you can use in elasticsearch.

    You can use one or any no. of coordinates (lat and long) along with x distance in your search query. Here are some of the useful links that might help you:

    http://www.elasticsearch.org/blog/geo-location-and-search/ http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-geo-distance-filter.html