improvements to search functionality
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -161,6 +161,8 @@ 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 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']:
|
||||
|
||||
Reference in New Issue
Block a user