Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -177,4 +181,4 @@ Resources
---------

* http://groups.google.com/group/django-sphinx
* http://www.davidcramer.net/code/65/setting-up-django-with-sphinx.html
* http://www.davidcramer.net/code/65/setting-up-django-with-sphinx.html
21 changes: 18 additions & 3 deletions djangosphinx/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
import warnings
import operator
import apis.current as sphinxapi
import logging
import re
try:
import decimal
except ImportError:
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')
Expand Down Expand Up @@ -40,6 +40,8 @@

UNDEFINED = object()

logger = getLogger('djangosphinx.models')

class SearchError(Exception): pass
class ConnectionError(Exception): pass

Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down