I am working on a query similar to the one below; I want to create this as a Django queryset for further filtering.
select distinct on (id) id, title, weight
from (select *, 3 + ts_rank_cd(to_tsvector(title), to_tsquery('sales'), 32) as weight
from courses
where to_tsvector(title) @@ to_tsquery('sales')
union all
select *, 2 + similarity(title, 'sales') as weight
from courses
where title % 'sales') as union_table
order by id, weight desc
I tried creating it as raw method, but that doesn't allow chaining additional filters and the extra method with the tables argument doesn't allow subqueries.
Is there any solution other than using the raw SQL?