Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add search order by customer first and last name in elasticsearch
fowczarek35247e15bd50
diff --git a/saleor/search/backends/elasticsearch_dashboard.py b/saleor/search/backends/elasticsearch_dashboard.pyindex f7a6374fed..703102e808 100644--- a/saleor/search/backends/elasticsearch_dashboard.py+++ b/saleor/search/backends/elasticsearch_dashboard.py@@ -23,7 +23,8 @@ def _search_users(phrase):def _search_orders(phrase):order_query = MultiMatch(- fields=['user', 'discount_name'], query=phrase)+ fields=['user', 'first_name', 'last_name', 'discount_name'],+ query=phrase)return OrderDocument.search().query(order_query).source(False)diff --git a/saleor/search/backends/test_elasticsearch_dashboard.py b/saleor/search/backends/test_elasticsearch_dashboard.pyindex dbf77ac9e8..927ccc1182 100644--- a/saleor/search/backends/test_elasticsearch_dashboard.py+++ b/saleor/search/backends/test_elasticsearch_dashboard.py@@ -13,7 +13,7 @@ ORDERS_QUERY = {'_source': False,'query': {'multi_match': {- 'fields': ['user', 'discount_name'],+ 'fields': ['user', 'first_name', 'last_name', 'discount_name'],'query': PHRASE}}}USERS_QUERY = {'_source': False,diff --git a/saleor/search/documents.py b/saleor/search/documents.pyindex 163513c8c3..7dc4811cbe 100644--- a/saleor/search/documents.py+++ b/saleor/search/documents.py@@ -71,12 +71,34 @@ orders.settings(number_of_shards=1, number_of_replicas=0)@orders.doc_typeclass OrderDocument(DocType):user = fields.StringField(analyzer=email_analyzer)+ first_name = fields.StringField()+ last_name = fields.StringField()def prepare_user(self, instance):if instance.user:return instance.user.emailreturn instance.user_email+ def prepare_first_name(self, instance):+ user = instance.user+ if user:+ if user.first_name:+ return user.first_name+ address = user.default_billing_address+ if address:+ return address.first_name+ return None++ def prepare_last_name(self, instance):+ user = instance.user+ if user:+ if user.last_name:+ return user.last_name+ address = user.default_billing_address+ if address:+ return address.last_name+ return None+class Meta:model = Orderfields = ['user_email', 'discount_name']diff --git a/tests/cassettes/test_dashboard_search_orders_by_user_email.yaml b/tests/cassettes/test_dashboard_search_orders_by_user_email.yamlnew file mode 100644index 0000000000..27a1b1abc5--- /dev/null+++ b/tests/cassettes/test_dashboard_search_orders_by_user_email.yaml@@ -0,0 +1,47 @@+interactions:+- request:+ body: '{"query": {"multi_match": {"fields": ["name", "title", "description"],+ "query": "[email protected]", "type": "cross_fields"}}, "sort":+ ["_score"], "_source": {"excludes": ["*"]}}'+ headers:+ connection: [keep-alive]+ content-type: [application/json]+ method: GET+ uri: http://search:9200/storefront/product_document/_search+ response:+ body: {string: '{"took":1,"timed_out":false,"_shards":{"total":1,"successful":1,"failed":0},"hits":{"total":3,"max_score":5.373132,"hits":[{"_index":"storefront","_type":"product_document","_id":"1","_score":5.373132,"_source":{}},{"_index":"storefront","_type":"product_document","_id":"18","_score":3.740644,"_source":{}},{"_index":"storefront","_type":"product_document","_id":"38","_score":3.740644,"_source":{}}]}}'}+ headers:+ content-length: ['403']+ content-type: [application/json; charset=UTF-8]+ status: {code: 200, message: OK}+- request:+ body: '{"query": {"multi_match": {"fields": ["user", "email", "first_name", "last_name"],+ "query": "[email protected]", "type": "cross_fields", "operator":+ "and"}}, "_source": {"excludes": ["*"]}}'+ headers:+ connection: [keep-alive]+ content-type: [application/json]+ method: GET+ uri: http://search:9200/users/user_document/_search+ response:+ body: {string: '{"took":1,"timed_out":false,"_shards":{"total":1,"successful":1,"failed":0},"hits":{"total":1,"max_score":5.1992044,"hits":[{"_index":"users","_type":"user_document","_id":"2","_score":5.1992044,"_source":{}}]}}'}+ headers:+ content-length: ['211']+ content-type: [application/json; charset=UTF-8]+ status: {code: 200, message: OK}+- request:+ body: '{"query": {"multi_match": {"fields": ["user", "first_name", "last_name",+ "discount_name"], "query": "[email protected]"}}, "_source": {"excludes":+ ["*"]}}'+ headers:+ connection: [keep-alive]+ content-type: [application/json]+ method: GET+ uri: http://search:9200/orders/order_document/_search+ response:+ body: {string: '{"took":1,"timed_out":false,"_shards":{"total":1,"successful":1,"failed":0},"hits":{"total":2,"max_score":2.1282318,"hits":[{"_index":"orders","_type":"order_document","_id":"17","_score":2.1282318,"_source":{}},{"_index":"orders","_type":"order_document","_id":"11","_score":2.1282318,"_source":{}}]}}'}+ headers:+ content-length: ['302']+ content-type: [application/json; charset=UTF-8]+ status: {code: 200, message: OK}+version: 1diff --git a/tests/cassettes/test_dashboard_search_orders_by_user_name.yaml b/tests/cassettes/test_dashboard_search_orders_by_user_name.yamlnew file mode 100644index 0000000000..c799a0cf16--- /dev/null+++ b/tests/cassettes/test_dashboard_search_orders_by_user_name.yaml@@ -0,0 +1,46 @@+interactions:+- request:+ body: '{"query": {"multi_match": {"fields": ["name", "title", "description"],+ "query": "Martin", "type": "cross_fields"}}, "sort": ["_score"], "_source":+ {"excludes": ["*"]}}'+ headers:+ connection: [keep-alive]+ content-type: [application/json]+ method: GET+ uri: http://search:9200/storefront/product_document/_search+ response:+ body: {string: '{"took":1,"timed_out":false,"_shards":{"total":1,"successful":1,"failed":0},"hits":{"total":3,"max_score":5.373132,"hits":[{"_index":"storefront","_type":"product_document","_id":"1","_score":5.373132,"_source":{}},{"_index":"storefront","_type":"product_document","_id":"18","_score":3.740644,"_source":{}},{"_index":"storefront","_type":"product_document","_id":"38","_score":3.740644,"_source":{}}]}}'}+ headers:+ content-length: ['403']+ content-type: [application/json; charset=UTF-8]+ status: {code: 200, message: OK}+- request:+ body: '{"query": {"multi_match": {"fields": ["user", "email", "first_name", "last_name"],+ "query": "Martin", "type": "cross_fields", "operator": "and"}}, "_source": {"excludes":+ ["*"]}}'+ headers:+ connection: [keep-alive]+ content-type: [application/json]+ method: GET+ uri: http://search:9200/users/user_document/_search+ response:+ body: {string: '{"took":1,"timed_out":false,"_shards":{"total":1,"successful":1,"failed":0},"hits":{"total":1,"max_score":2.7725887,"hits":[{"_index":"users","_type":"user_document","_id":"2","_score":2.7725887,"_source":{}}]}}'}+ headers:+ content-length: ['211']+ content-type: [application/json; charset=UTF-8]+ status: {code: 200, message: OK}+- request:+ body: '{"query": {"multi_match": {"fields": ["user", "first_name", "last_name",+ "discount_name"], "query": "Martin"}}, "_source": {"excludes": ["*"]}}'+ headers:+ connection: [keep-alive]+ content-type: [application/json]+ method: GET+ uri: http://search:9200/orders/order_document/_search+ response:+ body: {string: '{"took":1,"timed_out":false,"_shards":{"total":1,"successful":1,"failed":0},"hits":{"total":2,"max_score":1.1631508,"hits":[{"_index":"orders","_type":"order_document","_id":"17","_score":1.1631508,"_source":{}},{"_index":"orders","_type":"order_document","_id":"11","_score":1.1631508,"_source":{}}]}}'}+ headers:+ content-length: ['302']+ content-type: [application/json; charset=UTF-8]+ status: {code: 200, message: OK}+version: 1diff --git a/tests/cassettes/test_dashboard_search_orders_by_user_name_without_address.yaml b/tests/cassettes/test_dashboard_search_orders_by_user_name_without_address.yamlnew file mode 100644index 0000000000..163324cef7--- /dev/null+++ b/tests/cassettes/test_dashboard_search_orders_by_user_name_without_address.yaml@@ -0,0 +1,46 @@+interactions:+- request:+ body: '{"query": {"multi_match": {"fields": ["name", "title", "description"],+ "query": "Rachel", "type": "cross_fields"}}, "sort": ["_score"], "_source":+ {"excludes": ["*"]}}'+ headers:+ connection: [keep-alive]+ content-type: [application/json]+ method: GET+ uri: http://search:9200/storefront/product_document/_search+ response:+ body: {string: '{"took":0,"timed_out":false,"_shards":{"total":1,"successful":1,"failed":0},"hits":{"total":0,"max_score":null,"hits":[]}}'}+ headers:+ content-length: ['122']+ content-type: [application/json; charset=UTF-8]+ status: {code: 200, message: OK}+- request:+ body: '{"query": {"multi_match": {"fields": ["user", "email", "first_name", "last_name"],+ "query": "Rachel", "type": "cross_fields", "operator": "and"}}, "_source": {"excludes":+ ["*"]}}'+ headers:+ connection: [keep-alive]+ content-type: [application/json]+ method: GET+ uri: http://search:9200/users/user_document/_search+ response:+ body: {string: '{"took":1,"timed_out":false,"_shards":{"total":1,"successful":1,"failed":0},"hits":{"total":1,"max_score":2.261763,"hits":[{"_index":"users","_type":"user_document","_id":"17","_score":2.261763,"_source":{}}]}}'}+ headers:+ content-length: ['210']+ content-type: [application/json; charset=UTF-8]+ status: {code: 200, message: OK}+- request:+ body: '{"query": {"multi_match": {"fields": ["user", "first_name", "last_name",+ "discount_name"], "query": "Rachel"}}, "_source": {"excludes": ["*"]}}'+ headers:+ connection: [keep-alive]+ content-type: [application/json]+ method: GET+ uri: http://search:9200/orders/order_document/_search+ response:+ body: {string: '{"took":1,"timed_out":false,"_shards":{"total":1,"successful":1,"failed":0},"hits":{"total":1,"max_score":1.6739764,"hits":[{"_index":"orders","_type":"order_document","_id":"15","_score":1.6739764,"_source":{}}]}}'}+ headers:+ content-length: ['214']+ content-type: [application/json; charset=UTF-8]+ status: {code: 200, message: OK}+version: 1diff --git a/tests/test_elasticsearch.py b/tests/test_elasticsearch.pyindex 863d03194a..4f87874a23 100644--- a/tests/test_elasticsearch.py+++ b/tests/test_elasticsearch.py@@ -129,17 +129,22 @@ def test_dashboard_search_with_product_result(# data below must be aligned with recorded communication every timeEXISTING_EMAIL = '[email protected]'NON_EXISTING_EMAIL = '[email protected]'-USER_WITH_ORDER = '[email protected]'+USER_EMAIL_WITH_ORDER = '[email protected]'+USER_NAME_WITH_ORDER = 'Martin'+USER_EMAIL_WITH_ORDER_WITHOUT_ADDRES = '[email protected]'+USER_NAME_WITH_ORDER_WITHOUT_ADDRES = 'Rachel'EXISTING_NAME = 'Rhonda'EXISTING_NAME_FULL = '[email protected]'EXISTING_NAME_WITHOUT_ADDRESS = 'Brett'EXISTING_NAME_FULL_WITHOUT_ADDRESS = '[email protected]'USERS = {EXISTING_EMAIL: 9,NON_EXISTING_EMAIL: 870,- USER_WITH_ORDER: 666,+ USER_EMAIL_WITH_ORDER: 2,+ USER_EMAIL_WITH_ORDER_WITHOUT_ADDRES: 17,EXISTING_NAME_FULL: 6,EXISTING_NAME_FULL_WITHOUT_ADDRESS: 22}-ORDERS = {USER_WITH_ORDER: {18, 19}}+ORDERS = {USER_EMAIL_WITH_ORDER: {11, 17},+ USER_EMAIL_WITH_ORDER_WITHOUT_ADDRES: {15}}@pytest.fixture@@ -184,6 +189,27 @@ def orders(db, address):@pytest.mark.integration@pytest.mark.vcr(record_mode='once', match_on=MATCH_SEARCH_REQUEST)-def test_dashboard_search_orders_by_user(db, admin_client, customers, orders):- _, _, orders = execute_dashboard_search(admin_client, USER_WITH_ORDER)- assert ORDERS[USER_WITH_ORDER] == orders+def test_dashboard_search_orders_by_user_email(db, admin_client,+ customers, orders):+ _, _, orders = execute_dashboard_search(+ admin_client, USER_EMAIL_WITH_ORDER)+ assert ORDERS[USER_EMAIL_WITH_ORDER] == orders++[email protected](record_mode='once', match_on=MATCH_SEARCH_REQUEST)+def test_dashboard_search_orders_by_user_name(db, admin_client,+ customers, orders):+ _, _, orders = execute_dashboard_search(+ admin_client, USER_NAME_WITH_ORDER)+ assert ORDERS[USER_EMAIL_WITH_ORDER] == orders++[email protected](record_mode='once', match_on=MATCH_SEARCH_REQUEST)+def test_dashboard_search_orders_by_user_name_without_address(db, admin_client,+ customers,+ orders):+ _, _, orders = execute_dashboard_search(+ admin_client, USER_NAME_WITH_ORDER_WITHOUT_ADDRES)+ assert ORDERS[USER_EMAIL_WITH_ORDER_WITHOUT_ADDRES] == orders