« Easily Running the Django Test Suite | Class Based Template Tags »
It's been a little over a year since I started doing Django development full-time, for one of them real jobs. Around that time, there were a few large problems in the community that hadn't been solved yet. They were kind of blemishes when you would talk to people about Django, and I'm happy that most of them have been solved.
This will be a series of posts that talk about the different big problems that have been solved, and how they have been addressed in the community.
Search was probably the biggest annoyance for Django. It is something that every site needs, and something that just wasn't really happening in the Django community. 2008's Summer Of Code ended with djangosearch as a half-finished shell, that needed to be better architected; but it did show a good push in the realm of search.
Out of those ashes, comes an awesome solution to the search problem in Django. Haystack is something I am more familiar with (we use it in production at work), and is the brain child of the ever modest, house rocking Daniel Lindsley. It provides a number of Django patterns applied to search, which makes it easier to internalize.
Note: Another approach to search is available, which patches django's ORM. This uses the existing full-text search in your database.
The registration pattern of the admin allows you to unobtrusively make models searchable (including code you don't have access to). This allows you to register Django Comments as searchable for example, without forking the code base. This will look similar to the admin:
from haystack import site
site.register(Note, NoteIndex)
Haystack provides an interface familiar to the Django ORM Queryset API. This gives you most of the commonly used functions from the ORM, but allowing you to use them on searches!
unfriendly_results = SearchQuerySet().exclude(content='hello').filter(content='world')
unfriendly_results.order_by('-pub_date')[:5]
It also gives you Search specific methods such as boost and facet.
In 1.2, hopefully generic views will be class based. Haystack has an implementation of these as well. Like any other kind of class, it provides the easy ability to override functionality through subclassing.
This (simplified) example from the source shows how easy it is to provide extra context to a search view.
class FacetedSearchView(SearchView):
def extra_context(self):
extra = super(FacetedSearchView, self).extra_context()
extra['facets'] = self.results.facet_counts()
return extra
Haystack currently supports three different search backends: Whoosh, Xapian, and Solr. With a publicly documented backend API, you can enjoy all of the power of the search engine of your choice, by providing a backend for it!
HAYSTACK_SEARCH_ENGINE = 'solr'
A shining light in the Django world is the documentation. It is often talked about as being the biggest factor for how people learn Django and love it is the documentation. Haystack is another package with fantastic documentation. Here are a couple of little gems that really show the quality and thought that has been put into them:
The docs cover ways to improve your search and make it awesome, as well as just helping you get the software set up and running. The information is invaluable, and will help you make the search on your site really great!
If I'm preaching to the choir and you already use Haystack, there is a growing list of users that are using haystack. Daniel would love for you to contact him, and get yourself added to the list.
Let me know what you love (or hate) about Haystack. I think it reuses a lot of the good patterns in Django, allowing people to take knowledge they already have, and apply it to a new problem domain easily. Is there anything that you don't like, or something that you love that I missed? Let me know in the comments!
Comments have been close for this post.
Posted at 8:34 p.m. on November 2, 2009
Comments: 8
Tags: django , largeproblems , patterns , postaday09 , solutions
The role of designers in the Django community
2 days, 22 hours Ago (Comments: 6)
Large Problems in Django, Mostly Solved: Documentation
4 days, 7 hours Ago (Comments: 4)
1 month Ago (Comments: 0)
Correct way to handle default model fields.
2 months, 2 weeks Ago (Comments: 8)
2 months, 3 weeks Ago (Comments: 2)
I may not have gone where I intended to go, but I think I have ended up where I intended to be.
- Douglas Adams


Comments
1 Daniel Lindsley says...
First, thank you for writing this up. I swear I don't recall paying you off... :)
I'd like to point out that
djangosearchwas actually relatively complete for it's goals (pluggable backends and a single search interface) and was primarily not a GSoC project. Haystack owes a decent sized debt to it, both in code and lessons learned.Posted at 5:49 a.m. on November 3, 2009
2 Harro says...
We started working with Django a while ago and when we had to do our first search we actually stumbled upon haystack. I must say for 90% of the sites that need search haystack makes it very easy to implement. For the other 10% it's really a matter of talking the customer "down" to the 90% or make sure they pay for implementing what they want.
Posted at 7:47 a.m. on November 3, 2009
3 Emil Stenström says...
Hi!
A great series of articles has begun, I'm a new subscriber :)
Perhaps if you could combine that articles with some real life experience of them? For instance: I would love to hear more about your combination of backends that you use together with Haystack.
Posted at 1:16 p.m. on November 3, 2009
4 Colin Powell says...
Haystack is great, but I kept getting collisions when adding new models. Personally I prefer the index-generation-as-cronjob technique. And for that there is Djapian: http://code.google.com/p/djapian/
Posted at 9:29 p.m. on November 3, 2009
5 James says...
What about Sphinx search?
Posted at 6:31 p.m. on November 4, 2009
6 Erik Allik says...
Check out xappy which is a high level Python layer for Xapian. There's also django-xappy, but in my last project, I sticked to just xappy because it was more straightforward and easier to grasp and understand.
Posted at 5:02 p.m. on November 16, 2009
7 Harro says...
Haystack and xapian works great. Very easy to setup on ubuntu !
Posted at 2:27 p.m. on November 19, 2009
8 V_uada4in says...
Dear Author ericholscher.com ! Yes, really. And I have faced it.
Posted at 4:10 a.m. on December 4, 2009