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 58cad07aacc7b3524b4c2b5dd2834a9972d96e6f..29da5bcdbd910c7c9ff067638e1114e36edc0b5e 100644 --- a/lib/geocoder/models/mongo_base.rb +++ b/lib/geocoder/models/mongo_base.rb @@ -16,7 +16,8 @@ module Geocoder :geocode => true, :user_address => address_attr, :coordinates => options[:coordinates] || :coordinates, - :geocode_block => block + :geocode_block => block, + :skip_index => options[:skip_index] || false ) end @@ -28,7 +29,8 @@ module Geocoder :reverse_geocode => true, :fetched_address => options[:address] || :address, :coordinates => coordinates_attr, - :reverse_block => block + :reverse_block => block, + :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 2703f4f91209391fa7d7c6f2df5a9fb71281efc3..9fb801fce071be58eb17da4dc5fbe63ee184ee8f 100644 --- a/test/mongoid_test.rb +++ b/test/mongoid_test.rb @@ -19,4 +19,9 @@ class MongoidTest < Test::Unit::TestCase p = Place.near(location) assert_equal p.selector[:location]['$nearSphere'], location.reverse 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