Skip to content

Commit

Permalink
Allow geo indexing/search on types other than Float
Browse files Browse the repository at this point in the history
E.g. BigDecimal etc. -- the GeoHash library enforces Floats, so cast
arguments to_f before encoding.
  • Loading branch information
Mat Brown committed Aug 24, 2010
1 parent f49469d commit df0c792
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 2 deletions.
2 changes: 1 addition & 1 deletion sunspot/lib/sunspot/query/geo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Geo

def initialize(field, lat, lng, options)
@field, @options = field, options
@geohash = encode(lat, lng, MAX_PRECISION)
@geohash = encode(lat.to_f, lng.to_f, MAX_PRECISION)
end

def to_params
Expand Down
2 changes: 1 addition & 1 deletion sunspot/lib/sunspot/type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ def indexed_name(name)
end

def to_indexed(value)
encode(value.lat, value.lng, 12)
encode(value.lat.to_f, value.lng.to_f, 12)
end
end

Expand Down
8 changes: 8 additions & 0 deletions sunspot/spec/api/indexer/attributes_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require File.join(File.dirname(__FILE__), 'spec_helper')
require 'bigdecimal'

describe 'indexing attribute fields', :type => :indexer do
it 'should correctly index a stored string attribute field' do
Expand Down Expand Up @@ -90,6 +91,13 @@
connection.should have_add_with(:coordinates_s => 'dr5xx3nytvgs')
end

it 'should index latitude and longitude passed as non-Floats' do
coordinates = Sunspot::Util::Coordinates.new(
BigDecimal.new('40.7'), BigDecimal.new('-73.5'))
session.index(post(:coordinates => coordinates))
connection.should have_add_with(:coordinates_s => 'dr5xx3nytvgs')
end

it 'should correctly index an attribute field with block access' do
session.index(post(:title => 'The Blog Post'))
connection.should have_add_with(:sort_title_s => 'blog post')
Expand Down
8 changes: 8 additions & 0 deletions sunspot/spec/api/query/geo_examples.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require File.join(File.dirname(__FILE__), 'spec_helper')
require 'bigdecimal'

shared_examples_for 'geohash query' do
it 'searches for nearby points with defaults' do
Expand All @@ -8,6 +9,13 @@
connection.should have_last_search_including(:q, build_geo_query)
end

it 'searches for nearby points with non-Float arguments' do
search do
with(:coordinates).near(BigDecimal.new('40.7'), BigDecimal.new('-73.5'))
end
connection.should have_last_search_including(:q, build_geo_query)
end

it 'searches for nearby points with given precision' do
search do
with(:coordinates).near(40.7, -73.5, :precision => 10)
Expand Down

0 comments on commit df0c792

Please sign in to comment.