Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django full text search order by relevance

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?

like image 736
John Avatar asked Aug 20 '10 09:08

John


2 Answers

As noticed here, the search is Boolean.

There's no such relevance coefficient to use for ordering.

A stupid idea can be ordering by title length, which can make sense.

like image 78
Enrico Carlesso Avatar answered Nov 05 '22 12:11

Enrico Carlesso


The easiest way to get good fulltext search in a Django project is to use the excellent Haystack app. It's ridiculously easy to set up, especially if you use the simplest search engine backend (Whoosh, which is pure Python). You can set up fulltext indexing of your content, with relevance-ordered results and lots of other nice features besides, in a matter of minutes. And if you outgrow Whoosh's performance/concurrency/feature limitations, since you're using Haystack to abstract the search features you can swap in something like Solr for Whoosh anytime.

like image 45
Carl Meyer Avatar answered Nov 05 '22 11:11

Carl Meyer