Skip to content

Commit

Permalink
add search_handler medthod to define which use with solr
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean-Hugues Pinson committed May 25, 2012
1 parent 1ec94dd commit 2c5aae4
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
16 changes: 12 additions & 4 deletions haystack/backends/__init__.py
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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

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


Expand Down
19 changes: 13 additions & 6 deletions haystack/backends/solr_backend.py
Expand Up @@ -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',
}
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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 = []
Expand All @@ -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

Expand Down
7 changes: 6 additions & 1 deletion haystack/query.py
Expand Up @@ -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))
Expand Down

0 comments on commit 2c5aae4

Please sign in to comment.