Vous êtes sur la page 1sur 15

Search for the ORM

Ben Firshman http://benfirshman.com/


djangosearch
• Originally a Solr interface by Joseph
Kocherhans and Jacob Kaplan-Moss.
• For the Google Summer of Code, I added a
QuerySet style interface and other
backends.
• Adding SQL database backends to
djangosearch was the wrong approach.
SQL full text support

• PostgreSQL, MySQL, SQLite and Oracle all


have full text search built in.
• The __search field 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.
What do we need?
What do we need?
• Searching over multiple fields, alongside the
__search lookup.
What do we need?
• Searching over multiple fields, alongside the
__search lookup.

• Automatic index creation.


What do we need?
• Searching over multiple fields, alongside the
__search lookup.

• Automatic index creation.


• Relevance.
What do we need?
• Searching over multiple fields, alongside the
__search lookup.

• Automatic index creation.


• Relevance.
• Generic search views.
What do we need?
• Searching over multiple fields, alongside the
__search lookup.

• Automatic index creation.


• Relevance.
• Generic search views.
• Support for all databases.
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>]
What about other
engines?
• Solr, Xapian, Sphinx, Whoosh...
• All existing projects are fairly similar, we
should collaborate.
• Decide on a user API and write backends.
• django-haystack looks particularly
promising.
Try it out!

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

http://benfirshman.com/ @bfirsh

Vous aimerez peut-être aussi