From 9baa13f30a0f6ff0bb35a462b37fc157a0614e31 Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 2 Mar 2021 11:01:22 +0000 Subject: [PATCH] improvements to search functionality --- products/tests.py | 8 +++--- products/utils.py | 64 ----------------------------------------------- products/views.py | 14 ++++++----- 3 files changed, 12 insertions(+), 74 deletions(-) diff --git a/products/tests.py b/products/tests.py index 81f7916..2c23bc4 100644 --- a/products/tests.py +++ b/products/tests.py @@ -675,9 +675,9 @@ class ProductSearchTest(TestCase): def test_anon_user_can_filter_by_category(self): expected_instances = [ - self.factory(tags="lunares/rojos", category='ropa', description="zapatos verdes", discount=None), - self.factory(tags="lunares/rojos", category="ropa", discount=0.00), - self.factory(attributes='"zapatos de campo", tono/oscuro', category="ropa", discount=9.00), + self.factory(tags="lunares/rojos", category='ropa/nueva', description="zapatos verdes", discount=None), + self.factory(tags="lunares/rojos", category="ropa/nueva", discount=0.00), + self.factory(attributes='"zapatos de campo", tono/oscuro', category="ropa/nueva", discount=9.00), ] unexpected_instances = [ self.factory(description="chanclas", tags='rojos'), @@ -686,7 +686,7 @@ class ProductSearchTest(TestCase): q = quote("zapatos rojos") # discount=true - url = f"{self.endpoint}?q={q}&category=ropa" + url = f"{self.endpoint}?q={q}&category=ropa/nueva" # send in request response = self.client.get(url) # check response diff --git a/products/utils.py b/products/utils.py index bf05725..d0ad2e0 100644 --- a/products/utils.py +++ b/products/utils.py @@ -85,56 +85,6 @@ def extract_search_filters(result_set): return filter_dict -def find_related_products_v1(keyword): - """ - Classical approach to the search - - Using Q objects - - """ - # search in tags - tags = Product.tags.tag_model.objects.filter(name__icontains=keyword) - # search in category - categories = Product.category.tag_model.objects.filter(name__icontains=keyword) - # search in attributes - attributes = Product.attributes.tag_model.objects.filter(name__icontains=keyword) - # unified tag search - products_qs = Product.objects.filter( - Q(name__icontains=keyword)| - Q(description__icontains=keyword)| - Q(tags__in=tags)| - Q(category__in=categories)| - Q(attributes__in=attributes) - ) - return products_qs - - -def find_related_products_v5(keyword): - """ - Single query solution, using Q objects - """ - products_qs = Product.objects.filter( - Q(name__icontains=keyword)| - Q(description__icontains=keyword)| - Q(tags__label__icontains=keyword)| - Q(category__name__icontains=keyword)| - Q(attributes__label__icontains=keyword) - ) - return set(products_qs) - - -def find_related_products_v2(keyword): - """ - More advanced: using search vectors - """ - fields=('name', 'description', 'tags__label', 'attributes__label', 'category__name') - vector = SearchVector(*fields) - products_qs = Product.objects.annotate( - search=vector - ).filter(search=keyword) - return set(products_qs) - - def find_related_products_v3(keyword): """ Ranked product search @@ -215,20 +165,6 @@ def find_related_products_v6(keyword, shipping_cost=None, discount=None, categor return set(products_qs), min_price, max_price -def find_related_products_v4(keyword): - """ - Similarity-ranked search using trigrams - Not working - """ - # fields=('name', 'description', 'tags__label', 'attributes__label', 'category__name') - - products_qs = Product.objects.annotate( - similarity=TrigramSimilarity('name', keyword), - ).order_by('-similarity') - - return set(products_qs) - - def product_loader(csv_reader, user, company=None): """ Parse csv data and extract: diff --git a/products/views.py b/products/views.py index c730bd1..226c55d 100644 --- a/products/views.py +++ b/products/views.py @@ -161,12 +161,14 @@ def product_search(request): for chunk in chunks: product_set, min_price, max_price = find_related_products_v6(chunk, shipping_cost, discount, category, tags, price_min, price_max) # update price values - if prices['min'] is None or min_price['price__min'] < prices['min']: - prices['min'] = min_price['price__min'] - if prices['max'] is None or max_price['price__max'] > prices['max']: - prices['max'] = max_price['price__max'] - # add to result set - result_set.update(product_set) + if product_set: + # import ipdb; ipdb.set_trace() + if prices['min'] is None or min_price['price__min'] < prices['min']: + prices['min'] = min_price['price__min'] + if prices['max'] is None or max_price['price__max'] > prices['max']: + prices['max'] = max_price['price__max'] + # add to result set + result_set.update(product_set) # TODO: add search for entire phrase ??? # extract filters from result_set