Skip to content

Commit

Permalink
Retrieve specific objects from search query
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
JakubDraganek committed Nov 22, 2017
1 parent be38e64 commit d55805f
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 9 deletions.
11 changes: 9 additions & 2 deletions saleor/search/backends/newelastic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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]
3 changes: 2 additions & 1 deletion saleor/search/backends/test_newelastic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
}
}
Expand Down
16 changes: 16 additions & 0 deletions tests/cassettes/test_new_search_with_empty_results.yaml
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions tests/cassettes/test_new_search_with_results.yaml
Original file line number Diff line number Diff line change
@@ -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
17 changes: 11 additions & 6 deletions tests/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)

0 comments on commit d55805f

Please sign in to comment.