views:

307

answers:

3

I need to implement full text search for my Django application, running MySQL as backend.

Let's say I've got a model as follows:

class MyItem(models.Model):
    title = models.CharField()
    short_description = models.TextField()
    description = models.TextField()

I would like to have results first for search term occurences in title, then in short_description and at the end in description field. I'll be happier if I don't have to use additional modules/applications for this task. Any idea?

Thanks in advance.

+1  A: 

Here is a nice tutorial without extra applications: http://www.mercurytide.co.uk/news/article/django-full-text-search/

Felix Kling
I tried this approach already. But, the major problem is that the RELEVANCE represents the number of matches inside searched text, but not the "priority", as I need.
edkirin
A: 

If you are looking for a beefy solution I recommend http://haystacksearch.org/

It is very well thought out.

michael
I suppose I will end up with 3rd party solution which I tried to avoid.
edkirin
Yeah you might need to clarify why that is the solution you want? this option will keep you database independent and it scales well as you add db models to the search criteria. For instance on a project I started using haystack for one table but the final product was querying ten. Don't forget though If you know exactly what you want in raw sql terms you can use it.http://docs.djangoproject.com/en/dev/topics/db/sql/ . Good luck
michael
A: 

You can use full text search in django

MyItem.objects.filter(title__search="some search text")

One thing is - you can't define a fulltext index from a Django model, you need to do in directly in a database (using PHPMyAdmin or SQL query)

See Django documentation: http://docs.djangoproject.com/en/dev/ref/models/querysets/#search

Silver Light