Permalink
Browse files

Object query manager for searchindex

  • Loading branch information...
1 parent de41dab commit a9c00cb6100900cd6bbf3745cd0e5f191941b382 @MadanThangavelu MadanThangavelu committed with toastdriven Feb 26, 2012
Showing with 122 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +7 −0 haystack/indexes.py
  3. +111 −0 haystack/manager.py
  4. +3 −0 tests/core/tests/indexes.py
View
@@ -1,3 +1,4 @@
+.settings
*.pyc
.DS_Store
_build
View
@@ -6,6 +6,7 @@
from haystack import connections, connection_router
from haystack.constants import ID, DJANGO_CT, DJANGO_ID, Indexable, DEFAULT_ALIAS
from haystack.fields import *
+from haystack.manager import HaystackManager
from haystack.utils import get_identifier, get_facet_field_name
@@ -55,6 +56,12 @@ def __new__(cls, name, bases, attrs):
shadow_facet_field.set_instance_name(shadow_facet_name)
attrs['fields'][shadow_facet_name] = shadow_facet_field
+ # Assigning default 'objects' query manager
+ try:
+ attrs['objects'] = HaystackManager(attrs['meta'].index_label)
+ except KeyError:
+ attrs['objects'] = HaystackManager(DEFAULT_ALIAS)
+
return super(DeclarativeMetaclass, cls).__new__(cls, name, bases, attrs)
View
@@ -0,0 +1,111 @@
+'''
+Created on Feb 20, 2012
+
+@author: Madan Thangavelu
+'''
+from haystack.query import SearchQuerySet, EmptySearchQuerySet
+
+class HaystackManager(object):
+ def __init__(self, using=None):
+ super(HaystackManager, self).__init__()
+ self.using = using
+
+ def get_query_set(self):
+ """Returns a new SearchQuerySet object. Subclasses can override this method
+ to easily customize the behavior of the Manager.
+ """
+ return SearchQuerySet(using=self.using)
+
+ def get_empty_query_set(self):
+ return EmptySearchQuerySet(using=self.using)
+
+ def all(self):
+ return self.get_query_set()
+
+ def none(self):
+ return self.get_empty_query_set()
+
+ def filter(self, *args, **kwargs):
+ return self.get_query_set().filter(*args, **kwargs)
+
+ def exclude(self, *args, **kwargs):
+ return self.get_query_set().filter(*args, **kwargs)
+
+ def filter_and(self, *args, **kwargs):
+ return self.get_query_set().filter(*args, **kwargs)
+
+ def filter_or(self, *args, **kwargs):
+ return self.get_query_set().filter(*args, **kwargs)
+
+ def order_by(self, *args):
+ return self.get_query_set().filter(*args)
+
+ def order_by_distance(self, **kwargs):
+ return self.get_query_set().filter( **kwargs)
+
+ def highlight(self):
+ return self.get_query_set().filter()
+
+ def boost(self, term, boost):
+ return self.get_query_set().boost(term, boost)
+
+ def facet(self, field):
+ return self.get_query_set().facet(field)
+
+ def within(self, field, point_1, point_2):
+ return self.get_query_set().filter(field, point_1, point_2)
+
+ def dwithin(self, field, point, distance):
+ return self.get_query_set().dwithin(field, point, distance)
+
+ def distance(self, field, point):
+ return self.get_query_set().distance(field, point)
+
+ def date_facet(self, field, start_date, end_date, gap_by, gap_amount=1):
+ return self.get_query_set().date_facet(field, start_date, end_date, gap_by, gap_amount=1)
+
+ def query_facet(self, field, query):
+ return self.get_query_set().query_facet(field, query)
+
+ def narrow(self, query):
+ return self.get_query_set().narrow(query)
+
+ def raw_search(self, query_string, **kwargs):
+ return self.get_query_set().filter(query_string, **kwargs)
+
+ def load_all(self):
+ return self.get_query_set().load_all()
+
+ def auto_query(self, query_string, fieldname='content'):
+ return self.get_query_set().filter(query_string, fieldname=fieldname)
+
+ def autocomplete(self, **kwargs):
+ return self.get_query_set().autocomplete(**kwargs)
+
+ def using(self, connection_name):
+ return self.get_query_set().using(connection_name)
+
+ def count(self):
+ return self.get_query_set().count()
+
+ def best_match(self):
+ return self.get_query_set().best_match()
+
+ def latest(self, date_field):
+ return self.get_query_set().latest(date_field)
+
+ def more_like_this(self, model_instance):
+ return self.get_query_set().more_like_this(model_instance)
+
+ def facet_counts(self):
+ return self.get_query_set().facet_counts()
+
+ def spelling_suggestion(self, preferred_query=None):
+ return self.get_query_set().spelling_suggestion(preferred_query=None)
+
+ def values(self, *fields):
+ return self.get_query_set().values(*fields)
+
+ def values_list(self, *fields, **kwargs):
+ return self.get_query_set().values_list(*fields, **kwargs)
+
@@ -630,3 +630,6 @@ def test_float_integer_fields(self):
self.assertTrue(isinstance(self.yabmsi.fields['average_delay'], indexes.FloatField))
self.assertEqual(self.yabmsi.fields['average_delay'].null, False)
self.assertEqual(self.yabmsi.fields['average_delay'].index_fieldname, 'average_delay')
+
+
+

0 comments on commit a9c00cb

Please sign in to comment.