Permalink
Browse files

add search_handler medthod to define which use with solr

  • Loading branch information...
1 parent 1ec94dd commit 2c5aae4475877d224ce96b57572982a9d43bedcf Jean-Hugues Pinson committed May 25, 2012
Showing with 31 additions and 11 deletions.
  1. +12 −4 haystack/backends/__init__.py
  2. +13 −6 haystack/backends/solr_backend.py
  3. +6 −1 haystack/query.py
@@ -107,7 +107,7 @@ def search(self, query_string, sort_by=None, start_offset=0, end_offset=None,
fields='', highlight=False, facets=None, date_facets=None, query_facets=None,
narrow_queries=None, spelling_query=None, within=None,
dwithin=None, distance_point=None, models=None,
- limit_to_registered_models=None, result_class=None, **kwargs):
+ limit_to_registered_models=None, result_class=None, search_handler=None,**kwargs):
"""
Takes a query to search on and returns dictionary.
@@ -289,6 +289,7 @@ def __init__(self, using=DEFAULT_ALIAS):
self.date_facets = {}
self.query_facets = []
self.narrow_queries = set()
+ self.search_handler = None
#: If defined, fields should be a list of field names - no other values
#: will be retrieved so the caller must be careful to include django_ct
#: and django_id when using code which expects those to be included in
@@ -337,7 +338,7 @@ def build_params(self, spelling_query=None):
kwargs = {
'start_offset': self.start_offset,
}
-
+
if self.order_by:
kwargs['sort_by'] = self.order_by
@@ -358,7 +359,10 @@ def build_params(self, spelling_query=None):
if self.narrow_queries:
kwargs['narrow_queries'] = self.narrow_queries
-
+
+ if self.search_handler:
+ kwargs['search_handler'] = self.search_handler
+
if spelling_query:
kwargs['spelling_query'] = spelling_query
@@ -751,7 +755,10 @@ def add_narrow_query(self, query):
Generally used in conjunction with faceting.
"""
self.narrow_queries.add(query)
-
+
+ def set_search_handler(self,sh):
+ self.search_handler = sh
+
def set_result_class(self, klass):
"""
Sets the result class to use for results.
@@ -831,6 +838,7 @@ def _clone(self, klass=None, using=None):
clone.distance_point = self.distance_point.copy()
clone._raw_query = self._raw_query
clone._raw_query_params = self._raw_query_params
+ clone.search_handler = self.search_handler
return clone
@@ -110,13 +110,12 @@ def search(self, query_string, sort_by=None, start_offset=0, end_offset=None,
fields='', highlight=False, facets=None, date_facets=None, query_facets=None,
narrow_queries=None, spelling_query=None, within=None,
dwithin=None, distance_point=None, models=None,
- limit_to_registered_models=None, result_class=None, **kwargs):
+ limit_to_registered_models=None, result_class=None, search_handler=None, **kwargs):
if len(query_string) == 0:
return {
'results': [],
'hits': 0,
}
-
kwargs = {
'fl': '* score',
}
@@ -209,7 +208,10 @@ def search(self, query_string, sort_by=None, start_offset=0, end_offset=None,
if narrow_queries is not None:
kwargs['fq'] = list(narrow_queries)
-
+
+ if search_handler is not None:
+ kwargs['qt'] = search_handler
+
if within is not None:
from haystack.utils.geo import generate_bounding_box
@@ -607,7 +609,8 @@ def run(self, spelling_query=None, **kwargs):
'result_class': self.result_class,
}
order_by_list = None
-
+
+
if self.order_by:
if order_by_list is None:
order_by_list = []
@@ -634,10 +637,14 @@ def run(self, spelling_query=None, **kwargs):
if self.query_facets:
search_kwargs['query_facets'] = self.query_facets
-
+
if self.narrow_queries:
search_kwargs['narrow_queries'] = self.narrow_queries
-
+
+ if self.search_handler:
+ search_kwargs['search_handler'] = self.search_handler
+
+
if self.fields:
search_kwargs['fields'] = self.fields
View
@@ -394,7 +394,12 @@ def narrow(self, query):
clone = self._clone()
clone.query.add_narrow_query(query)
return clone
-
+
+ def search_handler(self, sh):
+ clone = self._clone()
+ clone.query.set_search_handler(sh)
+ return clone
+
def raw_search(self, query_string, **kwargs):
"""Passes a raw query directly to the backend."""
return self.filter(content=Raw(query_string, **kwargs))

0 comments on commit 2c5aae4

Please sign in to comment.