Skip to content

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

Simplify code and fix search return value

Jakub Draganek0afb4389ecf8
diff --git a/saleor/search/backends/newelastic.py b/saleor/search/backends/newelastic.py
index b72b3bf327..76a870ef0d 100644
--- a/saleor/search/backends/newelastic.py
+++ b/saleor/search/backends/newelastic.py
@@ -3,18 +3,12 @@ from django.utils.six.moves.urllib.parse import urlparse
from elasticsearch_dsl import Search
from elasticsearch_dsl.query import MultiMatch
-from ...product.models import Product
-
-
CLIENT = Elasticsearch()
def search(phrase):
- result = (Search()
- .query(MultiMatch(query=phrase, fields=['name', 'description']))
- .source(['pk'])
- .using(CLIENT)
- .execute())
+ query = MultiMatch(query=phrase, fields=['name', 'description'])
+ result = Search().query(query).source(['pk']).using(CLIENT).execute()
return [hit.pk for hit in result]
@@ -34,7 +28,8 @@ def _get_es_hosts(params):
'url_prefix': parsed_url.path,
'use_ssl': use_ssl,
'verify_certs': use_ssl,
- 'http_auth': http_auth})
+ 'http_auth': http_auth
+ })
return hosts
@@ -45,7 +40,5 @@ class SearchBackend(object):
global CLIENT
CLIENT = Elasticsearch(hosts=_get_es_hosts(params))
- def search(self, query, model_or_queryset=None):
- found_product_ids = search(query)
- products = Product.objects.filter(pk__in=found_product_ids)
- return [str(obj.pk) for obj in products]
+ def search(self, query, model_or_queryset):
+ return model_or_queryset.filter(pk__in=search(query))
diff --git a/saleor/search/forms.py b/saleor/search/forms.py
index 1c98ff69da..b051409931 100644
--- a/saleor/search/forms.py
+++ b/saleor/search/forms.py
@@ -5,8 +5,10 @@ from .backends import get_search_backend
USE_BACKEND = 'default'
+
class SearchForm(forms.Form):
- q = forms.CharField(label=pgettext('Search form label', 'Query'), required=True)
+ q = forms.CharField(
+ label=pgettext('Search form label', 'Query'), required=True)
def search(self, model_or_queryset):
backend = get_search_backend(USE_BACKEND)