Commits

Jason R. Coombs committed 66d230c

Added support for MongoDB full text search.

Comments (0)

Files changed (3)

+1106.1
+======
+
+* Added MongoDBFullTextLogger, leveraging MongoDB Full Text Search on MongoDB
+  2.4 or later (if enabled).
+
 1106
 ====
 
 
 import pytz
 import six
+from jaraco.util.context import ExceptionTrap
 
 import pmxbot
 from . import storage
 		message['datetime'] = self._fmt_date(dt)
 		self.db.insert(message)
 
+class FullTextMongoDBLogger(MongoDBLogger):
+	@classmethod
+	def uri_matches(cls, uri):
+		"""
+		override 'uri_matches' to disallow this logger if full text searching
+		is not available.
+		"""
+		return (
+			super(FullTextMongoDBLogger, cls).uri_matches(uri)
+			and cls._has_fulltext(uri)
+		)
+
+	@classmethod
+	def _has_fulltext(cls, uri):
+		"""
+		Enable full text search on the messages if possible and return True.
+		If the full text search cannot be enabled, then return False.
+		"""
+		coll = cls._get_collection(uri)
+		with ExceptionTrap(storage.pymongo.errors.OperationFailure) as trap:
+			coll.ensure_index([('message', 'text')], background=True)
+		return not trap
+
+	def search(self, *terms):
+		query = ' '.join(terms)
+		db = self.db.database
+		collection_name = db.name
+		resp = db.command('text', collection_name, search=query)
+		return (res['obj'] for res in resp['results'])
+
+
 def first(iterable):
 	"""
 	Return the first element from the iterable or None if no element is
 		"feedparser",
 		"pytz",
 		"wordnik>=2.1.2,<3.0",
-		"jaraco.util>=6.6,<9dev",
+		"jaraco.util>=8.8,<9dev",
 		"beautifulsoup4",
 		#for viewer
 		"jinja2",