擅长:python、mysql、java
<p>让它像这样工作:</p>
<pre><code>def globalsearch_elastic(request):
qs = request.GET.get('qs')
page = request.GET.get('page')
if qs:
post = PostDocument.search().query("multi_match", query=qs, fields=["title", "content", "tag"]).to_queryset()
post_extra = PostExtraDocument.search().query("multi_match", query=qs, fields=["title", "content_preview", "tag"]).to_queryset()
qs = list(
sorted(
chain(post, post_extra),
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, 'MyProject/search/search_results_elastic.html', {'object_list': qs})
</code></pre>
<p>我知道这可能不是“最佳实践”,因为我总是要加载所有帖子,然后对它们进行排序,然后按创建日期(pk)显示它们。在</p>
<p>无论如何,在我看来,如果你不必搜索数百万个帖子。。。除此之外,你仍然有弹性的力量,即使这不排序我的qs帖子列表</p>