Skip to content

Instantly share code, notes, and snippets.

@sumskyi
Created February 24, 2011 10:11
Show Gist options
  • Select an option

  • Save sumskyi/842013 to your computer and use it in GitHub Desktop.

Select an option

Save sumskyi/842013 to your computer and use it in GitHub Desktop.
app/models/location.rb
class Location < ActiveRecord::Base
searchable do
integer :id
spellchk :name
latlon :lat
latlon :lon
string :geohash
end
class << self
# r = Location.by_keyword_and_radius 29.72322024726690, -95.42722710312336, 'restoran', 100
# r.results
def by_keyword_and_radius(lat, lon, keyword, radius)
@search = Sunspot.search(Location) do
adjust_solr_params do |p|
p.delete(:fq)
p.delete(:qf)
p.delete(:defType)
p.delete(:fl)
p[:qt] = 'geo'
p[:spellcheck] = 'true'
p[:sort] = 'geo_distance asc'
p[:lat] = lat
p[:long] = lon
p[:radius] = radius
p[:q] = keyword
end
end
use_fuzzy
@search
end
private
def use_fuzzy
@raw_results = @search.instance_variable_get(:@solr_result)
logger.info(@raw_results.inspect)
response = @raw_results['response']
fuzzy = @raw_results['fuzzy']
@raw_results['response'] = fuzzy if response['numFound'].zero?
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment