added filter by shipping cost to product search

This commit is contained in:
Sam
2021-02-23 11:57:25 +00:00
parent 1c38d81a5f
commit 7ca180f8e6
3 changed files with 106 additions and 17 deletions

View File

@@ -125,6 +125,37 @@ def find_related_products_v3(keyword):
return set(products_qs)
def find_related_products_v6(keyword, shipping_cost=None):
"""
Ranked product search
SearchVectors for the fields
SearchQuery for the value
SearchRank for relevancy scoring and ranking
allow filtering by:
- shipping cost
"""
vector = SearchVector('name') + SearchVector('description') + SearchVector('tags__label') + SearchVector('attributes__label') + SearchVector('category__name')
query = SearchQuery(keyword)
products_qs = Product.objects.annotate(
rank=SearchRank(vector, query)
).filter(rank__gt=0.05) # removed order_by because its lost in casting
if shipping_cost is True:
# only instances with shipping costs
products_qs = products_qs.filter(
Q(shipping_cost__isnull=False)&
Q(shipping_cost__gte=1)
)
elif shipping_cost is False:
# only intances without shpping costs
products_qs = products_qs.filter(Q(shipping_cost=None)|Q(shipping_cost=0.00))
return set(products_qs)
def find_related_products_v4(keyword):
"""
Similarity-ranked search using trigrams