Merge branch 'geo2d' into dev

This commit is contained in:
Ross Lawley 2012-02-24 10:28:52 +00:00
commit 296ef5bddf
3 changed files with 44 additions and 7 deletions

View File

@ -431,11 +431,26 @@ If a dictionary is passed then the following options are available:
:attr:`unique` (Default: False) :attr:`unique` (Default: False)
Whether the index should be sparse. Whether the index should be sparse.
.. note::
Geospatial indexes
---------------------------
Geospatial indexes will be automatically created for all Geospatial indexes will be automatically created for all
:class:`~mongoengine.GeoPointField`\ s :class:`~mongoengine.GeoPointField`\ s
It is also possible to explicitly define geospatial indexes. This is
useful if you need to define a geospatial index on a subfield of a
:class:`~mongoengine.DictField` or a custom field that contains a
point. To create a geospatial index you must prefix the field with the
***** sign. ::
class Place(Document):
location = DictField()
meta = {
'indexes': [
'*location.point',
],
}
Ordering Ordering
======== ========
A default ordering can be specified for your A default ordering can be specified for your

View File

@ -399,11 +399,13 @@ class QuerySet(object):
index_list = [] index_list = []
use_types = doc_cls._meta.get('allow_inheritance', True) use_types = doc_cls._meta.get('allow_inheritance', True)
for key in spec['fields']: for key in spec['fields']:
# Get direction from + or - # Get ASCENDING direction from +, DESCENDING from -, and GEO2D from *
direction = pymongo.ASCENDING direction = pymongo.ASCENDING
if key.startswith("-"): if key.startswith("-"):
direction = pymongo.DESCENDING direction = pymongo.DESCENDING
if key.startswith(("+", "-")): elif key.startswith("*"):
direction = pymongo.GEO2D
if key.startswith(("+", "-", "*")):
key = key[1:] key = key[1:]
# Use real field name, do it manually because we need field # Use real field name, do it manually because we need field
@ -421,7 +423,7 @@ class QuerySet(object):
# If _types is being used, prepend it to every specified index # If _types is being used, prepend it to every specified index
index_types = doc_cls._meta.get('index_types', True) index_types = doc_cls._meta.get('index_types', True)
allow_inheritance = doc_cls._meta.get('allow_inheritance') allow_inheritance = doc_cls._meta.get('allow_inheritance')
if spec.get('types', index_types) and allow_inheritance and use_types: if spec.get('types', index_types) and allow_inheritance and use_types and direction is not pymongo.GEO2D:
index_list.insert(0, ('_types', 1)) index_list.insert(0, ('_types', 1))
spec['fields'] = index_list spec['fields'] = index_list

View File

@ -633,6 +633,26 @@ class DocumentTest(unittest.TestCase):
BlogPost.drop_collection() BlogPost.drop_collection()
def test_explicit_geo2d_index(self):
"""Ensure that geo2d indexes work when created via meta[indexes]
"""
class Place(Document):
location = DictField()
meta = {
'indexes': [
'*location.point',
],
}
Place.drop_collection()
info = Place.objects._collection.index_information()
# Indexes are lazy so use list() to perform query
list(Place.objects)
info = Place.objects._collection.index_information()
info = [value['key'] for key, value in info.iteritems()]
self.assertTrue([('location.point', '2d')] in info)
def test_dictionary_indexes(self): def test_dictionary_indexes(self):
"""Ensure that indexes are used when meta[indexes] contains dictionaries """Ensure that indexes are used when meta[indexes] contains dictionaries
instead of lists. instead of lists.