search optimization broken by tags

This commit is contained in:
Sam
2021-02-04 14:10:51 +00:00
parent 67ce92c33f
commit b9822edc8d
3 changed files with 28 additions and 14 deletions

View File

@@ -25,7 +25,7 @@ class ProductFactory(DjangoModelFactory):
update_date = FuzzyDateTime(start_dt=timezone.now()) update_date = FuzzyDateTime(start_dt=timezone.now())
discount = FuzzyDecimal(low=0.00, high=100.00) discount = FuzzyDecimal(low=0.00, high=100.00)
stock = FuzzyInteger(low=0) stock = FuzzyInteger(low=0)
tags = ['test-tag'] tags = ['test']
category = 'top-category' # main tag category category = 'top-category' # main tag category
attributes = ['programming/python', 'testing'] attributes = ['programming/python', 'testing']
identifiers = FuzzyText(prefix='IDENTIFIERS_', length=100) identifiers = FuzzyText(prefix='IDENTIFIERS_', length=100)

View File

@@ -380,6 +380,7 @@ class ProductSearchTest(TestCase):
def test_anon_user_can_search(self): def test_anon_user_can_search(self):
self.factory(description="zapatos") self.factory(description="zapatos")
self.factory(tags="rojos") self.factory(tags="rojos")
# self.factory(tags="azul")
query_string = quote("zapatos rojos") query_string = quote("zapatos rojos")
@@ -390,7 +391,8 @@ class ProductSearchTest(TestCase):
# check re sponse # check re sponse
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
# check for object creation # check for object creation
self.assertEquals(2, self.model.objects.count()) data = json.loads(response.data)
self.assertEquals(len(data), self.model.objects.count())
class MyProductsViewTest(APITestCase): class MyProductsViewTest(APITestCase):

View File

@@ -2,6 +2,7 @@ import logging
import csv import csv
import datetime import datetime
import operator import operator
from functools import reduce
from django.shortcuts import render from django.shortcuts import render
from django.conf import settings from django.conf import settings
@@ -142,18 +143,29 @@ def product_search(request):
result_set = set() result_set = set()
# split query string into single words # split query string into single words
chunks = query_string.split(' ') chunks = query_string.split(' ')
# create search queries
name_search = Q(name__in=chunks) for chunk in chunks:
description_search = Q(description__in=chunks) # search in name
tags_search = Q(tags__in=chunks) products = Product.objects.filter(name=chunk)
category_search = Q(category__in=chunks) for item in products:
attributes_search = Q(attributes__in=chunks) result_set.add(item)
query_list = [name_search, description_search, tags_search, category_search, attributes_search] # search in description
# get instances products = Product.objects.filter(description=chunk)
products = Product.objects.filter(reduce(operator.or_, query_list)) for item in products:
# add to set result_set.add(item)
for item in products: # search in tags
result_set.add(item) products = Product.objects.filter(tags=chunk)
for item in products:
result_set.add(item)
# search in category
products = Product.objects.filter(category=chunk)
for item in products:
result_set.add(item)
# search in attributes
products = Product.objects.filter(attributes=chunk)
for item in products:
result_set.add(item)
# serialize and respond # serialize and respond
data = serializers.serialize('json', result_set) data = serializers.serialize('json', result_set)
return Response(data=data) return Response(data=data)