Skip to content

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

Implement searching for user by first and last name

Jakub Draganekaae0729969b7
diff --git a/saleor/search/backends/elasticsearch_dashboard.py b/saleor/search/backends/elasticsearch_dashboard.py
index 7a5c56cae9..f0261b5dea 100644
--- a/saleor/search/backends/elasticsearch_dashboard.py
+++ b/saleor/search/backends/elasticsearch_dashboard.py
@@ -11,7 +11,7 @@ def _search_products(phrase):
def _search_users(phrase):
user_query = MultiMatch(
- fields=['user', 'email'],
+ fields=['user', 'email', 'first_name', 'last_name'],
query=phrase,
type='cross_fields',
operator='and')
diff --git a/saleor/search/backends/test_dashboard.py b/saleor/search/backends/test_dashboard.py
index a8cf1ea586..5aa45d9bf5 100644
--- a/saleor/search/backends/test_dashboard.py
+++ b/saleor/search/backends/test_dashboard.py
@@ -24,7 +24,7 @@ USERS_QUERY = {
'_source': False,
'query': {
'multi_match': {
- 'fields': ['user', 'email'],
+ 'fields': ['user', 'email', 'first_name', 'last_name'],
'operator': 'and',
'query': PHRASE,
'type': 'cross_fields'
diff --git a/saleor/search/documents.py b/saleor/search/documents.py
index 45f642916d..351a81408d 100644
--- a/saleor/search/documents.py
+++ b/saleor/search/documents.py
@@ -31,10 +31,22 @@ email_analyzer = analyzer('email_analyzer', tokenizer='uax_url_email')
@users.doc_type
class UserDocument(DocType):
user = fields.StringField(analyzer=email_analyzer)
+ first_name = fields.StringField()
+ last_name = fields.StringField()
def prepare_user(self, instance):
return instance.email
+ def prepare_first_name(self, instance):
+ address = instance.default_billing_address
+ if address:
+ return address.first_name
+
+ def prepare_last_name(self, instance):
+ address = instance.default_billing_address
+ if address:
+ return address.last_name
+
class Meta:
model = User
fields = ['email']
diff --git a/tests/cassettes/test_dashboard_search_orders_by_user.yaml b/tests/cassettes/test_dashboard_search_orders_by_user.yaml
index 52164c38a1..4555418e8d 100644
--- a/tests/cassettes/test_dashboard_search_orders_by_user.yaml
+++ b/tests/cassettes/test_dashboard_search_orders_by_user.yaml
@@ -1,49 +1,51 @@
interactions:
- request:
- body: '{"_source": {"exclude": ["*"]}, "query": {"multi_match": {"fields": ["name",
- "description"], "query": "[email protected]"}}}'
+ body: '{"query": {"multi_match": {"query": "[email protected]", "fields":
+ ["name", "description"]}}, "_source": {"exclude": ["*"]}}'
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":[]}}'}
+ body: {string: '{"took":1,"timed_out":false,"_shards":{"total":1,"successful":1,"failed":0},"hits":{"total":0,"max_score":null,"hits":[]}}'}
headers:
Warning: ['299 Elasticsearch-5.4.3-eed30a8 "Deprecated field [exclude] used,
- expected [excludes] instead" "Wed, 15 Nov 2017 14:45:10 GMT"']
+ expected [excludes] instead" "Thu, 16 Nov 2017 09:44:26 GMT"']
content-length: ['122']
content-type: [application/json; charset=UTF-8]
status: {code: 200, message: OK}
- request:
- body: '{"_source": {"exclude": ["*"]}, "query": {"multi_match": {"fields": ["user",
- "status", "discount_name"], "query": "[email protected]"}}}'
+ body: '{"query": {"multi_match": {"query": "[email protected]", "fields":
+ ["user", "status", "discount_name"]}}, "_source": {"exclude": ["*"]}}'
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":2.6390574,"hits":[{"_index":"orders","_type":"order_document","_id":"20","_score":2.6390574,"_source":{}}]}}'}
+ body: {string: '{"took":2,"timed_out":false,"_shards":{"total":1,"successful":1,"failed":0},"hits":{"total":2,"max_score":2.1282318,"hits":[{"_index":"orders","_type":"order_document","_id":"19","_score":2.1282318,"_source":{}},{"_index":"orders","_type":"order_document","_id":"18","_score":2.1282318,"_source":{}}]}}'}
headers:
Warning: ['299 Elasticsearch-5.4.3-eed30a8 "Deprecated field [exclude] used,
- expected [excludes] instead" "Wed, 15 Nov 2017 14:45:10 GMT"']
- content-length: ['214']
+ expected [excludes] instead" "Thu, 16 Nov 2017 09:44:26 GMT"']
+ content-length: ['302']
content-type: [application/json; charset=UTF-8]
status: {code: 200, message: OK}
- request:
- body: '{"_source": {"exclude": ["*"]}, "query": {"match": {"user": "[email protected]"}}}'
+ body: '{"query": {"multi_match": {"query": "[email protected]", "type":
+ "cross_fields", "operator": "and", "fields": ["user", "email", "first_name",
+ "last_name"]}}, "_source": {"exclude": ["*"]}}'
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.6855774,"hits":[{"_index":"users","_type":"user_document","_id":"12","_score":2.6855774,"_source":{}}]}}'}
+ body: {string: '{"took":1,"timed_out":false,"_shards":{"total":1,"successful":1,"failed":0},"hits":{"total":1,"max_score":5.115776,"hits":[{"_index":"users","_type":"user_document","_id":"17","_score":5.115776,"_source":{}}]}}'}
headers:
Warning: ['299 Elasticsearch-5.4.3-eed30a8 "Deprecated field [exclude] used,
- expected [excludes] instead" "Wed, 15 Nov 2017 14:45:11 GMT"']
- content-length: ['212']
+ expected [excludes] instead" "Thu, 16 Nov 2017 09:44:26 GMT"']
+ content-length: ['210']
content-type: [application/json; charset=UTF-8]
status: {code: 200, message: OK}
version: 1
diff --git a/tests/cassettes/test_dashboard_search_user_by_email.yaml b/tests/cassettes/test_dashboard_search_user_by_email.yaml
index b1dca3d7d4..63b4e71b46 100644
--- a/tests/cassettes/test_dashboard_search_user_by_email.yaml
+++ b/tests/cassettes/test_dashboard_search_user_by_email.yaml
@@ -1,49 +1,51 @@
interactions:
- request:
- body: '{"_source": {"exclude": ["*"]}, "query": {"multi_match": {"fields": ["user",
- "status", "discount_name"], "query": "[email protected]"}}}'
+ body: '{"query": {"multi_match": {"query": "[email protected]", "fields":
+ ["name", "description"]}}, "_source": {"exclude": ["*"]}}'
headers:
connection: [keep-alive]
content-type: [application/json]
method: GET
- uri: http://search:9200/orders/order_document/_search
+ uri: http://search:9200/storefront/product_document/_search
response:
- body: {string: '{"took":2,"timed_out":false,"_shards":{"total":1,"successful":1,"failed":0},"hits":{"total":0,"max_score":null,"hits":[]}}'}
+ body: {string: '{"took":0,"timed_out":false,"_shards":{"total":1,"successful":1,"failed":0},"hits":{"total":0,"max_score":null,"hits":[]}}'}
headers:
Warning: ['299 Elasticsearch-5.4.3-eed30a8 "Deprecated field [exclude] used,
- expected [excludes] instead" "Wed, 15 Nov 2017 13:19:49 GMT"']
+ expected [excludes] instead" "Thu, 16 Nov 2017 09:44:26 GMT"']
content-length: ['122']
content-type: [application/json; charset=UTF-8]
status: {code: 200, message: OK}
- request:
- body: '{"_source": {"exclude": ["*"]}, "query": {"match": {"user": "[email protected]"}}}'
+ body: '{"query": {"multi_match": {"query": "[email protected]", "fields":
+ ["user", "status", "discount_name"]}}, "_source": {"exclude": ["*"]}}'
headers:
connection: [keep-alive]
content-type: [application/json]
method: GET
- uri: http://search:9200/users/user_document/_search
+ uri: http://search:9200/orders/order_document/_search
response:
- body: {string: '{"took":2,"timed_out":false,"_shards":{"total":1,"successful":1,"failed":0},"hits":{"total":1,"max_score":2.6855774,"hits":[{"_index":"users","_type":"user_document","_id":"9","_score":2.6855774,"_source":{}}]}}'}
+ body: {string: '{"took":1,"timed_out":false,"_shards":{"total":1,"successful":1,"failed":0},"hits":{"total":0,"max_score":null,"hits":[]}}'}
headers:
Warning: ['299 Elasticsearch-5.4.3-eed30a8 "Deprecated field [exclude] used,
- expected [excludes] instead" "Wed, 15 Nov 2017 13:19:49 GMT"']
- content-length: ['211']
+ expected [excludes] instead" "Thu, 16 Nov 2017 09:44:26 GMT"']
+ content-length: ['122']
content-type: [application/json; charset=UTF-8]
status: {code: 200, message: OK}
- request:
- body: '{"_source": {"exclude": ["*"]}, "query": {"multi_match": {"fields": ["name",
- "description"], "query": "[email protected]"}}}'
+ body: '{"query": {"multi_match": {"query": "[email protected]", "type":
+ "cross_fields", "operator": "and", "fields": ["user", "email", "first_name",
+ "last_name"]}}, "_source": {"exclude": ["*"]}}'
headers:
connection: [keep-alive]
content-type: [application/json]
method: GET
- uri: http://search:9200/storefront/product_document/_search
+ uri: http://search:9200/users/user_document/_search
response:
- body: {string: '{"took":0,"timed_out":false,"_shards":{"total":1,"successful":1,"failed":0},"hits":{"total":0,"max_score":null,"hits":[]}}'}
+ body: {string: '{"took":3,"timed_out":false,"_shards":{"total":1,"successful":1,"failed":0},"hits":{"total":1,"max_score":5.115776,"hits":[{"_index":"users","_type":"user_document","_id":"9","_score":5.115776,"_source":{}}]}}'}
headers:
Warning: ['299 Elasticsearch-5.4.3-eed30a8 "Deprecated field [exclude] used,
- expected [excludes] instead" "Wed, 15 Nov 2017 13:19:49 GMT"']
- content-length: ['122']
+ expected [excludes] instead" "Thu, 16 Nov 2017 09:44:26 GMT"']
+ content-length: ['209']
content-type: [application/json; charset=UTF-8]
status: {code: 200, message: OK}
version: 1
diff --git a/tests/cassettes/test_dashboard_search_with_empty_results.yaml b/tests/cassettes/test_dashboard_search_with_empty_results.yaml
index 9d018e4756..b74eae5448 100644
--- a/tests/cassettes/test_dashboard_search_with_empty_results.yaml
+++ b/tests/cassettes/test_dashboard_search_with_empty_results.yaml
@@ -11,7 +11,7 @@ interactions:
body: {string: '{"took":1,"timed_out":false,"_shards":{"total":1,"successful":1,"failed":0},"hits":{"total":0,"max_score":null,"hits":[]}}'}
headers:
Warning: ['299 Elasticsearch-5.4.3-eed30a8 "Deprecated field [exclude] used,
- expected [excludes] instead" "Wed, 15 Nov 2017 13:38:09 GMT"']
+ expected [excludes] instead" "Thu, 16 Nov 2017 09:44:24 GMT"']
content-length: ['122']
content-type: [application/json; charset=UTF-8]
status: {code: 200, message: OK}
@@ -24,25 +24,27 @@ interactions:
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":0,"max_score":null,"hits":[]}}'}
+ body: {string: '{"took":0,"timed_out":false,"_shards":{"total":1,"successful":1,"failed":0},"hits":{"total":0,"max_score":null,"hits":[]}}'}
headers:
Warning: ['299 Elasticsearch-5.4.3-eed30a8 "Deprecated field [exclude] used,
- expected [excludes] instead" "Wed, 15 Nov 2017 13:38:09 GMT"']
+ expected [excludes] instead" "Thu, 16 Nov 2017 09:44:24 GMT"']
content-length: ['122']
content-type: [application/json; charset=UTF-8]
status: {code: 200, message: OK}
- request:
- body: '{"query": {"match": {"user": "foo"}}, "_source": {"exclude": ["*"]}}'
+ body: '{"query": {"multi_match": {"query": "foo", "type": "cross_fields", "operator":
+ "and", "fields": ["user", "email", "first_name", "last_name"]}}, "_source":
+ {"exclude": ["*"]}}'
headers:
connection: [keep-alive]
content-type: [application/json]
method: GET
uri: http://search:9200/users/user_document/_search
response:
- body: {string: '{"took":0,"timed_out":false,"_shards":{"total":1,"successful":1,"failed":0},"hits":{"total":0,"max_score":null,"hits":[]}}'}
+ body: {string: '{"took":3,"timed_out":false,"_shards":{"total":1,"successful":1,"failed":0},"hits":{"total":0,"max_score":null,"hits":[]}}'}
headers:
Warning: ['299 Elasticsearch-5.4.3-eed30a8 "Deprecated field [exclude] used,
- expected [excludes] instead" "Wed, 15 Nov 2017 13:38:09 GMT"']
+ expected [excludes] instead" "Thu, 16 Nov 2017 09:44:24 GMT"']
content-length: ['122']
content-type: [application/json; charset=UTF-8]
status: {code: 200, message: OK}
diff --git a/tests/cassettes/test_dashboard_search_with_product_result.yaml b/tests/cassettes/test_dashboard_search_with_product_result.yaml
index 77b9e838ed..202153ac7c 100644
--- a/tests/cassettes/test_dashboard_search_with_product_result.yaml
+++ b/tests/cassettes/test_dashboard_search_with_product_result.yaml
@@ -8,11 +8,11 @@ interactions:
method: GET
uri: http://search:9200/storefront/product_document/_search
response:
- body: {string: '{"took":2,"timed_out":false,"_shards":{"total":1,"successful":1,"failed":0},"hits":{"total":15,"max_score":2.972128,"hits":[{"_index":"storefront","_type":"product_document","_id":"58","_score":2.972128,"_source":{}},{"_index":"storefront","_type":"product_document","_id":"83","_score":2.972128,"_source":{}},{"_index":"storefront","_type":"product_document","_id":"84","_score":2.972128,"_source":{}},{"_index":"storefront","_type":"product_document","_id":"122","_score":2.972128,"_source":{}},{"_index":"storefront","_type":"product_document","_id":"121","_score":2.972128,"_source":{}},{"_index":"storefront","_type":"product_document","_id":"127","_score":2.972128,"_source":{}},{"_index":"storefront","_type":"product_document","_id":"152","_score":2.972128,"_source":{}},{"_index":"storefront","_type":"product_document","_id":"162","_score":2.972128,"_source":{}},{"_index":"storefront","_type":"product_document","_id":"176","_score":2.972128,"_source":{}},{"_index":"storefront","_type":"product_document","_id":"177","_score":2.972128,"_source":{}}]}}'}
+ body: {string: '{"took":2,"timed_out":false,"_shards":{"total":1,"successful":1,"failed":0},"hits":{"total":3,"max_score":2.9128819,"hits":[{"_index":"storefront","_type":"product_document","_id":"23","_score":2.9128819,"_source":{}},{"_index":"storefront","_type":"product_document","_id":"58","_score":2.9128819,"_source":{}},{"_index":"storefront","_type":"product_document","_id":"56","_score":2.9128819,"_source":{}}]}}'}
headers:
Warning: ['299 Elasticsearch-5.4.3-eed30a8 "Deprecated field [exclude] used,
- expected [excludes] instead" "Wed, 15 Nov 2017 13:38:09 GMT"']
- content-length: ['1063']
+ expected [excludes] instead" "Thu, 16 Nov 2017 09:44:25 GMT"']
+ content-length: ['408']
content-type: [application/json; charset=UTF-8]
status: {code: 200, message: OK}
- request:
@@ -27,22 +27,24 @@ interactions:
body: {string: '{"took":1,"timed_out":false,"_shards":{"total":1,"successful":1,"failed":0},"hits":{"total":0,"max_score":null,"hits":[]}}'}
headers:
Warning: ['299 Elasticsearch-5.4.3-eed30a8 "Deprecated field [exclude] used,
- expected [excludes] instead" "Wed, 15 Nov 2017 13:38:09 GMT"']
+ expected [excludes] instead" "Thu, 16 Nov 2017 09:44:25 GMT"']
content-length: ['122']
content-type: [application/json; charset=UTF-8]
status: {code: 200, message: OK}
- request:
- body: '{"query": {"match": {"user": "Group"}}, "_source": {"exclude": ["*"]}}'
+ body: '{"query": {"multi_match": {"query": "Group", "type": "cross_fields", "operator":
+ "and", "fields": ["user", "email", "first_name", "last_name"]}}, "_source":
+ {"exclude": ["*"]}}'
headers:
connection: [keep-alive]
content-type: [application/json]
method: GET
uri: http://search:9200/users/user_document/_search
response:
- body: {string: '{"took":0,"timed_out":false,"_shards":{"total":1,"successful":1,"failed":0},"hits":{"total":0,"max_score":null,"hits":[]}}'}
+ body: {string: '{"took":1,"timed_out":false,"_shards":{"total":1,"successful":1,"failed":0},"hits":{"total":0,"max_score":null,"hits":[]}}'}
headers:
Warning: ['299 Elasticsearch-5.4.3-eed30a8 "Deprecated field [exclude] used,
- expected [excludes] instead" "Wed, 15 Nov 2017 13:38:09 GMT"']
+ expected [excludes] instead" "Thu, 16 Nov 2017 09:44:25 GMT"']
content-length: ['122']
content-type: [application/json; charset=UTF-8]
status: {code: 200, message: OK}
diff --git a/tests/test_search.py b/tests/test_search.py
index e3099a404e..1d5e944a2d 100644
--- a/tests/test_search.py
+++ b/tests/test_search.py
@@ -8,7 +8,7 @@ import pytest
MATCH_SEARCH_REQUEST = ['method', 'host', 'port', 'path', 'body']
STOREFRONT_PRODUCTS = {15, 56} # same as in recorded data!
-DASHBOARD_PRODUCTS = {58, 83, 84, 122, 162}
+DASHBOARD_PRODUCTS = {58, 56}
PRODUCTS_INDEXED = STOREFRONT_PRODUCTS | DASHBOARD_PRODUCTS
PRODUCTS_TO_UNPUBLISH = {56} # choose from PRODUCTS_INDEXED
PHRASE_WITH_RESULTS = 'Group'
@@ -120,13 +120,16 @@ def test_dashboard_search_with_product_result(db, indexed_products,
# data below must be aligned with recorded communication every time
-EXISTING_EMAIL = '[email protected]'
+EXISTING_EMAIL = '[email protected]'
NON_EXISTING_EMAIL = '[email protected]'
-USER_WITH_ORDER = '[email protected]'
+USER_WITH_ORDER = '[email protected]'
+EXISTING_NAME = 'Rhonda'
+EXISTING_NAME_FULL = '[email protected]'
USERS = {EXISTING_EMAIL: 9,
- NON_EXISTING_EMAIL: 100,
- USER_WITH_ORDER: 666}
-ORDERS = {USER_WITH_ORDER: 20}
+ NON_EXISTING_EMAIL: 870,
+ USER_WITH_ORDER: 666,
+ EXISTING_NAME_FULL: 6}
+ORDERS = {USER_WITH_ORDER: {18, 19}}
@pytest.fixture
@@ -143,14 +146,23 @@ def test_dashboard_search_user_by_email(db, admin_client, customers):
assert {USERS[EXISTING_EMAIL]} == users
[email protected](record_mode='once', match_on=MATCH_SEARCH_REQUEST)
+def test_dashboard_search_user_name(db, admin_client, customers):
+ ''' user can be found in dashboard search by partial name match '''
+ _, users, _ = execute_dashboard_search(admin_client, EXISTING_NAME)
+ assert USERS[EXISTING_NAME_FULL] in users
+
+
@pytest.fixture
def orders(db, billing_address):
- pks = set()
- for email, pk in ORDERS.items():
- Order.objects.create(
- billing_address=billing_address, user_email=email, pk=pk)
- pks.add(pk)
- return pks
+ all_pks = set()
+ for email, pks in ORDERS.items():
+ for pk in pks:
+ Order.objects.create(
+ billing_address=billing_address, user_email=email, pk=pk)
+ all_pks = all_pks | pks
+ return all_pks
@pytest.mark.integration
@@ -158,5 +170,4 @@ def orders(db, billing_address):
def test_dashboard_search_orders_by_user(db, admin_client, customers, orders):
''' order can be found in dashboard search by customers email '''
_, _, orders = execute_dashboard_search(admin_client, USER_WITH_ORDER)
- assert 1 == len(orders)
- assert ORDERS[USER_WITH_ORDER] in orders
+ assert ORDERS[USER_WITH_ORDER] == orders