Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

126
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda