fixes addtestdata, working on vectorized database search
This commit is contained in:
@@ -66,7 +66,7 @@ By default a `LimitOffsetPagination` pagination is enabled
|
|||||||
|
|
||||||
Examples: `http://127.0.0.1:8000/api/v1/products/?limit=10&offset=0`
|
Examples: `http://127.0.0.1:8000/api/v1/products/?limit=10&offset=0`
|
||||||
|
|
||||||
The response data has the following keyspayload:
|
The response data has the following keys:
|
||||||
```
|
```
|
||||||
dict_keys(['count', 'next', 'previous', 'results'])
|
dict_keys(['count', 'next', 'previous', 'results'])
|
||||||
```
|
```
|
||||||
@@ -294,4 +294,6 @@ To create a dataset of fake companies and products:
|
|||||||
|
|
||||||
`python manage.py addtestdata`
|
`python manage.py addtestdata`
|
||||||
|
|
||||||
Creates 10 Companies, with 100 products each.
|
Creates 10 Companies, with 10 products each.
|
||||||
|
|
||||||
|
WARNING: the script deletes existing instances of both Company and Product
|
||||||
|
|||||||
@@ -42,6 +42,7 @@ INSTALLED_APPS = [
|
|||||||
'django.contrib.messages',
|
'django.contrib.messages',
|
||||||
'django.contrib.staticfiles',
|
'django.contrib.staticfiles',
|
||||||
'django.contrib.gis',
|
'django.contrib.gis',
|
||||||
|
'django.contrib.postgres',
|
||||||
|
|
||||||
# 3rd party
|
# 3rd party
|
||||||
'rest_framework',
|
'rest_framework',
|
||||||
|
|||||||
@@ -70,7 +70,10 @@ class Command(BaseCommand):
|
|||||||
# TODO: apply automatic tags from tag list
|
# TODO: apply automatic tags from tag list
|
||||||
# TODO: write image to S3 storage
|
# TODO: write image to S3 storage
|
||||||
# create instance
|
# create instance
|
||||||
product = ProductFactory(name=name, description=description)
|
product = ProductFactory(
|
||||||
|
company=company,
|
||||||
|
name=name,
|
||||||
|
description=description)
|
||||||
|
|
||||||
# get image
|
# get image
|
||||||
response = requests.get(self.logo_url, stream=True)
|
response = requests.get(self.logo_url, stream=True)
|
||||||
|
|||||||
@@ -477,6 +477,9 @@ class ProductSearchTest(TestCase):
|
|||||||
url = f"{self.endpoint}?query_string={query_string}"
|
url = f"{self.endpoint}?query_string={query_string}"
|
||||||
# send in request
|
# send in request
|
||||||
response = self.client.get(url)
|
response = self.client.get(url)
|
||||||
|
|
||||||
|
import ipdb; ipdb.set_trace()
|
||||||
|
|
||||||
payload = response.json()
|
payload = response.json()
|
||||||
# check response
|
# check response
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import logging
|
import logging
|
||||||
|
|
||||||
from django.db.models import Q
|
from django.db.models import Q
|
||||||
|
from django.contrib.postgres.search import SearchQuery, SearchRank, SearchVector
|
||||||
|
|
||||||
from products.models import Product
|
from products.models import Product
|
||||||
|
|
||||||
@@ -72,3 +73,15 @@ def find_related_products(keyword):
|
|||||||
Q(attributes__in=attributes)
|
Q(attributes__in=attributes)
|
||||||
)
|
)
|
||||||
return products_qs
|
return products_qs
|
||||||
|
|
||||||
|
|
||||||
|
def search_by_phrase(phrase):
|
||||||
|
SearchQuery(phrase, search_type='phrase')
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def alt_find_related_products(keyword, fields=('tags', 'attributes', 'category')):
|
||||||
|
vector = SearchVector(*fields)
|
||||||
|
products_qs = Product.objects.annotate(search=vector).filter(search=keyword)
|
||||||
|
return products_qs
|
||||||
|
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ from companies.models import Company
|
|||||||
from history.models import HistorySync
|
from history.models import HistorySync
|
||||||
|
|
||||||
from back_latienda.permissions import IsCreator
|
from back_latienda.permissions import IsCreator
|
||||||
from .utils import extract_search_filters, find_related_products
|
from .utils import extract_search_filters, find_related_products, alt_find_related_products
|
||||||
from utils.tag_serializers import TaggitSerializer
|
from utils.tag_serializers import TaggitSerializer
|
||||||
from utils.tag_filters import ProductTagFilter
|
from utils.tag_filters import ProductTagFilter
|
||||||
|
|
||||||
@@ -155,7 +155,8 @@ def product_search(request):
|
|||||||
chunks = query_string.split(' ')
|
chunks = query_string.split(' ')
|
||||||
|
|
||||||
for chunk in chunks:
|
for chunk in chunks:
|
||||||
products_qs = find_related_products(chunk)
|
# products_qs = find_related_products(chunk)
|
||||||
|
products_qs = alt_find_related_products(chunk)
|
||||||
for instance in products_qs:
|
for instance in products_qs:
|
||||||
result_set.add(instance)
|
result_set.add(instance)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user