开发者

Django full text search order by relevance

开发者 https://www.devze.com 2023-01-12 15:16 出处:网络
I am using the Django query filter __search to perform a full text search e.g MyMod开发者_运维知识库el.objects.filter(title__search = \'some title\')

I am using the Django query filter __search to perform a full text search e.g

MyMod开发者_运维知识库el.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?


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.


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.


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.


For search result sorted by relevance, it will take a little more the the builtin boolean search. Here are two approaches (Sphinxsearch and Whoosh):

  • http://pkarl.com/articles/guide-django-full-text-search-sphinx-and-django-sp/
  • http://www.arnebrodowski.de/blog/add-full-text-search-to-your-django-project-with-whoosh.html


You can achieve this by using a raw search with an ORDER BY clause for the relevance if using Django >= 1.2

File.objects.raw(query_string, params[x,y,z])

Though raw_querset has its own disadvantages at the moment in not supporting count() for instance.

It does however bring back models so is pretty easy to use, though not as simple as __search

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号