1👍
✅
Your problem is that you’re adding the tags field directly to your SearchVector
Lets concatenate the tags with Django’s StringAgg
into a single string and then use that string in your SearchVector
first we import StringAgg
from django.contrib.postgres.aggregates import StringAgg
then this is how you have to change your get_queryset
function
def get_queryset(self):
queryset = super().get_queryset()
form = self.form_class(self.request.GET)
if form.is_valid():
if form.cleaned_data['active_only']:
queryset = queryset.filter(is_active=True)
if not form.cleaned_data['search']:
return queryset
queryset = queryset.annotate(tags_str=StringAgg('tags__name', delimiter=' '))
search_vector = SearchVector('identifier', 'short_text', 'serial_number', 'revision', 'part_number',
'manufacturer', 'description', 'tags_str')
search_query = SearchQuery(form.cleaned_data['search'], search_type='websearch')
return (
queryset.annotate(
search=search_vector, rank=SearchRank(search_vector, search_query)
)
.order_by("-rank").distinct()
)
return queryset
Source:stackexchange.com