2

I am considering the use of Thinking Sphinx, as I already have used ElasticSearch and would like to try something new.

In using ThinkingSphinx, how would one go about setting up a geo distance filter. There would be a User model containing the basic information of a user that includes their zip code. There a Locations model that would have the geographical information of the United States (zip code, latitude, longitude, state).

EXAMPLE: Current user “Michael” zip code is 30601. Michael types in the search form “programmer, video games”. The return results will show Users who have the words “programmer” or “video games” from a attribute in the User model that are located within 100 miles of Michael’s zip code 30601.

I have installed ThinkingSphinx, and on my app if I performed the search as detailed in the above example it will return the “programmer” or “video games” matches but only with users who have a 100% exact match to the zip code (it cancels out using geo dist). Now with the code I have I can perform a geo distance search using the zip code, which would returning surrounding Users. The geo distance doesn't seem to work when I factor in attributes from the User model with the zip code.

This was done with ease using Elastic in the past, but I wanted to see how Thinking Sphinx works. If someone has a clue with how this would look in the Searches controller, please

pwz2000
  • 1,385
  • 2
  • 16
  • 50

1 Answers1

1

From the perspective of Sphinx, zip codes are not useful - it all comes down to latitude and longitude.

So, if in your example Michael is current_user, you might have a search call looking something like this:

User.search 'programmer video games',
  :geo   => [current_user.latitude, current_user.longitude],
  :with  => {:geodist => 0.0..161_000.0},
  :order => 'geodist ASC'

Keep in mind this presumes you have latitude and longitude values stored in radians, not degrees. If they are in degrees, then you'll want to convert them in your index definition (as noted in the docs) and when you're searching as well (e.g. current_user.latitude * Math::PI / 180.0).

When filtering by distance, Sphinx uses metres - one mile is almost 1610 metres, hence the conversion in my example above.

pat
  • 16,116
  • 5
  • 40
  • 46