search tests broken, but geo search working
This commit is contained in:
@@ -126,7 +126,7 @@ def get_related_products(product):
|
||||
return total_results[:10]
|
||||
|
||||
|
||||
def ranked_product_search(keyword, shipping_cost=None, discount=None, category=None, tags=None, price_min=None,price_max=None, coordinates=None):
|
||||
def ranked_product_search(keywords, shipping_cost=None, discount=None, category=None, tags=None, price_min=None,price_max=None, coordinates=None):
|
||||
"""
|
||||
Ranked product search
|
||||
|
||||
@@ -145,28 +145,29 @@ def ranked_product_search(keyword, shipping_cost=None, discount=None, category=N
|
||||
"""
|
||||
vector = SearchVector('name') + SearchVector('description') + SearchVector('tags__label') + SearchVector('attributes__label') + SearchVector('category__label') + SearchVector('company__company_name')
|
||||
|
||||
query_string = ''
|
||||
for word in keyword:
|
||||
query_string += f" | '{keyword}' "
|
||||
|
||||
query = SearchQuery(query_string)
|
||||
if keywords and len(keywords) == 1:
|
||||
query_string = keywords[0]
|
||||
else:
|
||||
query_string = keywords[0]
|
||||
for i in range(1, len(keywords)):
|
||||
query_string += f" | {keywords[i]} "
|
||||
query = SearchQuery(query_string, search_type='raw')
|
||||
|
||||
products_qs = Product.objects.annotate(
|
||||
rank=SearchRank(vector, query)
|
||||
).filter(rank__gt=0.05, active=True)
|
||||
|
||||
# geolocation filtering
|
||||
if coordinates is not None:
|
||||
point = Point(coordinates)
|
||||
filtered_qs = products_qs.filter(geo__distance_lte=(point, D(km=10)))
|
||||
filtered_qs = products_qs.filter(company__geo__distance_lte=(point, D(km=10)))
|
||||
georesult = '10k'
|
||||
if filtered_qs.count() <= 10:
|
||||
products_qs = products_qs.filter(geo__distance_lte=(point, D(km=50)))
|
||||
products_qs = products_qs.filter(company__geo__distance_lte=(point, D(km=50)))
|
||||
georesult = '50k'
|
||||
if filtered_qs.count() <= 10:
|
||||
products_qs = products_qs.filter(geo__distance_lte=(point, D(km=200)))
|
||||
products_qs = products_qs.filter(company__geo__distance_lte=(point, D(km=200)))
|
||||
georesult = '200k'
|
||||
if filtered_qs.count > 10:
|
||||
if filtered_qs.count() > 10:
|
||||
products_qs = filtered_qs
|
||||
else:
|
||||
georesult = None
|
||||
|
||||
Reference in New Issue
Block a user