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

170
Views
Matching one or more keywords with Django's Postgres full-text searching

If I search on a website with multiple keywords (and no quotes) -- such as for red car -- my expectation is that items containing "red car" exactly should be first, followed by items containing both keywords (but non-sequentially), followed by items containing one of the keywords. (I believe this is the default behavior in Lucene-like systems, but it's been a while since I've used them, so can't say for sure.)

My hope was that the Postgres full-text searching would automatically do this, but my early tests show this is not the case:

## ASSUME: items in database: <blue car>, <green car>, <red truck>

keywords = "red car"
items = ForSaleItem.objects.filter(name__search=keywords)

## RESULT: items is empty/None, whereas it should have each of 
##         the items since one keyword matches.

The hack I'm seeing is to using Django's disjunction operator, but I'm hoping there is something less-hacky. I'm also pretty sure this hack wouldn't put exact matches at the top of the list. Here's the hack:

from django.db.models import Q
keyword_query = Q()
for keyword in keywords.split(' '):
    keyword_query.add(Q(name__search=keyword), Q.OR)
items = ForSaleItem.objects.filter(keyword_query)

Is there some settings/API that I'm missing (or something implementable on the postgres side) that gets the functionality I'm expecting?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Thanks to @Dharshan for pointing me in the right direction. As he or she noted, the disjunction of SearchQuery objects will allow matching of either keyword. Additionally, to have items that contain both keywords at the top of the list -- as described in the Django full text search docs -- the SearchRank class can be used as follows:

vector = SearchVector('name')
query = SearchQuery('red') | SearchQuery('car')
items = ForSaleItem.objects.annotate(rank=SearchRank(vector, query)).order_by('-rank')
over 4 years ago · Santiago Trujillo Report

0

items = ForSaleItem.objects.filter(name__contains=keywords)
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!