Skip to content

Commit

Permalink
Implementing a full-text search engine with Django and PostgreSQL
Browse files Browse the repository at this point in the history
  • Loading branch information
yahyaaly151989 committed Aug 31, 2023
1 parent 5b0487e commit 8a4919e
Show file tree
Hide file tree
Showing 10 changed files with 1,550 additions and 4 deletions.
Binary file modified Module03/mysite/blog/__pycache__/forms.cpython-311.pyc
Binary file not shown.
Binary file modified Module03/mysite/blog/__pycache__/urls.cpython-311.pyc
Binary file not shown.
Binary file modified Module03/mysite/blog/__pycache__/views.cpython-311.pyc
Binary file not shown.
4 changes: 4 additions & 0 deletions Module03/mysite/blog/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ class CommentForm(forms.ModelForm):
class Meta:
model = Comment
fields = ['name', 'email', 'body']


class SearchForm(forms.Form):
query = forms.CharField()
34 changes: 34 additions & 0 deletions Module03/mysite/blog/templates/blog/post/search.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{% extends "blog/base.html" %}
{% load blog_tags %}

{% block title %}Search{% endblock %}

{% block content %}
{% if query %}
<h1>Posts containing "{{ query }}"</h1>
<h3>
{% with results.count as total_results %}
Found {{ total_results }} result{{ total_results|pluralize }}
{% endwith %}
</h3>

{% for post in results %}
<h4>
<a href="{{ post.get_absolute_url }}">
{{ post.title }}
</a>
</h4>
{{ post.body|markdown|truncatewords_html:12 }}
{% empty %}
<p>There are no results for your query.</p>
{% endfor %}
<p><a href="{% url "blog:post_search" %}">Search again</a></p>

{% else %}
<h1>Search for posts</h1>
<form method="get">
{{ form.as_p }}
<input type="submit" value="Search">
</form>
{% endif %}
{% endblock %}
1 change: 1 addition & 0 deletions Module03/mysite/blog/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
path('<int:year>/<int:month>/<int:day>/<slug:post>/', views.post_detail, name='post_detail'),
path('<int:post_id>/share/', views.post_share, name='post_share'),
path('<int:post_id>/comment/', views.post_comment, name='post_comment'),
path('search/', views.post_search, name='post_search'),
]
20 changes: 19 additions & 1 deletion Module03/mysite/blog/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
from .models import Post
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.views.generic import ListView
from .forms import EmailPostForm, CommentForm
from .forms import EmailPostForm, CommentForm, SearchForm
from django.core.mail import send_mail
from django.views.decorators.http import require_POST
from taggit.models import Tag
from django.db.models import Count
from django.contrib.postgres.search import SearchVector, SearchQuery, SearchRank


def post_list(request, tag_slug=None):
post_list = Post.objects.all()
Expand Down Expand Up @@ -87,3 +89,19 @@ def post_comment(request, post_id):
# Save the comment to the database
comment.save()
return render(request, 'blog/post/comment.html', {'post': post, 'form': form, 'comment': comment})



def post_search(request):
form = SearchForm()
query = None
results = []
if 'query' in request.GET:
form = SearchForm(request.GET)
if form.is_valid():
query = form.cleaned_data['query']
search_vector = SearchVector('title', 'body')
search_query = SearchQuery(query, config='english')
results = Post.objects.annotate(search=search_vector, rank=SearchRank(search_vector, search_query)).filter(search=search_query).order_by('-rank')

return render(request,'blog/post/search.html',{'form': form,'query': query,'results': results})
Binary file modified Module03/mysite/mysite/__pycache__/settings.cpython-311.pyc
Binary file not shown.
8 changes: 5 additions & 3 deletions Module03/mysite/mysite/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
'taggit',
'django.contrib.sites',
'django.contrib.sitemaps',
'django.contrib.postgres',
]

MIDDLEWARE = [
Expand Down Expand Up @@ -81,12 +82,13 @@

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'blog',
'USER': 'blog',
'PASSWORD': '123456',
}
}


# Password validation
# https://docs.djangoproject.com/en/4.2/ref/settings/#auth-password-validators

Expand Down
Loading

0 comments on commit 8a4919e

Please sign in to comment.