I am using the Django query filter __search to perform a full text search e.g
MyModel.objects.filter(title__search = 'some title')
How do I get it to order by relevance, as currently it seems to be ordering alphabetically?
Specifically I would like search results where the title was some title to appear first before something that had the title a different but contains some title.
edit:
What I’ve noticed is that on the model definition for MyModel I have:
class Meta:
ordering = ['title']
If I remove this then the ordering becomes correct i.e. sorted by relevance. So is there a way I can leave this in the model definition as its useful elsewhere but then on my query tell it to ignore it?
Try:
Model.objects.all().order_by().search()– calling order_by without any parameters does no ordering at all.Beyond that: I’ll second Carl’s recommendation of Haystack, particularly since that allows more complicated things like stemming (“dance” would match “dances” ,”dancers”, and “dancing”), faceting (“Show me user & number of hits for each search result”), getting objects which are similar to the one you’re currently displaying, etc. When I last tried Whoosh it was unstable (i.e. crashed during indexing) but it took a rather short period of time to fire up Solr, which is great.