Permalink
Browse files

Testable searchable sphinx

  • Loading branch information...
1 parent 125c8c1 commit 1ff31408323637bb833e5dd0112670b4bab22cb7 @davedash davedash committed with fwenzel Jul 4, 2010
View
No changes.
Oops, something went wrong.
View
@@ -0,0 +1,57 @@
+import socket
+import time
+
+from django.conf import settings
+
+import sphinxapi as sphinx
+from reporter.utils import crc32, manual_order
+from feedback.models import Opinion
+
+
+class SearchError(Exception):
+ pass
+
+
+class Client():
+
+ def __init__(self):
+ self.sphinx = sphinx.SphinxClient()
+ self.sphinx.SetServer(settings.SPHINX_HOST, settings.SPHINX_PORT)
+
+ def query(self, term, limit=10, offset=0, **kwargs):
+ """Submits formatted query, retrieves ids, returns Opinions."""
+ sc = self.sphinx
+
+ if isinstance(kwargs.get('product'), int):
+ sc.SetFilter('product', (kwargs['product'],))
+
+ if kwargs.get('version'):
+ sc.SetFilter('version', (crc32(kwargs['version']),))
+
+ if isinstance(kwargs.get('positive'), int):
+ sc.SetFilter('positive', (kwargs['positive'], ))
+
+ if kwargs.get('os'):
+ sc.SetFilter('os', (crc32(kwargs['os']),))
+
+ if kwargs.get('locale'):
+ sc.SetFilter('locale', (crc32(kwargs['locale']),))
+
+ if kwargs.get('date_end') and kwargs.get('date_start'):
+ start = int(time.mktime(kwargs['date_start'].timetuple()))
+ end = int(time.mktime(kwargs['date_end'].timetuple()))
+ sc.SetFilterRange('created', start, end)
+
+ try:
+ result = sc.Query(term, 'opinions')
+ except socket.timeout:
+ raise SearchError("Query has timed out.")
+ except Exception, e:
+ raise SearchError("Sphinx threw an unknown exception: %s" % e)
+
+ if sc.GetLastError():
+ raise SearchError(sc.GetLastError())
+
+
+ opinion_ids = [m['id'] for m in result['matches']]
+ return manual_order(Opinion.objects.all(), opinion_ids)
Oops, something went wrong.

0 comments on commit 1ff3140

Please sign in to comment.