From d55805fb4faa605d8868a3576427cc865cd96d01 Mon Sep 17 00:00:00 2001 From: Jakub Draganek Date: Tue, 31 Oct 2017 11:05:20 +0100 Subject: [PATCH] Retrieve specific objects from search query - limit source included to pk - record new cassettes - due to changes in used ES query cassettes wont be reused - do first integration of new backend with rest of search view --- saleor/search/backends/newelastic.py | 11 +++++++++-- saleor/search/backends/test_newelastic.py | 3 ++- .../test_new_search_with_empty_results.yaml | 16 ++++++++++++++++ .../cassettes/test_new_search_with_results.yaml | 16 ++++++++++++++++ tests/test_search.py | 17 +++++++++++------ 5 files changed, 54 insertions(+), 9 deletions(-) create mode 100644 tests/cassettes/test_new_search_with_empty_results.yaml create mode 100644 tests/cassettes/test_new_search_with_results.yaml diff --git a/saleor/search/backends/newelastic.py b/saleor/search/backends/newelastic.py index bf676442e0a..b72b3bf3275 100644 --- a/saleor/search/backends/newelastic.py +++ b/saleor/search/backends/newelastic.py @@ -3,15 +3,19 @@ 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 5ca0d929193..8b1ff4edf7c 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 00000000000..97437b606d2 --- /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 00000000000..29365b118fb --- /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 0f59c64e9a0..065922d6342 100644 --- a/tests/test_search.py +++ b/tests/test_search.py @@ -4,12 +4,12 @@ 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 gen_product_with_id(object_id): 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 -@pytest.mark.vcr(record_mode='none', match_on=MATCH_SEARCH_REQUEST) -@vcr.use_cassette('test_search_with_empty_results.yaml') +@pytest.mark.vcr(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'] + + +@pytest.mark.integration +@pytest.mark.vcr(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)