more tweaks for geo search

This commit is contained in:
Sam
2021-03-11 12:17:40 +00:00
parent c343bda367
commit 8de48c6698
3 changed files with 9 additions and 15 deletions

View File

@@ -944,12 +944,9 @@ class ProductSearchTest(TestCase):
company = CompanyFactory(geo=point)
expected_instances = [
self.factory(tags="lunares/rojos", category='zapatos', description="zapatos verdes", company=company),
self.factory(tags="colores/rojos, tono/brillante", company=company),
self.factory(tags="lunares/azules", description="zapatos rojos", company=company),
self.factory(tags="lunares/rojos", description="zapatos", company=company),
self.factory(attributes='"zapatos de campo", tono/rojo', company=company),
self.factory(tags="lunares/rojos", category='zapatos', description="zapatos verdes", company=company) for i in range(12)
]
unexpected_instances = [
self.factory(description="chanclas"),
self.factory(tags="azules"),
@@ -962,6 +959,9 @@ class ProductSearchTest(TestCase):
response = self.client.get(url)
# check response
self.assertEqual(response.status_code, 200)
payload = response.json()
self.assertIsNotNone(payload['georesult'])
self.assertEquals(payload['georesult'], '10k')
class MyProductsViewTest(APITestCase):
"""my_products tests

View File

@@ -145,13 +145,7 @@ def ranked_product_search(keywords, shipping_cost=None, discount=None, category=
"""
vector = SearchVector('name') + SearchVector('description') + SearchVector('tags__label') + SearchVector('attributes__label') + SearchVector('category__label') + SearchVector('company__company_name')
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')
query = SearchQuery(keywords, search_type='plain')
products_qs = Product.objects.annotate(
rank=SearchRank(vector, query)

View File

@@ -208,7 +208,7 @@ def product_search(request):
# split query string into single words
chunks = q.split(' ')
# all-in-one search
product_set, min_price, max_price, georesult = ranked_product_search(chunks, shipping_cost, discount, category, tags, price_min, price_max, coordinates)
product_set, min_price, max_price, georesult = ranked_product_search(q, shipping_cost, discount, category, tags, price_min, price_max, coordinates)
# update price values
if product_set:
if prices['min'] is None or min_price['price__min'] < prices['min']:
@@ -243,7 +243,7 @@ def product_search(request):
elif limit is not None:
limit = int(limit)
result_list = result_list[:limit]
return Response(data={"filters": filters, "count": total_results, "products": result_list, 'prices': prices})
return Response(data={"filters": filters, "count": total_results, "products": result_list, 'prices': prices, 'georesult': georesult})
except Exception as e:
import ipdb; ipdb.set_trace()
return Response({"errors": {"details": str(e)}}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)