Skip to content

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

Make product search query more specific

Jakub Draganek69b98fbc633e
diff --git a/saleor/search/backends/newelastic.py b/saleor/search/backends/newelastic.py
index 76a870ef0d..17dfeafafa 100644
--- a/saleor/search/backends/newelastic.py
+++ b/saleor/search/backends/newelastic.py
@@ -6,26 +6,26 @@ from elasticsearch_dsl.query import MultiMatch
CLIENT = Elasticsearch()
-def search(phrase):
- query = MultiMatch(query=phrase, fields=['name', 'description'])
+def search_products(phrase):
+ query = MultiMatch(
+ index='storefront__product_product',
+ fields=['name', 'description'],
+ query=phrase)
result = Search().query(query).source(['pk']).using(CLIENT).execute()
return [hit.pk for hit in result]
def _get_es_hosts(params):
- hosts = params.pop('HOSTS', None) or []
- es_urls = params.pop('URLS', ['http://localhost:9200'])
- for url in es_urls:
- parsed_url = urlparse(url)
- use_ssl = parsed_url.scheme == 'https'
- port = parsed_url.port or (443 if use_ssl else 80)
- http_auth = None
- if parsed_url.username and parsed_url.password:
- http_auth = (parsed_url.username, parsed_url.password)
+ hosts = params.pop('HOSTS', [])
+ es_urls = params.pop('URLS', [])
+ for url in map(urlparse, es_urls):
+ use_ssl = url.scheme == 'https'
+ auth = (url.username, url.password)
+ http_auth = auth if all(auth) else None
hosts.append({
- 'host': parsed_url.hostname,
- 'port': port,
- 'url_prefix': parsed_url.path,
+ 'host': url.hostname,
+ 'port': url.port or (443 if use_ssl else 80),
+ 'url_prefix': url.path,
'use_ssl': use_ssl,
'verify_certs': use_ssl,
'http_auth': http_auth
@@ -41,4 +41,4 @@ class SearchBackend(object):
CLIENT = Elasticsearch(hosts=_get_es_hosts(params))
def search(self, query, model_or_queryset):
- return model_or_queryset.filter(pk__in=search(query))
+ return model_or_queryset.filter(pk__in=search_products(query))
diff --git a/saleor/search/backends/test_newelastic.py b/saleor/search/backends/test_newelastic.py
index 8b1ff4edf7..1b5407c3bb 100644
--- a/saleor/search/backends/test_newelastic.py
+++ b/saleor/search/backends/test_newelastic.py
@@ -10,20 +10,21 @@ def search_phrase():
@pytest.fixture
def es_search_query(search_phrase):
query = {
- '_source': ['pk'],
'query': {
'multi_match': {
- 'fields': ['name', 'description'],
- 'query': 'How fortunate man with none'
+ 'query': 'How fortunate man with none',
+ 'index': 'storefront__product_product',
+ 'fields': ['name', 'description']
}
- }
+ },
+ '_source': ['pk']
}
return query
def test_no_result_search(mocker, es_search_query, search_phrase):
mocker.patch.object(newelastic.CLIENT, 'search', returnvalue=[])
- products = newelastic.search(search_phrase)
+ products = newelastic.search_products(search_phrase)
newelastic.CLIENT.search.assert_called_once_with(
body=es_search_query, doc_type=[], index=None)
assert not products