Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Use model doctype to perform search
Jakub Draganek48c0ebd1b9d3
diff --git a/saleor/search/backends/newelastic.py b/saleor/search/backends/newelastic.pyindex 0df68bf6cc..e455e995e4 100644--- a/saleor/search/backends/newelastic.py+++ b/saleor/search/backends/newelastic.py@@ -3,8 +3,8 @@ from __future__ import unicode_literalsfrom elasticsearch import Elasticsearchfrom django.utils.six.moves.urllib.parse import urlparsefrom django.db.models.query import QuerySet-from elasticsearch_dsl import Searchfrom elasticsearch_dsl.query import MultiMatch+from ..documents import ProductDocumentdef _make_host_entry(url):@@ -48,12 +48,10 @@ class SearchBackend(object):def search_products(phrase):''' Execute external search for product matching phrase '''- INDEX = 'storefront__product_product'- 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()]+ search = (ProductDocument.search()+ .query(query)+ .source(False)+ .filter('term', is_published=True)+ .using(SearchBackend.client)) # TODO: use default+ return [hit.meta.id for hit in search.execute()]diff --git a/saleor/search/backends/test_newelastic.py b/saleor/search/backends/test_newelastic.pyindex e2affa05b7..db55db3596 100644--- a/saleor/search/backends/test_newelastic.py+++ b/saleor/search/backends/test_newelastic.py@@ -5,31 +5,27 @@ import pytestPHRASE = 'How fortunate man with none'-CONTENT_TYPE = 'product.Product'FIELDS = ['name', 'description']-INDEX = 'storefront__product_product'+INDEX = 'storefront'+DOC_TYPE = 'product_document'QUERY = {- 'query': {- 'bool': {- 'must': [{- 'multi_match': {- 'fields': FIELDS,- 'query': 'How fortunate man with none'- }- }],- 'filter': [{- 'term': {- 'is_published_filter': True- }- }, {- 'match': {- 'content_type': CONTENT_TYPE- }- }]- }- },- '_source': ['pk']+ '_source': False,+ 'query': {+ 'bool': {+ 'must': [{+ 'multi_match': {+ 'query': PHRASE,+ 'fields': FIELDS+ }+ }],+ 'filter': [{+ 'term': {+ 'is_published': True+ }+ }]+ }}+}@pytest.fixture@@ -44,4 +40,4 @@ def elasticsearch_client(mocker):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])+ body=QUERY, doc_type=[DOC_TYPE], index=[INDEX])