Jasper
Jasper

Reputation: 2231

Django - Search matches with all objects - even if they don't actually match

This is the model that has to be searched:

class BlockQuote(models.Model):
    debate = models.ForeignKey(Debate, related_name='quotes')
    speaker = models.ForeignKey(Speaker, related_name='quotes')
    text = models.TextField()

I have around a thousand instances on the database on my laptop (with around 50000 on the production server)

I am creating a 'manage.py' function that will search through the database and returns all 'BlockQuote' objects whose textfield contains the keyword.

I am doing this with the Django's (1.11) Postgres search options in order to use the 'rank' attribute, which sounds like something that would come in handy. I used the official Django fulltext-search documentation for the code below

Yet when I run this code, it matches with all objects, regardless if BlockQuote.text actually contains the queryfield.

def handle(self, *args, **options):
    vector = SearchVector('text')
    query = options['query'][0]
    Search_Instance = Search_Instance.objects.create(query=query)
    set = BlockQuote.objects.annotate(rank=SearchRank(vector, query)).order_by('-rank')

    for result in set:
        match = QueryMatch.objects.create(quote=result, query=Search_Instance)
        match.save()

Does anyone have an idea of what I am doing wrong?

Upvotes: 1

Views: 287

Answers (1)

kevswanberg
kevswanberg

Reputation: 2109

I don't see you actually filtering ever.

BlockQuote.objects.annotate(...).filter(rank__gte=0.5)

Upvotes: 2

Related Questions