Search code examples
djangopostgresqlfull-text-searchdjango-orm

How can I use SearchRank in a Django model field?


I have a model Post:

class Post(models.Model):
    post_title = models.CharField(max_length=120)
    post_subtitle = models.TextField()
    post_text = models.TextField()
    vector_column = SearchVectorField(null=True)
    
    class Meta:
        indexes = (GinIndex(fields=['vector_column']),)

and I've created a trigger in the database to update the vector_column value:

create function core_post_trigger() returns trigger as $$
begin
    new.vector_column :=
        setweight(to_tsvector('pg_catalog.english', coalesce(new.post_title, '')), 'A') ||
        setweight(to_tsvector('pg_catalog.english', coalesce(new.post_subtitle, '')), 'B') ||
        setweight(to_tsvector('pg_catalog.english', coalesce(new.post_text, '')), 'C');
    return new; 
end
$$ language plpgsql;

create trigger vector_column_trigger
before insert or update on core_post
for each row execute procedure
core_post_trigger();

And I search through this model like this:

Post.objects.filter(vector_column=SearchQuery(query, config='english', search_type='websearch')

Although I've applied weights in this search, I did not apply ranking (to_tsrank). I know that I can apply rank in Django like this:

vector = SearchVector('post_title', weight='A', config='english') + SearchVector('post_subtitle', weight='B', config='english') + 
SearchVector('post_text', weight='C', config='english')

query = SearchQuery(query, search_type='websearch')

Post.objects.annotate(rank=SearchRank(vector, query)).order_by('-rank')

The question is that I don't know how to use SearchRank with my vector_column column, as this column has a GinIndex. If I use this vector variable created above, I will not use the index, and then this search will be slow. I've tried to write this query in various ways, but I don't know how to do. I tried, for example, this:

Post.objects.annotate(SearchRank(vector_column, SearchQuery('Angel', config='english'))).order_by('-rank')

But it throws an error vector_column is not defined.

How can I achieve it?


Solution

  • I found it. I needed to use a F expression:

    Post.objects.annotate(
       rank=SearchRank(F('vector_column'),
       SearchQuery(query, config='english'))
    ).order_by('-rank'), 15