Skip to content
This repository was archived by the owner on Aug 13, 2019. It is now read-only.

Commit

Permalink
Merge branch 'rank-based-search' into 'master'
Browse files Browse the repository at this point in the history
Add rank in search and show nearest result at the top

See merge request TeamSPD/Chirp!73
  • Loading branch information
skshetry committed Feb 20, 2018
2 parents b121adf + bb53ac1 commit 0ea4811
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
15 changes: 13 additions & 2 deletions apps/search/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
from django.db.models import Q
from django.contrib.auth.models import User
from django.contrib.auth.decorators import login_required
from django.contrib.postgres.search import SearchQuery, SearchRank, SearchVector

from posts.models import Post
from .decorators import ajax_required
from posts.forms import PostMediaFormSet, PostForm
# Create your views here.

from .decorators import ajax_required


@login_required
def search(request):
if 'q' in request.GET:
Expand All @@ -34,6 +37,14 @@ def search(request):
Q(username__icontains=query) | Q(
first_name__icontains=query) | Q(
last_name__icontains=query))

# full text search
query = SearchQuery(querystring)
vector = SearchVector('text')

result_fulltext = Post.objects.annotate(rank=SearchRank(vector, query)).filter(rank__gte=0.0001).order_by('-rank')
results['posts'] = result_fulltext | results['posts']

count['posts'] = results['posts'].count()
count['users'] = results['users'].count()

Expand Down
1 change: 1 addition & 0 deletions config/settings/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
'whitenoise.runserver_nostatic',
'django.contrib.staticfiles',
'django.contrib.humanize',
'django.contrib.postgres',

]

Expand Down

0 comments on commit 0ea4811

Please sign in to comment.