Weighting queries

You can boost specific vectors so that more weight is attributed to them when ordering results by relevancy. For example, you can use this to give more relevance to posts that are matched by title rather than by content. Edit the previous lines of the views.py file of your blog application and make them look like this:

search_vector = SearchVector('title', weight='A') + SearchVector('body', weight='B')search_query = SearchQuery(query)results = Post.objects.annotate( rank=SearchRank(search_vector, search_query) ).filter(rank__gte=0.3).order_by('-rank')

In the preceding code, we apply different weights to the search vectors built using the title and body fields. The default weights are D, C, B, and A that refer to the numbers ...

Get Django 2 by Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.