You are on page 1of 12

Search for Django

Why so long?

Ben Firshman http://benfirshman.com/


Why’s there no search?
• Search is integral to most sites, yet nothing
useful has made it into Django.
• It has long been considered the realm of
pluggable apps.
• Only recently have decent full text features
made their way into all the databases
Django supports.
djangosearch
• Originally a Solr interface by Joseph
Kocherhans and Jacob Kaplan-Moss.
• All objects stored in one index alongside
content type.
• Index maintained by signals on models.
• For the Google Summer of Code, I added a
QuerySet style interface and other
backends.
It’s the wrong approach
• For most sites, setting up a separate search
engine is impractical.
• Search results are inflexible (filter() is
hard to get right).
• Data is stored in two different locations.
• Model instances still have to be loaded
from database.
SQL full text support

• PostgreSQL, MySQL, SQLite and Oracle all


have full text search built in.
• The __search lookup for filter()
already exists, why not build on that?
__search lookup

• Only one field is searched.


• Indexes aren’t created automatically.
• Only MySQL support.
The Solution
class Article(models.Model):
headline = models.CharField(max_length=100)
pub_date = models.DateTimeField()
body = models.TextField()

class Meta:
ordering = ('pub_date','headline')

def __unicode__(self):
return self.headline
The Solution
class Article(models.Model):
headline = models.CharField(max_length=100,
search_index=True, search_weight=1)
pub_date = models.DateTimeField()
body = models.TextField(search_index=True,
search_weight=0.3)

class Meta:
ordering = ('pub_date','headline')

def __unicode__(self):
return self.headline
The Solution
>>> Article.objects.search('alpha')
[<Article: Django 1.1 alpha 1 released>, <Article:
Django 1.1 beta released>]

>>> Article.objects.search('beta')
[<Article: Django 1.1 alpha 1 released>, <Article:
Django 1.1 beta released>]

>>> _.order_by('-search__relevance')
[<Article: Django 1.1 beta released>, <Article:
Django 1.1 alpha 1 released>]

>>> Article.objects.filter(body__search='prague')
[<Article: EuroDjangoCon 2009>]
The future...

• SQLite and Oracle support.


• Relevance for __search lookups.
• Generic search views.
• Admin search interface.
• Better query strings.
What about other
engines?
• Solr, Xapian, Sphinx, Whoosh...
• All existing projects are fairly similar,
collaboration!
• Decide on an API.
• Include in Django or separate pluggable?
Try it out!

http://github.com/bfirsh/django/tree/search

You might also like