我的网站具有搜索功能,并使用elasticsearch,但是目前我只能搜索特定类别,而且我不知道如何使该功能适用于所有现有类别元素。我的目标是将默认选择器设置为“全部”,以便搜索所有类别。
urls.py
url(r'^search/$', app_views.search_elastic, name='search'),
views.py
def search_elastic(request):
qs = request.GET.get('qs')
page = request.GET.get('page')
if qs:
post_a = Post_A_Document.search().query("multi_match", query=qs, fields=["title", "content", "tag"]).to_queryset()
post_b = Post_B_MultipleDocument.search().query("multi_match", query=qs, fields=["title", "content", "tag"]).to_queryset()
post_c = Post_C_Document.search().query("multi_match", query=qs,fields=["title", "content","tag"]).to_queryset()
qs = list(
sorted(
chain(post_a, post_b, post_c),
key=lambda objects: objects.pk
))
else:
qs = ''
paginator = Paginator(qs, 10) # Show 10 results per page
try:
qs = paginator.page(page)
except PageNotAnInteger:
qs = paginator.page(1)
except EmptyPage:
qs = paginator.page(paginator.num_pages)
return render(request, 'app/search/search_results_elastic.html', {'object_list': qs})
base.html:
<div class="search">
<form id="searchform" action="{% url 'search' %}" method="get" accept-charset="utf-8">
<div class="form-row align-items-center">
<input class="class-search-input-fields" id="searchbox" name="qs" required="required" type="text" placeholder="Search ..."><a>in</a>
<div class="custom-dropdown">
<a>{{ categorysearch_form.category }}</a>
</div>
<button class="btn btn-outline-dark" type="submit">
<i class="fa fa-search"></i>
</button>
</div>
</form>
</div>