Skip to content

Instantly share code, notes, and snippets.

@nz
Created December 11, 2012 18:05
Show Gist options
  • Save nz/4260708 to your computer and use it in GitHub Desktop.
Save nz/4260708 to your computer and use it in GitHub Desktop.
Adapting Sunspot pre-2.0 spatial search on Solr 1.4 for Solr 3 spatial search api
<schema>
<types>
<!-- add a location fieldtype, which splits a string into component subfields -->
<fieldType name="location" class="solr.LatLonType" subFieldSuffix="_coordinate"/>
</types>
<fields>
<!-- add the location field, and the coordinate subtype -->
<field name="location" type="location" indexed="true" />
<dynamicField name="*_coordinate" type="tdouble" indexed="true" stored="false"/>
</fields>
</schema>
# index the location as a "lat,lng" string
searchable do
string(:location, :as => :location) { [lat,lng].join(",") }
end
# change your searches from this...
search do
with(:lat_lng).near(lat, lng, :precision => foo)
end
# ...to this
search do
adjust_solr_params do |p|
p['fq'] = "{!geofilt sfield=location pt=#{lat,lng} d=#{params[:distance]}}"
end
end
# ...or this
search do
adjust_solr_params do |p|
p['fq'] = '{!geofilt}'
p['sfield'] = 'location'
p['pt'] = [lat,lng].join(',')
p['d'] = params[:distance] # kilometers
end
end
@nz
Copy link
Author

nz commented Dec 11, 2012

disclaimer: these are untested notes written off the top of my head. useful as a place to start your own research, but your mileage may vary. feedback welcome ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment