diff --git a/README.rst b/README.rst index 66e57c5..d91f9ff 100644 --- a/README.rst +++ b/README.rst @@ -52,8 +52,12 @@ The following is some example usage:: }, mode='SPH_MATCH_ALL', rankmode='SPH_RANK_NONE', + maxmatches=1000, # 1000 is the default value + passages=..., + passages_opts=... ) + queryset = MyModel.search.query('query') results1 = queryset.order_by('@weight', '@id', 'my_attribute') results2 = queryset.filter(my_attribute=5) @@ -177,4 +181,4 @@ Resources --------- * http://groups.google.com/group/django-sphinx -* http://www.davidcramer.net/code/65/setting-up-django-with-sphinx.html \ No newline at end of file +* http://www.davidcramer.net/code/65/setting-up-django-with-sphinx.html diff --git a/djangosphinx/models.py b/djangosphinx/models.py index 5812ba1..919cbca 100644 --- a/djangosphinx/models.py +++ b/djangosphinx/models.py @@ -5,7 +5,6 @@ import warnings import operator import apis.current as sphinxapi -import logging import re try: import decimal @@ -13,6 +12,7 @@ from django.utils import _decimal as decimal # for Python 2.3 from django.db.models.query import QuerySet, Q +from django.utils.log import getLogger from django.conf import settings __all__ = ('SearchError', 'ConnectionError', 'SphinxSearch', 'SphinxRelation', 'SphinxQuerySet') @@ -40,6 +40,8 @@ UNDEFINED = object() +logger = getLogger('djangosphinx.models') + class SearchError(Exception): pass class ConnectionError(Exception): pass @@ -423,7 +425,9 @@ def _get_data(self): return self._result_cache def _get_sphinx_results(self): - assert(self._offset + self._limit <= self._maxmatches) + assert(self._offset + self._limit <= self._maxmatches),\ + "Slice length can't be greated than SphinxQuerySet._maxmatches `%i`" %\ + self._maxmatches client = self._get_sphinx_client() @@ -480,6 +484,7 @@ def _handle_filters(filter_list, exclude=False): args = (name, values[0], values[1], exclude) else: raise NotImplementedError, 'Related object and/or field lookup "%s" not supported' % lookup + if is_float: client.SetFilterFloatRange(*args) elif not exclude and self.model and name == self.model._meta.pk.column: @@ -540,7 +545,7 @@ def _handle_filters(filter_list, exclude=False): elif not results['matches']: results = EMPTY_RESULT_SET - logging.debug('Found %s results for search query %s on %s with params: %s', results['total'], self._query, self._index, ', '.join(params)) + logger.debug('Found %s results for search query %s on %s with params: %s', results['total'], self._query, self._index, ', '.join(params)) return results @@ -667,7 +672,11 @@ class EmptySphinxQuerySet(SphinxQuerySet): def _get_sphinx_results(self): return None + class SphinxModelManager(object): + """A special Manager through which sphinx queries are provided to Django + models. + """ def __init__(self, model, **kwargs): self.model = model self._index = kwargs.pop('index', model._meta.db_table) @@ -708,7 +717,12 @@ def update(self, **kwargs): assert sphinxapi.VER_COMMAND_SEARCH >= 0x113, "You must upgrade sphinxapi to version 0.98 to use UpdateAttributes." sphinxapi.UpdateAttributes(self._index, kwargs.keys(), dict(self.instance.pk, map(to_sphinx, kwargs.values()))) + class SphinxSearch(object): + """A descriptor used to get the appropiate manager in case the attribute + is accessed from a model class or a model instance. + """ + def __init__(self, index=None, using=None, **kwargs): self._kwargs = kwargs self._sphinx = None @@ -740,6 +754,7 @@ def contribute_to_class(self, model, name, **kwargs): model.__sphinx_indexes__.append(self._index) setattr(model, name, self._sphinx) + class SphinxRelationProxy(SphinxProxy): def count(self): return min(self._sphinx['attrs']['@count'], self._maxmatches)