search optimization broken by tags
This commit is contained in:
@@ -25,7 +25,7 @@ class ProductFactory(DjangoModelFactory):
|
||||
update_date = FuzzyDateTime(start_dt=timezone.now())
|
||||
discount = FuzzyDecimal(low=0.00, high=100.00)
|
||||
stock = FuzzyInteger(low=0)
|
||||
tags = ['test-tag']
|
||||
tags = ['test']
|
||||
category = 'top-category' # main tag category
|
||||
attributes = ['programming/python', 'testing']
|
||||
identifiers = FuzzyText(prefix='IDENTIFIERS_', length=100)
|
||||
|
||||
@@ -380,6 +380,7 @@ class ProductSearchTest(TestCase):
|
||||
def test_anon_user_can_search(self):
|
||||
self.factory(description="zapatos")
|
||||
self.factory(tags="rojos")
|
||||
# self.factory(tags="azul")
|
||||
|
||||
query_string = quote("zapatos rojos")
|
||||
|
||||
@@ -390,7 +391,8 @@ class ProductSearchTest(TestCase):
|
||||
# check re sponse
|
||||
self.assertEqual(response.status_code, 200)
|
||||
# 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):
|
||||
|
||||
@@ -2,6 +2,7 @@ import logging
|
||||
import csv
|
||||
import datetime
|
||||
import operator
|
||||
from functools import reduce
|
||||
|
||||
from django.shortcuts import render
|
||||
from django.conf import settings
|
||||
@@ -142,18 +143,29 @@ def product_search(request):
|
||||
result_set = set()
|
||||
# split query string into single words
|
||||
chunks = query_string.split(' ')
|
||||
# create search queries
|
||||
name_search = Q(name__in=chunks)
|
||||
description_search = Q(description__in=chunks)
|
||||
tags_search = Q(tags__in=chunks)
|
||||
category_search = Q(category__in=chunks)
|
||||
attributes_search = Q(attributes__in=chunks)
|
||||
query_list = [name_search, description_search, tags_search, category_search, attributes_search]
|
||||
# get instances
|
||||
products = Product.objects.filter(reduce(operator.or_, query_list))
|
||||
# add to set
|
||||
|
||||
for chunk in chunks:
|
||||
# search in name
|
||||
products = Product.objects.filter(name=chunk)
|
||||
for item in products:
|
||||
result_set.add(item)
|
||||
# search in description
|
||||
products = Product.objects.filter(description=chunk)
|
||||
for item in products:
|
||||
result_set.add(item)
|
||||
# search in tags
|
||||
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
|
||||
data = serializers.serialize('json', result_set)
|
||||
return Response(data=data)
|
||||
|
||||
Reference in New Issue
Block a user