From 84c6e3b5428309b60a947b153ec2aa588c53414c Mon Sep 17 00:00:00 2001 From: noobie Date: Mon, 1 Aug 2016 10:21:43 -0400 Subject: [PATCH] Added support for text indexing. Slight format change in query filters to allow the class to be more extensible in the future with class variable `ALLOWED_DIRECTIONS`. --- txmongo/filter.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/txmongo/filter.py b/txmongo/filter.py index 8ec95ed..fd00417 100644 --- a/txmongo/filter.py +++ b/txmongo/filter.py @@ -50,7 +50,18 @@ def GEOHAYSTACK(keys): return _direction(keys, "geoHaystack") +def TEXT(keys): + """ + Text-based index + https://docs.mongodb.com/manual/core/index-text/ + """ + return _direction(keys, "text") + + class _QueryFilter(defaultdict): + + ALLOWED_DIRECTIONS = (1, -1, '2d', '2dsphere', 'geoHaystack', 'text',) + def __init__(self): defaultdict.__init__(self, lambda: ()) @@ -68,12 +79,15 @@ def _index_document(self, operation, index_list): assert isinstance(index_list, (list, tuple)) for key, direction in index_list: if not isinstance(key, (bytes, unicode)): - raise TypeError("TxMongo: invalid {0}ing key '{1}'".format(name, repr(key))) - if direction not in (1, -1, "2d", "2dsphere", "geoHaystack"): - raise TypeError("TxMongo invalid {0}ing direction '{1}'".format(name, direction)) + raise TypeError("TxMongo: invalid {0}ing key '{1}'" + .format(name, repr(key))) + if direction not in self.__class__.ALLOWED_DIRECTIONS: + raise TypeError("TxMongo invalid {0}ing direction '{1}'" + .format(name, direction)) self[operation] += tuple(((key, direction),)) except Exception: - raise TypeError("TxMongo: invalid list of keys for {0}, {1}".format(name, repr(index_list))) + raise TypeError("TxMongo: invalid list of keys for {0}, {1}" + .format(name, repr(index_list))) def __repr__(self): return "" % dict.__repr__(self)