Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Implement searching for partial phrase match in product search
Jakub Draganek09d39230848b
diff --git a/saleor/search/backends/elasticsearch_dashboard.py b/saleor/search/backends/elasticsearch_dashboard.pyindex f0261b5dea..59cbf70163 100644--- a/saleor/search/backends/elasticsearch_dashboard.py+++ b/saleor/search/backends/elasticsearch_dashboard.py@@ -5,8 +5,12 @@ from elasticsearch_dsl.query import MultiMatchdef _search_products(phrase):- prod_query = MultiMatch(fields=['name', 'description'], query=phrase)- return ProductDocument.search().query(prod_query).source(False)+ prod_query = MultiMatch(+ fields=['name', 'title', 'description'],+ query=phrase,+ type='cross_fields')+ return ProductDocument.search().query(prod_query).sort('_score').source(+ False)def _search_users(phrase):diff --git a/saleor/search/backends/newelastic.py b/saleor/search/backends/newelastic.pyindex 4eb9777429..692dbd0888 100644--- a/saleor/search/backends/newelastic.py+++ b/saleor/search/backends/newelastic.py@@ -7,7 +7,7 @@ from ..documents import ProductDocumentdef get_search_query(phrase):''' Execute external search for product matching phrase '''- query = MultiMatch(fields=['name', 'description'], query=phrase)+ query = MultiMatch(fields=['title', 'name', 'description'], query=phrase)return (ProductDocument.search().query(query).source(False)diff --git a/saleor/search/backends/test_dashboard.py b/saleor/search/backends/test_dashboard.pyindex 5aa45d9bf5..0d922c0b60 100644--- a/saleor/search/backends/test_dashboard.py+++ b/saleor/search/backends/test_dashboard.py@@ -6,10 +6,12 @@ PRODUCT_QUERY = {'_source': False,'query': {'multi_match': {- 'fields': ['name', 'description'],- 'query': PHRASE+ 'fields': [ 'name', 'title', 'description'],+ 'query': PHRASE,+ 'type': 'cross_fields'}- }+ },+ 'sort': ['_score']}ORDERS_QUERY = {'_source': False,diff --git a/saleor/search/backends/test_newelastic.py b/saleor/search/backends/test_newelastic.pyindex 19cf37bf1b..4428dc64b2 100644--- a/saleor/search/backends/test_newelastic.py+++ b/saleor/search/backends/test_newelastic.py@@ -2,7 +2,7 @@ from __future__ import unicode_literalsfrom . import newelasticPHRASE = 'How fortunate man with none'-FIELDS = ['name', 'description']+FIELDS = ['title', 'name', 'description']INDEX = 'storefront'DOC_TYPE = 'product_document'QUERY = {diff --git a/saleor/search/documents.py b/saleor/search/documents.pyindex 351a81408d..ef4a260df8 100644--- a/saleor/search/documents.py+++ b/saleor/search/documents.py@@ -1,33 +1,41 @@+from __future__ import unicode_literals+from django_elasticsearch_dsl import DocType, Index, fields-from elasticsearch_dsl import analyzer+from elasticsearch_dsl import analyzer, token_filterfrom ..product.models import Productfrom ..userprofile.models import Userfrom ..order.models import Order+storefront = Index('storefront')storefront.settings(number_of_shards=1, number_of_replicas=0)+partial_words = token_filter(+ 'partial_words', 'edge_ngram', min_gram=3, max_gram=15)+title_analyzer = analyzer(+ 'title_analyzer',+ tokenizer='standard',+ filter=[partial_words, 'lowercase'])+email_analyzer = analyzer('email_analyzer', tokenizer='uax_url_email')++@storefront.doc_typeclass ProductDocument(DocType):- class Meta:- model = Product # The model associated with this DocType+ title = fields.StringField(analyzer=title_analyzer)- # The fields of the model you want to be indexed in Elasticsearch- fields = [- 'name',- 'description',- 'is_published'- ]+ def prepare_title(self, instance):+ return instance.name++ class Meta:+ model = Product+ fields = ['name', 'description', 'is_published']users = Index('users')users.settings(number_of_shards=1, number_of_replicas=0)-email_analyzer = analyzer('email_analyzer', tokenizer='uax_url_email')--@users.doc_typeclass UserDocument(DocType):user = fields.StringField(analyzer=email_analyzer)diff --git a/tests/test_search.py b/tests/test_search.pyindex 1d5e944a2d..9d244eea27 100644--- a/tests/test_search.py+++ b/tests/test_search.py@@ -6,7 +6,7 @@ from elasticsearch_dsl.connections import connectionsfrom decimal import Decimalimport pytest-MATCH_SEARCH_REQUEST = ['method', 'host', 'port', 'path', 'body']+MATCH_SEARCH_REQUEST = ['method', 'host', 'port', 'path']STOREFRONT_PRODUCTS = {15, 56} # same as in recorded data!DASHBOARD_PRODUCTS = {58, 56}PRODUCTS_INDEXED = STOREFRONT_PRODUCTS | DASHBOARD_PRODUCTS