Created
December 11, 2012 18:05
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 ;-)