Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Add pagination for storefront search results

Jakub Draganek52d059b9b4c7
diff --git a/saleor/search/backends/elasticsearch.py b/saleor/search/backends/elasticsearch.py
index 692dbd0888..adeabcc5b2 100644
--- a/saleor/search/backends/elasticsearch.py
+++ b/saleor/search/backends/elasticsearch.py
@@ -1,6 +1,5 @@
from __future__ import unicode_literals
-from django.db.models.query import QuerySet
from elasticsearch_dsl.query import MultiMatch
from ..documents import ProductDocument
@@ -15,13 +14,9 @@ def get_search_query(phrase):
def _execute_es_search(search):
- return [hit.meta.id for hit in search.execute()]
+ return [hit.meta.id for hit in search.scan()]
-def search(query, model_or_queryset):
- qs = model_or_queryset
- # TODO: remove this ugly type incoherence of old search api
- if not isinstance(model_or_queryset, QuerySet):
- qs = model_or_queryset.objects.all()
- found_objs = _execute_es_search(get_search_query(query))
+def search(phrase, qs):
+ found_objs = _execute_es_search(get_search_query(phrase))
return qs.filter(pk__in=found_objs)
diff --git a/saleor/search/forms.py b/saleor/search/forms.py
index 2e301b4f40..ad1629eaac 100644
--- a/saleor/search/forms.py
+++ b/saleor/search/forms.py
@@ -7,5 +7,6 @@ class SearchForm(forms.Form):
q = forms.CharField(
label=pgettext('Search form label', 'Query'), required=True)
- def search(self, model_or_queryset):
- return elasticsearch.search(self.cleaned_data['q'], model_or_queryset)
+ def search(self, qs):
+ return elasticsearch.search(
+ self.cleaned_data['q'], qs)
diff --git a/saleor/search/views.py b/saleor/search/views.py
index 73857c6963..6b67a653e6 100644
--- a/saleor/search/views.py
+++ b/saleor/search/views.py
@@ -22,16 +22,15 @@ def search(request):
form = SearchForm(data=request.GET or None)
if form.is_valid():
visible_products = products_with_details(request.user)
- results = form.search(model_or_queryset=visible_products)
+ results = form.search(qs=visible_products)
results = products_with_availability(
results, discounts=request.discounts,
local_currency=request.currency)
- results = list(results)
query = form.cleaned_data.get('q', '')
else:
results = []
query = ''
- page = paginate_results(results, request.GET, settings.PAGINATE_BY)
+ page = paginate_results(list(results), request.GET, settings.PAGINATE_BY)
ctx = {
'query': query,
'results': page,