即使未插入搜索查询,我也希望显示与所选方面匹配的所有结果。与某些商店应用程序的工作方式类似亚马逊
e.g. Show all products which are "blue" and between $10-$100.
如果未指定搜索查询,Haystack不会返回任何值。
我有什么想法可以解决它吗?
谢谢!
答案 0 :(得分:12)
如果还有人在寻找,那么在haystack代码中建议使用简单的解决方案:
https://github.com/toastdriven/django-haystack/blob/master/haystack/forms.py#L34
class SearchForm(forms.Form):
def no_query_found(self):
"""
Determines the behavior when no query was found.
By default, no results are returned (``EmptySearchQuerySet``).
Should you want to show all results, override this method in your
own ``SearchForm`` subclass and do ``return self.searchqueryset.all()``.
"""
return EmptySearchQuerySet()
答案 1 :(得分:6)
我想你正在使用类似于haystack getting started documentation中的搜索模板。如果没有查询,则此视图不显示任何内容:
{% if query %}
{# Display the results #}
{% else %}
{# Show some example queries to run, maybe query syntax, something else? #}
{% endif %}
第二个问题是默认搜索表单的search()
方法实际上并不搜索任何内容,除非有查询。
为了解决这个问题,我正在使用自定义搜索表单。这是一个简短的样本:
class CustomSearchForm(SearchForm):
...
def search(self):
# First, store the SearchQuerySet received from other processing.
sqs = super(CustomSearchForm, self).search()
if not self.is_valid():
return sqs
filts = []
# Check to see if a start_date was chosen.
if self.cleaned_data['start_date']:
filts.append(SQ(created_date__gte=self.cleaned_data['start_date']))
# Check to see if an end_date was chosen.
if self.cleaned_data['end_date']:
filts.append(SQ(created_date__lte=self.cleaned_data['end_date']))
# Etc., for any other things you add
# If we started without a query, we'd have no search
# results (which is fine, normally). However, if we
# had no query but we DID have other parameters, then
# we'd like to filter starting from everything rather
# than nothing (i.e., q='' and tags='bear' should
# return everything with a tag 'bear'.)
if len(filts) > 0 and not self.cleaned_data['q']:
sqs = SearchQuerySet().order_by('-created_date')
# Apply the filters
for filt in filts:
sqs = sqs.filter(filt)
return sqs
另外,不要忘记更改视图:
{% if query or page.object_list %}
{# Display the results #}
{% else %}
{# Show some example queries to run, maybe query syntax, something else? #}
{% endif %}
实际上,视图代码有点hackish。它不区分无查询搜索,没有没有参数的搜索结果。
干杯!
答案 2 :(得分:3)
如果您的SearchIndex中定义了颜色和价格,则应该可以这样做:
sqs = SearchQuerySet().filter(color="blue", price__range=(10,100))
您可以通过向SearchQuerySet添加models(Model)
来将查询限制为某些模型。因此,如果要将查询限制为模型Item,请使用:
sqs = SearchQuerySet().filter(color="blue", price__range=(10,100)).models(Item)
答案 3 :(得分:1)
在表单显示后,如果不存在查询字符串,则显示所有结果。现在您可以添加自定义过滤器。
from your_app.forms import NonEmptySearchForm
url(r'^your_url$',
SearchView(template='search.html',searchqueryset=sqs,form_class=NonEmptySearchForm), name='haystack_search'),
#Overridding because the default sqs is always none if no query string is present
class NonEmptySearchForm(SearchForm):
def search(self):
if not self.is_valid():
return self.no_query_found()
sqs = self.searchqueryset.auto_query(self.cleaned_data['q'])
if self.load_all:
sqs = sqs.load_all()
return sqs
答案 4 :(得分:0)
Stumpy Joe Pete的回答很明显,但正如他所提到的,模板if query or page.object_list
检查有点被黑了。解决此问题的更好方法是创建自己的SearchForm
,如果q
为空,仍然会找到一些内容 - 不会重新发布 - 并使用以下内容自定义SearchView
/ p>
class MySearchView(SearchView):
def get_query(self):
query = []
if self.form.is_valid():
for field in self.form:
if field.name in self.form.cleaned_data and self.form.cleaned_data[field.name]:
query.append(field.name+'='+str(self.form.cleaned_data[field.name]))
return ' AND '.join(query)
在大多数情况下,您甚至不会使用query
值,因此您也可以快速检查是否设置了任何字段并返回True
或类似的内容..或者你当然可以按照你想要的方式修改输出(我甚至不能100%确定我的解决方案适用于所有字段类型,但你明白了。)