Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Simplify search backend configuration
Jakub Draganekb1d799da3cf8
diff --git a/saleor/search/backends/elasticsearch.py b/saleor/search/backends/elasticsearch.pyindex ddf0c4acb2..62a908cd74 100644--- a/saleor/search/backends/elasticsearch.py+++ b/saleor/search/backends/elasticsearch.py@@ -1,17 +1,11 @@+# -*- coding: utf-8 -*-from __future__ import unicode_literals+from . import elasticsearch_dashboard, elasticsearch_storefront-from elasticsearch_dsl.query import MultiMatch-from ..documents import ProductDocument+def search_storefront(phrase):+ return elasticsearch_storefront.search(phrase)-def get_search_query(phrase):- ''' Execute external search for product matching phrase '''- query = MultiMatch(fields=['title', 'name', 'description'], query=phrase)- return (ProductDocument.search()- .query(query)- .source(False)- .filter('term', is_published=True))--def search(phrase):- return get_search_query(phrase).to_queryset()+def search_dashboard(phrase):+ return elasticsearch_dashboard.search(phrase)diff --git a/saleor/search/backends/elasticsearch_storefront.py b/saleor/search/backends/elasticsearch_storefront.pynew file mode 100644index 0000000000..3dd363c71c--- /dev/null+++ b/saleor/search/backends/elasticsearch_storefront.py@@ -0,0 +1,17 @@+from __future__ import unicode_literals++from elasticsearch_dsl.query import MultiMatch+from ..documents import ProductDocument+++def get_search_query(phrase):+ ''' Execute external search for product matching phrase '''+ query = MultiMatch(fields=['title', 'name', 'description'], query=phrase)+ return (ProductDocument.search()+ .query(query)+ .source(False)+ .filter('term', is_published=True))+++def search(phrase):+ return get_search_query(phrase).to_queryset()diff --git a/saleor/search/backends/picker.py b/saleor/search/backends/picker.pyindex e2d559dd5d..732bb5ab83 100644--- a/saleor/search/backends/picker.py+++ b/saleor/search/backends/picker.py@@ -1,6 +1,6 @@from __future__ import unicode_literals-import importlib+from django.utils.module_loading import import_modulefrom django.conf import settings@@ -10,9 +10,7 @@ def pick_backend()::rtype: callable with one argument of type str'''- backend = importlib.import_module(- settings.SEARCH_BACKENDS[settings.SEARCH_BACKEND]['storefront'])- return backend.search+ return import_module(settings.SEARCH_BACKEND).search_storefrontdef pick_dashboard_backend():@@ -21,6 +19,4 @@ def pick_dashboard_backend()::rtype: callable with one argument of type str'''- backend = importlib.import_module(- settings.SEARCH_BACKENDS[settings.SEARCH_BACKEND]['dashboard'])- return backend.search+ return import_module(settings.SEARCH_BACKEND).search_dashboarddiff --git a/saleor/search/backends/postgresql.py b/saleor/search/backends/postgresql.pyindex cd21076210..020da0c903 100644--- a/saleor/search/backends/postgresql.py+++ b/saleor/search/backends/postgresql.py@@ -1,22 +1,11 @@-from ...product.models import Product-from django.db.models import Q-from django.contrib.postgres.search import TrigramSimilarity+# -*- coding: utf-8 -*-+from __future__ import unicode_literals+from . import postgresql_dashboard, postgresql_storefront-def search(phrase):- '''Storefront full text product search+def search_storefront(phrase):+ return postgresql_storefront.search(phrase)- Fuzzy storefront search that is resistant to small typing errors made- by user. Name is matched using trigram similarity, description uses- standard postgres full text search.- Args:- phrase (str): searched phrase-- '''- name_sim = TrigramSimilarity('name', phrase)- published = Q(is_published=True)- ft_in_description = Q(description__search=phrase)- name_similar = Q(name_sim__gt=0.2)- return Product.objects.annotate(name_sim=name_sim).filter(- (ft_in_description | name_similar) & published)+def search_dashboard(phrase):+ return postgresql_dashboard.search(phrase)diff --git a/saleor/search/backends/postgresql_storefront.py b/saleor/search/backends/postgresql_storefront.pynew file mode 100644index 0000000000..cd21076210--- /dev/null+++ b/saleor/search/backends/postgresql_storefront.py@@ -0,0 +1,22 @@+from ...product.models import Product+from django.db.models import Q+from django.contrib.postgres.search import TrigramSimilarity+++def search(phrase):+ '''Storefront full text product search++ Fuzzy storefront search that is resistant to small typing errors made+ by user. Name is matched using trigram similarity, description uses+ standard postgres full text search.++ Args:+ phrase (str): searched phrase++ '''+ name_sim = TrigramSimilarity('name', phrase)+ published = Q(is_published=True)+ ft_in_description = Q(description__search=phrase)+ name_similar = Q(name_sim__gt=0.2)+ return Product.objects.annotate(name_sim=name_sim).filter(+ (ft_in_description | name_similar) & published)diff --git a/saleor/search/backends/test_dashboard.py b/saleor/search/backends/test_elasticsearch_dashboard.pysimilarity index 100%rename from saleor/search/backends/test_dashboard.pyrename to saleor/search/backends/test_elasticsearch_dashboard.pydiff --git a/saleor/search/backends/test_elasticsearch.py b/saleor/search/backends/test_elasticsearch_storefront.pysimilarity index 83%rename from saleor/search/backends/test_elasticsearch.pyrename to saleor/search/backends/test_elasticsearch_storefront.pyindex daa8223b23..a8d3f40ed6 100644--- a/saleor/search/backends/test_elasticsearch.py+++ b/saleor/search/backends/test_elasticsearch_storefront.py@@ -1,5 +1,5 @@from __future__ import unicode_literals-from . import elasticsearch+from . import elasticsearch_storefrontPHRASE = 'How fortunate man with none'FIELDS = ['title', 'name', 'description']@@ -26,4 +26,4 @@ QUERY = {def test_storefront_product_search_query_syntax():- assert QUERY == elasticsearch.get_search_query(PHRASE).to_dict()+ assert QUERY == elasticsearch_storefront.get_search_query(PHRASE).to_dict()diff --git a/saleor/settings.py b/saleor/settings.pyindex 0bf6ca2dc7..40b4191061 100644--- a/saleor/settings.py+++ b/saleor/settings.py@@ -366,21 +366,10 @@ ES_URL = (os.environ.get('ELASTICSEARCH_URL') orENABLE_SEARCH = bool(ES_URL) or DB_SEARCH_ENABLED # global search disabling-SEARCH_BACKENDS = {- 'default': {- 'storefront': 'saleor.search.backends.postgresql',- 'dashboard': 'saleor.search.backends.postgresql_dashboard'- },- 'elasticsearch': {- 'storefront': 'saleor.search.backends.elasticsearch',- 'dashboard': 'saleor.search.backends.elasticsearch_dashboard'- }-}--SEARCH_BACKEND = 'default'+SEARCH_BACKEND = 'saleor.search.backends.postgresql'if ES_URL:- SEARCH_BACKEND = 'elasticsearch'+ SEARCH_BACKEND = 'saleor.search.backends.elasticsearch'INSTALLED_APPS.append('django_elasticsearch_dsl')ELASTICSEARCH_DSL = {'default': {diff --git a/tests/test_elasticsearch.py b/tests/test_elasticsearch.pyindex dacafaf283..01e5c243a5 100644--- a/tests/test_elasticsearch.py+++ b/tests/test_elasticsearch.py@@ -30,7 +30,7 @@ def elasticsearch_connection():def elasticsearch_enabled(settings):settings.ES_URL = ES_URLsettings.ENABLE_SEARCH = True- settings.SEARCH_BACKEND = 'elasticsearch'+ settings.SEARCH_BACKEND = 'saleor.search.backends.elasticsearch'@pytest.fixture(scope='function', autouse=True)diff --git a/tests/test_postgresql_search.py b/tests/test_postgresql_search.pyindex d3d5ef04e6..aee71295e3 100644--- a/tests/test_postgresql_search.py+++ b/tests/test_postgresql_search.py@@ -13,7 +13,7 @@ import pytest@pytest.fixture(scope='function', autouse=True)def postgresql_search_enabled(settings):settings.ENABLE_SEARCH = True- settings.SEARCH_BACKEND = 'default'+ settings.SEARCH_BACKEND = 'saleor.search.backends.postgresql'PRODUCTS = [('Arabica Coffee', 'The best grains in galactic'),