producing separate filters for tags and attributes in product search

This commit is contained in:
Sam
2021-02-25 11:51:07 +00:00
parent 8d471830eb
commit 07644dfc52
2 changed files with 18 additions and 14 deletions

View File

@@ -549,8 +549,8 @@ class ProductSearchTest(TestCase):
self.assertTrue(payload['products'][i]['rank'] <= current ) self.assertTrue(payload['products'][i]['rank'] <= current )
current = payload['products'][i]['rank'] current = payload['products'][i]['rank']
# check for filters # check for filters
self.assertNotEquals([], payload['filters']['singles']) self.assertNotEquals([], payload['filters']['tags']['singles'])
self.assertTrue(len(payload['filters']) >= 2 ) self.assertTrue(len(payload['filters']['tags']) >= 2 )
def test_anon_user_can_paginate_search(self): def test_anon_user_can_paginate_search(self):
expected_instances = [ expected_instances = [

View File

@@ -12,13 +12,17 @@ def extract_search_filters(result_set):
Returned object should look something like: Returned object should look something like:
{ {
"singles": [], # non tree tags "tags": [],
"entry_1": [ 'tag1', 'tag2' ], "attributes": [],
"entry_2": [ 'tag1', 'tag2' ],
} }
""" """
filter_dict = { filter_dict = {
"tags": {
'singles': set(), 'singles': set(),
},
"attributes": {
'singles': set(),
}
} }
for item in result_set: for item in result_set:
try: try:
@@ -26,32 +30,32 @@ def extract_search_filters(result_set):
tags = item.tags.all() tags = item.tags.all()
for tag in tags: for tag in tags:
if len(tag.name.split('/')) == 1: if len(tag.name.split('/')) == 1:
filter_dict['singles'].add(tag.name) filter_dict['tags']['singles'].add(tag.name)
else: else:
# set penultimate tag as header # set penultimate tag as header
chunks = tag.name.split('/') chunks = tag.name.split('/')
header = chunks[-2] header = chunks[-2]
name = chunks[-1] name = chunks[-1]
# check if # check if
entry = filter_dict.get(header) entry = filter_dict['tags'].get(header)
if entry is None: if entry is None:
filter_dict[header] = set() filter_dict['tags'][header] = set()
filter_dict[header].add(name) filter_dict['tags'][header].add(name)
# extract attributes # extract attributes
attributes = item.attributes.all() attributes = item.attributes.all()
for tag in attributes: for tag in attributes:
if len(tag.name.split('/')) == 1: if len(tag.name.split('/')) == 1:
filter_dict['singles'].add(tag.name) filter_dict['attributes']['singles'].add(tag.name)
else: else:
# set penultimate tag as header # set penultimate tag as header
chunks = tag.name.split('/') chunks = tag.name.split('/')
header = chunks[-2] header = chunks[-2]
name = chunks[-1] name = chunks[-1]
# check if # check if
entry = filter_dict.get(header) entry = filter_dict['attributes'].get(header)
if entry is None: if entry is None:
filter_dict[header] = set() filter_dict['attributes'][header] = set()
filter_dict[header].add(name) filter_dict['attributes'][header].add(name)
except Exception as e: except Exception as e:
logging.error(f'Extacting filters for {item}') logging.error(f'Extacting filters for {item}')
return filter_dict return filter_dict