Skip to content

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

Remove global symbol for elasticsearch client

Jakub Draganek1d03ac4ae84e
diff --git a/saleor/search/backends/newelastic.py b/saleor/search/backends/newelastic.py
index eddad847ee..cf2e37689d 100644
--- a/saleor/search/backends/newelastic.py
+++ b/saleor/search/backends/newelastic.py
@@ -9,19 +9,6 @@ from elasticsearch_dsl.query import MultiMatch
CLIENT = Elasticsearch()
-def search_products(phrase):
- ''' Execute external search for product matching phrase '''
- INDEX = 'storefront__product_product' # TODO: parametrize this
- CONTENT = 'product.Product'
- query = MultiMatch(fields=['name', 'description'], query=phrase)
- search = (Search(index=INDEX).query(query)
- .filter('term', is_published_filter=True)
- .filter('match', content_type=CONTENT)
- .source(['pk'])
- .using(CLIENT))
- return [hit.pk for hit in search.execute()]
-
-
def _make_host_entry(url):
use_ssl = url.scheme == 'https'
auth = (url.username, url.password)
@@ -43,10 +30,28 @@ def _get_es_hosts(params):
class SearchBackend(object):
rebuilder_class = None
+ client = None
def __init__(self, params):
- global CLIENT
- CLIENT = Elasticsearch(hosts=_get_es_hosts(params))
+ self._init_client(params)
+
+ @classmethod
+ def _init_client(cls, params):
+ if cls.client is None:
+ cls.client = Elasticsearch(hosts=_get_es_hosts(params))
def search(self, query, model_or_queryset):
return model_or_queryset.filter(pk__in=search_products(query))
+
+
+def search_products(phrase):
+ ''' Execute external search for product matching phrase '''
+ INDEX = 'storefront__product_product' # TODO: parametrize this
+ CONTENT = 'product.Product'
+ query = MultiMatch(fields=['name', 'description'], query=phrase)
+ search = (Search(index=INDEX).query(query)
+ .filter('term', is_published_filter=True)
+ .filter('match', content_type=CONTENT)
+ .source(['pk'])
+ .using(SearchBackend.client))
+ return [hit.pk for hit in search.execute()]
diff --git a/saleor/search/backends/test_newelastic.py b/saleor/search/backends/test_newelastic.py
index 9b1e1c5594..9e79cf98e7 100644
--- a/saleor/search/backends/test_newelastic.py
+++ b/saleor/search/backends/test_newelastic.py
@@ -4,16 +4,11 @@ from . import newelastic
import pytest
-def search_phrase():
- return 'How fortunate man with none'
-
-
-def es_search_query(search_phrase):
- CONTENT_TYPE = 'product.Product'
- FIELDS = ['name', 'description']
- return {
+PHRASE = 'How fortunate man with none'
+CONTENT_TYPE = 'product.Product'
+FIELDS = ['name', 'description']
+INDEX = 'storefront__product_product'
+QUERY = {
'query': {
'bool': {
'must': [{
@@ -38,14 +33,16 @@ def es_search_query(search_phrase):
@pytest.fixture
-def storefront_index():
- return 'storefront__product_product'
-
-
-def test_storefront_product_search_query_syntax(
- mocker, es_search_query, search_phrase, storefront_index):
- mocker.patch.object(newelastic.CLIENT, 'search', returnvalue=[])
- products = newelastic.search_products(search_phrase)
- newelastic.CLIENT.search.assert_called_once_with(
- body=es_search_query, doc_type=[], index=[storefront_index])
+def elasticsearch_client(mocker):
+ SETTINGS = {'URLS': ['http://localhost']}
+ nes = newelastic.SearchBackend(SETTINGS)
+ mocker.patch.object(
+ newelastic.SearchBackend.client, 'search', returnvalue=[])
+ return nes
+
+
+def test_storefront_product_search_query_syntax(elasticsearch_client):
+ products = newelastic.search_products(PHRASE)
+ newelastic.SearchBackend.client.search.assert_called_once_with(
+ body=QUERY, doc_type=[], index=[INDEX])
assert not products