Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion db_dump/data_mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ async def upload_image(image_url):
f"VALUES ('{name}', '{description}', {digital}, {price}, {stock}, '{image}', {category_id});"
)

upload_image(data['product_img_url'])
asyncio.run(upload_image(data['product_img_url']))
print(f'Product {name} added to the database')
# commit the changes to the database
connection.commit()
Expand Down
14 changes: 1 addition & 13 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,21 +77,9 @@ services:
volumes:
- redis_data:/data

elasticsearch:
image: elasticsearch:7.17.10 # version 8 is not supported by django-elasticsearch-dsl
container_name: elasticsearch
restart: always
environment:
- discovery.type=single-node
ports:
- "9200:9200"
volumes:
- elasticsearch_data:/usr/share/elasticsearch/data

volumes:
postgres_data:
static_volume:
media_volume:
db_dump:
redis_data:
elasticsearch_data:
redis_data:
9 changes: 1 addition & 8 deletions ecom_website/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
'phonenumber_field',
'rest_framework',
'rest_framework.authtoken',
'django_elasticsearch_dsl',
]

REST_FRAMEWORK = {
Expand Down Expand Up @@ -175,10 +174,4 @@
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = os.environ.get('EMAIL_HOST_USER')
EMAIL_HOST_PASSWORD = os.environ.get('EMAIL_HOST_PASSWORD')

ELASTICSEARCH_DSL = {
'default': {
'hosts': os.environ.get('ELASTICSEARCH_HOST', 'elasticsearch:9200')
},
}
EMAIL_HOST_PASSWORD = os.environ.get('EMAIL_HOST_PASSWORD')
32 changes: 0 additions & 32 deletions store/documents.py

This file was deleted.

2 changes: 1 addition & 1 deletion store/templates/store/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@
// Make an AJAX request to the search view

const xhr = new XMLHttpRequest();
xhr.open('GET', '{% url 'store:search_elastic' %}?query=' + query, true);
xhr.open('GET', '{% url 'store:search_rt' %}?query=' + query, true);

xhr.onload = function () {
if (xhr.status === 200) {
Expand Down
4 changes: 2 additions & 2 deletions store/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from .views.category import CategoryView
from .views.product import ProductView, add_review
from .views.auth import sign_up, log_in, log_out, update_cart_after_logout
from .views.search import search_product, search_elastic
from .views.search import search_product, search_rt
from .views.cart import cart, checkout, update_item, process_order
from .views.customer_views import customer_orders

Expand All @@ -16,7 +16,7 @@
path('login/', log_in, name='login'),
path('logout/', log_out, name='logout'),
path('search/', search_product, name='search'),
path('search_elastic/', search_elastic, name='search_elastic'),
path('search_rt/', search_rt, name='search_rt'),
path('cart/', cart, name='cart'),
path('checkout/', checkout, name='checkout'),
path('update_item/', update_item, name='update_item'),
Expand Down
55 changes: 35 additions & 20 deletions store/views/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
from django.db.models import Q
from django.core.paginator import Paginator
from django.core.cache import cache
from store.documents import ProductDocument
from django.http import JsonResponse
from elasticsearch_dsl import Q as DSLQ
from functools import reduce
from operator import and_

def search_product(request):
query = request.GET.get('query')
Expand All @@ -31,28 +31,43 @@ def search_product(request):
return render(request, 'store/search.html', context)


def search_elastic(request):
def search_rt(request):
query = request.GET.get('query', '').lower()

if not query:
return JsonResponse([], safe=False)

q_string = f'?rt-{query}'
keywords = query.split()


# AJAX request to postgreSQL database

products = cache.get(q_string)

if not products:
products = cache.get('all_products')
if not products:
products = Product.objects.all()
cache.set('all_products', products)

filters = [
Q(name__icontains=keyword) |
Q(description__icontains=keyword) |
Q(category__name__icontains=keyword)
for keyword in keywords
]

products = products.filter(reduce(and_, filters)).distinct()
cache.set(q_string, products[:5])

# Split the query string into individual words
words = query.split()

# Construct a list of wildcard queries for each word in the query
wildcard_queries = [
DSLQ('wildcard', name='*' + word + '*') |
DSLQ('wildcard', description='*' + word + '*') |
DSLQ('wildcard', brand='*' + word + '*')
for word in words
]

# Perform a search query on the Elasticsearch index where name, description, or brand fields contain all of the words
products = ProductDocument.search().query(
DSLQ('bool', must=wildcard_queries)
)

results = [product.to_dict() for product in products[0:5]]
# Serialize products to a JSON serializable format
results = []
for product in products[:5]:
serialized_product = {
'id': product.pk,
'name': product.name,
}
results.append(serialized_product)

return JsonResponse(results, safe=False)