django haysteck FacetedSearchView返回空结果吗? [英] django haysteck FacetedSearchView returning empty results?
问题描述
我正在使用Django haystack FacetedSearchView我的 views.py
:
I'm using Django haystack FacetedSearchView my views.py
:
from haystack.generic_views import FacetedSearchView as BaseFacetedSearchView
class FacetedSearchView(BaseFacetedSearchView):
template_name = 'test.html'
facet_fields = ['source']
和 urls.py
中:
url(r'^search', FacetedSearchView.as_view(), name='haystack_search')
并进行测试.html我正在渲染构面。
and in test.html I'm rendering the facets.
发出请求时,事实
上下文对象的内容为空字典。但我认为它应该返回我在 facets_fields
中指定的所有构面,并且当我在请求的查询字符串中附加 q
参数时(具有任何值),它返回结果,但文档为零。是否需要提供 q
参数?
When I issue a request, the content of the factes
context object is empty dict. but I think it should return all the I specified facets in facets_fields
, and when I append q
parameter to the request's querystring (with any value) it returns result but with zero document. is it neccessary to provide the q
parameter? and with which value?
推荐答案
来解决需要覆盖FacetedSearchForm的搜索方法的问题,因为原始实现假定查询 q,但构面仅需要 facet_fields
即可工作。
to solve the issue on need to override the search method of FacetedSearchForm, because the original implementation assumes a query 'q' but faceting needs only facet_fields
to work.
from haystack.forms import FacetedSearchForm as BaseFacetedSearchForm
from haystack.generic_views import FacetedSearchView as BaseFacetedSearchView
class FacetedSearchForm(BaseFacetedSearchForm):
def __init__(self, *args, **kwargs):
self.selected_facets = kwargs.pop("selected_facets", [])
super(FacetedSearchForm, self).__init__(*args, **kwargs)
def search(self):
if not self.is_valid():
return self.no_query_found()
sqs = self.searchqueryset
# We need to process each facet to ensure that the field name and the
# value are quoted correctly and separately:
for facet in self.selected_facets:
if ":" not in facet:
continue
field, value = facet.split(":", 1)
if value:
sqs = sqs.narrow(u'%s:"%s"' % (field, sqs.query.clean(value)))
if self.load_all:
sqs = sqs.load_all()
return sqs
class FacetedSearchView(BaseFacetedSearchView):
template_name = 'facets.html'
facet_fields = []
form_class = FacetedSearchForm
这篇关于django haysteck FacetedSearchView返回空结果吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!