Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

119
Views
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?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!