diff --git a/README.rdoc b/README.rdoc index 883788e5d89a199436bd6e90be123ce9f86b1a77..eafabe84c00d53d3e59d8ae76668e018041334f0 100644 --- a/README.rdoc +++ b/README.rdoc @@ -83,6 +83,12 @@ Be sure to read <i>Latitude/Longitude Order</i> in the <i>Notes on MongoDB</i> s MongoMapper is very similar to Mongoid, just be sure to include <tt>Geocoder::Model::MongoMapper</tt>. +=== MongoMapper, Mongoid Indexes + +By default the methods geocoded_by and reverse_geocoded_by will create a geospatial index. If you want to skip this option call the methids with the :skip_index option + include Geocoder::Model::Mongoid + geocoded_by :address, :skip_index => true + === Bulk Geocoding If you have just added geocoding to an existing application with a lot of objects you can use this Rake task to geocode them all: diff --git a/lib/geocoder/models/mongo_base.rb b/lib/geocoder/models/mongo_base.rb index b64443ab9abd237a22e65e7a4221bc884a343342..fe7a0e1f5fe968b1178aaed8b655b2087816cc42 100644 --- a/lib/geocoder/models/mongo_base.rb +++ b/lib/geocoder/models/mongo_base.rb @@ -18,7 +18,8 @@ module Geocoder :coordinates => options[:coordinates] || :coordinates, :geocode_block => block, :units => options[:units], - :method => options[:method] + :method => options[:method], + :skip_index => options[:skip_index] || false ) end @@ -31,8 +32,9 @@ module Geocoder :fetched_address => options[:address] || :address, :coordinates => coordinates_attr, :reverse_block => block, - :units => options[:units], - :method => options[:method] + :units => options[:units], + :method => options[:method], + :skip_index => options[:skip_index] || false ) end diff --git a/lib/geocoder/models/mongo_mapper.rb b/lib/geocoder/models/mongo_mapper.rb index a093f6bf056a1418ce422c38d7c80994ae3a1011..17d83402e71e78fe65a4e97386d0582e163509fb 100644 --- a/lib/geocoder/models/mongo_mapper.rb +++ b/lib/geocoder/models/mongo_mapper.rb @@ -16,8 +16,10 @@ module Geocoder def geocoder_init(options) super(options) - ensure_index [[ geocoder_options[:coordinates], Mongo::GEO2D ]], - :min => -180, :max => 180 # create 2d index + if options[:skip_index] == false + ensure_index [[ geocoder_options[:coordinates], Mongo::GEO2D ]], + :min => -180, :max => 180 # create 2d index + end end end end diff --git a/lib/geocoder/models/mongoid.rb b/lib/geocoder/models/mongoid.rb index e3ca53a3254c38ca9145f1db221727a2727c5486..a7124e5bfd0681c36de543f90d96f52911c096c0 100644 --- a/lib/geocoder/models/mongoid.rb +++ b/lib/geocoder/models/mongoid.rb @@ -16,8 +16,10 @@ module Geocoder def geocoder_init(options) super(options) - index [[ geocoder_options[:coordinates], Mongo::GEO2D ]], - :min => -180, :max => 180 # create 2d index + if options[:skip_index] == false + index [[ geocoder_options[:coordinates], Mongo::GEO2D ]], + :min => -180, :max => 180 # create 2d index + end end end end diff --git a/test/mongoid_test.rb b/test/mongoid_test.rb index 29c015ecbf40b6dd10c5197730570513de92ba27..1d334aa7b81fb2a2c6cffe90c4475e2164474ce5 100644 --- a/test/mongoid_test.rb +++ b/test/mongoid_test.rb @@ -30,4 +30,9 @@ class MongoidTest < Test::Unit::TestCase Place.geocoded_by :address, :coordinates => :location, :units => :mi assert_equal 69, p.distance_to([0,1]).round end + + def test_index_is_skipped_if_skip_option_flag + result = PlaceWithoutIndex.index_options.keys.flatten[0] == :coordinates + assert_equal result, false + end end diff --git a/test/mongoid_test_helper.rb b/test/mongoid_test_helper.rb index 68c2af8288b592d20e417981ca69ce3d44b71234..572db2b985bfb12bd0861845f3df088cb5cf5680 100644 --- a/test/mongoid_test_helper.rb +++ b/test/mongoid_test_helper.rb @@ -29,3 +29,11 @@ class Place write_attribute :address, address end end + +class PlaceWithoutIndex + include Mongoid::Document + include Geocoder::Model::Mongoid + + field :location, :type => Array + geocoded_by :location, :skip_index => true +end