I'm using a full text search in Django + PostgreSQL. So far the various configurations of SearchRank don't quite give the best ordering in my case.
However, after some experimentation, what I want I think is a rank value that is
- 1/(number of words in field), if the field matches the search query
- 0 otherwise
Which in raw SQL would be:
(to_tsvector('english', title) @@ plainto_tsquery('english', 'my search query')) ::int::float / array_length(regexp_split_to_array(title, '\s+'), 1),
How can I alias/annotate a queryset to have such a field in Django? The issue seems to be that the match operator @@ is abstracted away by Django - I can't seem to see any combination of functions at https://docs.djangoproject.com/en/4.0/ref/contrib/postgres/search/ that allows me to to this.