Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sunspot Spatial Search Not Returning Results

I've just implemented the Sunspot gem into my application and I really like it except for the fact that when I do a location search it seems to be excluding some results. For example: I live in Columbus Ohio so if I search for "Columbus Ohio" my application translates that into a lat/lng and I do:

@search = (Skatepark.search {

  with(:coordinates).near lat, lng, :precision => 3
  fulltext text
  paginate :page => params[:page], :per_page => 15

})

This returns some records that are geocoded on the west side of columbus but none of my records that I have in my DB that are on the east side. Am I doing something wrong w/ my search?

You can try it out for yourself at http://skateparks.co/search

If you search for "Columbus Ohio" you'll get totally different results than if you search for "Lancaster Ohio" which is only a few miles to the southeast.

like image 717
Kyle Decot Avatar asked Oct 11 '22 04:10

Kyle Decot


1 Answers

This is because Sunspot depends on gem 'pr_geohash' which generates the geospatial index.

A geohash is a form of z-order curve.

You are attempting to solve a nearest neighbor problem using this index, which by its nature can only produce approximate results. The author would have chosen this approach since Solr is designed to deal with large datasets, which suffer from the curse of dimensionality.

Depending on your requirements, perhaps you should try

  1. a siloless solution eg. Freebase (there are several gems)
  2. a saas solution
  3. a spatial database,
  4. a geodatabase,
  5. or your own approach?

Proposed Solution

Use geojson objects for cities, towns, & villages rather than plain lat/long. Use semantic autocompletion so that users can select the polygon directly from Freebase. (Interface example: Quora search box) This way, all results in the requested city will be returned as you are now doing a polygonal bounding search rather than a radial search.

I urge you to make your data available as Freebase Locations where the parent is a City/Town/Village if this is at all possible given your business model. Turns out there is both a location type and a location topic all ready for you.

Update 1

I notice that you're on Heroku.

If you have a dedicated db you could use PostGIS.

If not, read How do you do GIS queries on Heroku using the shared database?

like image 173
Alec Wenzowski Avatar answered Oct 12 '22 19:10

Alec Wenzowski