From 8de48c6698b3ce904dd98fe0281c4c9e6a52940a Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 11 Mar 2021 12:17:40 +0000 Subject: [PATCH] more tweaks for geo search --- products/tests.py | 12 ++++++------ products/utils.py | 8 +------- products/views.py | 4 ++-- 3 files changed, 9 insertions(+), 15 deletions(-) diff --git a/products/tests.py b/products/tests.py index b7eb9ae..a8c7cf2 100644 --- a/products/tests.py +++ b/products/tests.py @@ -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 diff --git a/products/utils.py b/products/utils.py index 431d368..fd9569b 100644 --- a/products/utils.py +++ b/products/utils.py @@ -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) diff --git a/products/views.py b/products/views.py index fd14ced..4b73d3d 100644 --- a/products/views.py +++ b/products/views.py @@ -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)