Full-text search using postgresql #162
@ -2,7 +2,7 @@ import logging
|
|||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.contrib.auth import get_user_model
|
from django.contrib.auth import get_user_model
|
||||||
from django.contrib.postgres.search import SearchQuery, SearchVector
|
from django.contrib.postgres.search import SearchQuery, SearchRank, SearchVector
|
||||||
from django.db.models import Q
|
from django.db.models import Q
|
||||||
from django.shortcuts import get_object_or_404, redirect
|
from django.shortcuts import get_object_or_404, redirect
|
||||||
from django.views.generic.list import ListView
|
from django.views.generic.list import ListView
|
||||||
@ -105,8 +105,15 @@ class SearchView(ListedExtensionsView):
|
|||||||
queryset = queryset.filter(search_query).distinct()
|
queryset = queryset.filter(search_query).distinct()
|
||||||
else:
|
else:
|
||||||
query = SearchQuery(self.request.GET['q'], search_type='websearch')
|
query = SearchQuery(self.request.GET['q'], search_type='websearch')
|
||||||
vector = SearchVector('slug', 'name', 'description', 'latest_version__tags__name')
|
vector = (
|
||||||
queryset = queryset.annotate(search=vector).filter(search=query).distinct()
|
SearchVector('name', weight='A')
|
||||||
|
+ SearchVector('description', weight='B')
|
||||||
|
+ SearchVector('latest_version__tags__name', weight='C')
|
||||||
|
)
|
||||||
|
rank = SearchRank(vector, query)
|
||||||
|
queryset = (
|
||||||
|
queryset.annotate(rank=rank).filter(rank__gte=0.3).distinct().order_by('-rank')
|
||||||
|
)
|
||||||
return queryset.prefetch_related(
|
return queryset.prefetch_related(
|
||||||
'authors',
|
'authors',
|
||||||
'latest_version__file',
|
'latest_version__file',
|
||||||
|
Loading…
Reference in New Issue
Block a user