I’m building a Django project that needs search functionality, and until there’s a django.contrib.search, I have to choose a search app. So, which is the best? By “best” I mean…
easy to install / set up
has a Django- or at least Python-friendly API
can perform reasonably complex searches
Here are some apps I’ve heard of, please suggest others if you know of any:
I’d also like to avoid using a third-party search engine (like Google SiteSearch), because some of the data I’d like to index is for site members only and should not be public.
Check out Haystack Search – a new model based search abstraction layer that currently supports Xapian, Solr and Whoosh. Looks like it’s well supported and documented.
I’d recommend Sphinx for full-text search and aggregation, and django-sphinx is good enough for production use. We found that Sphinx was the least resource-intensive and fastest way to index and search our documents and that django-sphinx was a nice wrapper on top of the sphinx client.
The group by aggregation is particularly nice, if for example you want to display how many documents with a certain tag or by a certain author (or both) matched a search. In memory attribute updates were convenient too, especially for removing deleted articles immediately.
# set up the modelclassEvent(models.Model):
title = models.CharField(max_length=255)
date = models.DateField()
is_outdoors = models.BooleanField()
index = djangosearch.ModelIndex(text=['title'],
additional=['date','is_outdoors'])# run a search
results =Event.index.search("django conference")
Thanks Garth. I had seen that djangosearch wanted to become the official Django search, but I was hesitant to use it because I couldn’t find any documentation! Luckily, there’s a README in subversion that I hadn’t seen before, and it makes the API look very cool:
# set up the model
class Event(models.Model):
title = models.CharField(max_length=255)
date = models.DateField()
is_outdoors = models.BooleanField()
index = djangosearch.ModelIndex(text=['title'],
additional=['date', 'is_outdoors'])
# run a search
results = Event.index.search("django conference")
Obviously it lacks the speed, scalability and features of the real projects like Haystack, but this one is easier to set up, and I don’t really need anything else than keyword AND-search.
You might want to consider letting Yahoo do all the hard work with their Build your own Search Service (BOSS). Here is a great blog post that walks you through the process:
http://www.peterkrantz.com/2008/yahoo-search-in-django/
After quick evaluation of all existing search addons for Django, I found this one as most flexible and easiest to use. It’s rough on the edges in few places, but it’s still the best way to use power of Xapian search engine inside Django projects.
If you have large amount of data to be indexed or you expect high traffic, I’d suggest using some external search engine, like Solr. This way, you’ll keep shared-nothing approach and be able to scale your site components independently.
We decided to go with Tsearch2 and a custom postgres adaptor. Tsearch2 does not need an extra process to run, which was convenient since we are on a WebFaction hosting with limited memory… It’s not completely done yet, but seems to be a good solution…