Skip to content
10 changes: 7 additions & 3 deletions haystack/admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from django.contrib.admin.options import ModelAdmin
from django.contrib.admin.views.main import (ChangeList, MAX_SHOW_ALL_ALLOWED,
SEARCH_VAR)
from django.contrib.admin.views.main import ChangeList, SEARCH_VAR
from django.core.exceptions import PermissionDenied, ImproperlyConfigured
from django.core.paginator import Paginator, InvalidPage
from django.shortcuts import render_to_response
Expand All @@ -22,6 +21,10 @@ def wraps(request, *args, **kwargs):

csrf_protect_m = method_decorator(csrf_protect)

try:
from django.contrib.admin.views.main import MAX_SHOW_ALL_ALLOWED
except ImportError:
pass

class SearchChangeList(ChangeList):
def get_results(self, request):
Expand All @@ -36,7 +39,8 @@ def get_results(self, request):
result_count = paginator.count
full_result_count = SearchQuerySet().models(self.model).all().count()

can_show_all = result_count <= MAX_SHOW_ALL_ALLOWED
can_show_all = result_count <= getattr(self, 'list_max_show_all', MAX_SHOW_ALL_ALLOWED)

multi_page = result_count > self.list_per_page

# Get the list of objects to display on this page.
Expand Down
59 changes: 54 additions & 5 deletions haystack/backends/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
import inspect
from copy import deepcopy
from time import time
from django.conf import settings
Expand All @@ -22,7 +23,6 @@
# A means to inspect all search queries that have run in the last request.
queries = []


# Per-request, reset the ghetto query log.
# Probably not extraordinarily thread-safe but should only matter when
# DEBUG = True.
Expand Down Expand Up @@ -55,6 +55,7 @@ def wrapper(obj, query_string, *args, **kwargs):
'additional_args': args,
'additional_kwargs': kwargs,
'time': "%.3f" % (stop - start),
'stacktrace': inspect.stack()
})

return wrapper
Expand Down Expand Up @@ -122,7 +123,7 @@ def clear(self, models=[], commit=True):
@log_query
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,
pivot_facets=None, narrow_queries=None, spelling_query=None,
limit_to_registered_models=None, result_class=None, **kwargs):
"""
Takes a query to search on and returns dictionary.
Expand Down Expand Up @@ -280,15 +281,20 @@ def __init__(self, site=None, backend=None):
self.order_by = []
self.models = set()
self.boost = {}
self.dismax = {}
self.start_offset = 0
self.end_offset = None
self.highlight = False
self.facets = set()
self.date_facets = {}
self.facet_mincount = None
self.facet_limit = None
self.facet_field_limit = {}
self.facet_prefix = None
self.facet_sort = None
self.query_facets = []
self.pivot_facets = set()
self.facet_pivot_mincount = None
self.narrow_queries = set()
self._raw_query = None
self._raw_query_params = {}
Expand Down Expand Up @@ -357,15 +363,27 @@ def build_params(self, spelling_query=None):

if self.query_facets:
kwargs['query_facets'] = self.query_facets

if self.pivot_facets:
kwargs['pivot_facets'] = list(self.pivot_facets)

if self.facet_mincount:
kwargs['facet_mincount'] = self.facet_mincount

if self.facet_limit:
kwargs['facet_limit'] = self.facet_limit

if self.facet_field_limit:
kwargs['facet_field_limit'] = self.facet_field_limit

if self.facet_prefix:
kwargs['facet_prefix'] = self.facet_prefix

if self.facet_sort:
kwargs['facet_sort'] = self.facet_sort

if self.facet_pivot_mincount:
kwargs['facet_pivot_mincount'] = self.facet_pivot_mincount

if self.narrow_queries:
kwargs['narrow_queries'] = self.narrow_queries
Expand Down Expand Up @@ -604,14 +622,24 @@ def add_order_by(self, field):
"""Orders the search result by a field."""
self.order_by.append(field)

def set_facet_mincount(self,mincount):
def set_facet_mincount(self, mincount):
self.facet_mincount = mincount

def set_facet_limit(self, limit):
self.facet_limit = limit

def set_facet_field_limit(self, field, limit):
facet_field = self.backend.site.get_facet_field_name(field)
self.facet_field_limit[facet_field] = limit

def set_facet_prefix(self,prefix):
def set_facet_prefix(self, prefix):
self.facet_prefix = prefix

def set_facet_sort(self, sort):
self.facet_sort = sort

def set_facet_pivot_mincount(self, mincount):
self.facet_pivot_mincount = mincount

def clear_order_by(self):
"""
Expand Down Expand Up @@ -660,7 +688,15 @@ def raw_search(self, query_string, **kwargs):
"""
self._raw_query = query_string
self._raw_query_params = kwargs


def add_dismax(self, **kwargs):
"""
Allows backends with support for "dismax" to perform enhanced user query parsing
"""

self.dismax = dict([(p, v) for p, v in kwargs.iteritems()
if p in getattr(self.backend, 'DISMAX_PARAMETERS', [])])

def more_like_this(self, model_instance):
"""
Allows backends with support for "More Like This" to return results
Expand Down Expand Up @@ -693,6 +729,13 @@ def add_date_facet(self, field, start_date, end_date, gap_by, gap_amount=1):
def add_query_facet(self, field, query):
"""Adds a query facet on a field."""
self.query_facets.append((self.backend.site.get_facet_field_name(field), query))

def add_pivot_facet(self, *args):
"""Adds pivot faceting to a query for the provided fields."""
facet_fields = []
for field in args:
facet_fields.append(self.backend.site.get_facet_field_name(field))
self.pivot_facets.add((','.join(facet_fields)))

def add_narrow_query(self, query):
"""
Expand Down Expand Up @@ -752,18 +795,24 @@ def _clone(self, klass=None):
clone.order_by = self.order_by[:]
clone.models = self.models.copy()
clone.boost = self.boost.copy()
clone.dismax = self.dismax.copy()
clone.highlight = self.highlight
clone.facets = self.facets.copy()
clone.date_facets = self.date_facets.copy()
clone.query_facets = self.query_facets[:]
clone.pivot_facets = self.pivot_facets.copy()
clone.facet_mincount = self.facet_mincount
clone.facet_limit = self.facet_limit
clone.facet_field_limit = self.facet_field_limit.copy()
clone.facet_prefix = self.facet_prefix
clone.facet_sort = self.facet_sort
clone.facet_pivot_mincount = self.facet_pivot_mincount
clone.narrow_queries = self.narrow_queries.copy()
clone.start_offset = self.start_offset
clone.end_offset = self.end_offset
clone.backend = self.backend
clone.result_class = self.result_class
clone._raw_query = self._raw_query
clone._raw_query_params = self._raw_query_params
clone._facet_counts = self._facet_counts
return clone
Loading