Skip to content

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

Retrieve specific objects from search query

Jakub Draganekd55805fb4faa
diff --git a/saleor/search/backends/newelastic.py b/saleor/search/backends/newelastic.py
index bf676442e0..b72b3bf327 100644
--- a/saleor/search/backends/newelastic.py
+++ b/saleor/search/backends/newelastic.py
@@ -3,15 +3,19 @@ from django.utils.six.moves.urllib.parse import urlparse
from elasticsearch_dsl import Search
from elasticsearch_dsl.query import MultiMatch
+from ...product.models import Product
+
+
CLIENT = Elasticsearch()
def search(phrase):
result = (Search()
.query(MultiMatch(query=phrase, fields=['name', 'description']))
+ .source(['pk'])
.using(CLIENT)
.execute())
- return []
+ return [hit.pk for hit in result]
def _get_es_hosts(params):
@@ -38,7 +42,10 @@ class SearchBackend(object):
rebuilder_class = None
def __init__(self, params):
+ global CLIENT
CLIENT = Elasticsearch(hosts=_get_es_hosts(params))
def search(self, query, model_or_queryset=None):
- return []
+ found_product_ids = search(query)
+ products = Product.objects.filter(pk__in=found_product_ids)
+ return [str(obj.pk) for obj in products]
diff --git a/saleor/search/backends/test_newelastic.py b/saleor/search/backends/test_newelastic.py
index 5ca0d92919..8b1ff4edf7 100644
--- a/saleor/search/backends/test_newelastic.py
+++ b/saleor/search/backends/test_newelastic.py
@@ -10,10 +10,11 @@ def search_phrase():
@pytest.fixture
def es_search_query(search_phrase):
query = {
+ '_source': ['pk'],
'query': {
'multi_match': {
'fields': ['name', 'description'],
- 'query': search_phrase
+ 'query': 'How fortunate man with none'
}
}
}
diff --git a/tests/cassettes/test_new_search_with_empty_results.yaml b/tests/cassettes/test_new_search_with_empty_results.yaml
new file mode 100644
index 0000000000..97437b606d
--- /dev/null
+++ b/tests/cassettes/test_new_search_with_empty_results.yaml
@@ -0,0 +1,16 @@
+interactions:
+- request:
+ body: '{"query": {"multi_match": {"query": "foo", "fields": ["name", "description"]}},
+ "_source": ["pk"]}'
+ headers:
+ connection: [keep-alive]
+ content-type: [application/json]
+ method: GET
+ uri: http://search:9200/_search
+ response:
+ body: {string: '{"took":3,"timed_out":false,"_shards":{"total":15,"successful":15,"failed":0},"hits":{"total":0,"max_score":null,"hits":[]}}'}
+ headers:
+ content-length: ['124']
+ content-type: [application/json; charset=UTF-8]
+ status: {code: 200, message: OK}
+version: 1
diff --git a/tests/cassettes/test_new_search_with_results.yaml b/tests/cassettes/test_new_search_with_results.yaml
new file mode 100644
index 0000000000..29365b118f
--- /dev/null
+++ b/tests/cassettes/test_new_search_with_results.yaml
@@ -0,0 +1,16 @@
+interactions:
+- request:
+ body: '{"_source": ["pk"], "query": {"multi_match": {"fields": ["name", "description"],
+ "query": "group"}}}'
+ headers:
+ connection: [keep-alive]
+ content-type: [application/json]
+ method: GET
+ uri: http://search:9200/_search
+ response:
+ body: {string: '{"took":5,"timed_out":false,"_shards":{"total":15,"successful":15,"failed":0},"hits":{"total":4,"max_score":3.1094596,"hits":[{"_index":"storefront__product_product","_type":"product_product","_id":"product_product:60","_score":3.1094596,"_source":{"pk":"60"}},{"_index":"storefront__product_product","_type":"product_product","_id":"product_product:9","_score":2.7236264,"_source":{"pk":"9"}},{"_index":"storefront__product_product","_type":"product_product","_id":"product_product:35","_score":2.7236264,"_source":{"pk":"35"}},{"_index":"storefront__product_product","_type":"product_product","_id":"product_product:8","_score":2.5116475,"_source":{"pk":"8"}}]}}'}
+ headers:
+ content-length: ['664']
+ content-type: [application/json; charset=UTF-8]
+ status: {code: 200, message: OK}
+version: 1
diff --git a/tests/test_search.py b/tests/test_search.py
index 0f59c64e9a..065922d634 100644
--- a/tests/test_search.py
+++ b/tests/test_search.py
@@ -4,12 +4,12 @@ from django.core.urlresolvers import reverse
from decimal import Decimal
import pytest
-import vcr
MATCH_SEARCH_REQUEST = ['method', 'host', 'port', 'path', 'body']
PRODUCTS_FOUND = [41, 59] # same as in recorded data!
DATE_OF_RECORDING = '2017-10-18'
+
@pytest.fixture(scope='function', autouse=True)
def recorded_on_date(freezer):
'''Freeze date during tests to date of recording
@@ -66,9 +66,7 @@ def indexed_products(product_class, default_category):
def _extract_pks(object_list):
- def get_pk(prod):
- return prod.pk
- return [get_pk(prod) for prod, _ in object_list]
+ return [prod.pk for prod, _ in object_list]
@pytest.mark.integration
@@ -91,10 +89,17 @@ def new_search_backend():
@pytest.mark.integration
[email protected](record_mode='none', match_on=MATCH_SEARCH_REQUEST)
[email protected]_cassette('test_search_with_empty_results.yaml')
[email protected](record_mode='once', match_on=MATCH_SEARCH_REQUEST)
def test_new_search_with_empty_results(db, client, new_search_backend):
WORD = 'foo'
response = client.get(reverse('search:search'), {'q': WORD})
assert 0 == len(response.context['results'].object_list)
assert WORD == response.context['query']
+
+
[email protected](record_mode='once', match_on=MATCH_SEARCH_REQUEST)
+def test_new_search_with_results(db, client, new_search_backend):
+ WORD = 'group'
+ response = client.get(reverse('search:search'), {'q': WORD})
+ _extract_pks(response.context['results'].object_list)