Providing Search in Django based applications
I have seen many Django based applications that do not provide intuitive and powerful search capabilities to their users. If you pickup a product created in django and try to do a search, you will be disappointed by the fact that the search is so primitive. No spelling correction, no fuzzy searching, no complex multi-field searches as well. Oh .. and more over, don’t even think about relevance based search.
I have created some applications, but I have been mostly written one-off custom code to integrate best of the breed open source engines like Solr and Lucene into my web applications. While doing that, it got me thinking -
What are the options that a developer has, to provide search in an django based application?
- Use of “contains” using QuerySet API
- Use Django Sphinx
- Use - django-search-lucene app
The first approach is by far the most commonly used in Django applications. What it does is that it makes use of the underlying LIKE operator of the database. The problem is with this approach is that it’s too primitive. No Relevancy, No complex constraints, and won’t work for multi-word query where the two words don’t appear together.
Second option is to use django-sphinx project. Sphinx is an open source search engine, that was primarily written to integrate well with databases (SQL focused). Though, it seems to be gaining some momentum, but for a really poweful and featureful search engine, I have found Lucene much better. Also, the integration with django requires you to install and set it up as separate server, which is always more than you are looking for.
Lastly, there is an app called django-search-lucene that provides lucene and django integration using PyLucene (Python port of Lucene). The application provides easy integration with Django ORM and simple APIs to perform search. Moreover, for power users, they have exposed an api where you can fire native lucene queries. In addition to that, it also exposes some basic status reporting in the Django admin, which is helpful is monitoring the index / searches.
I am also noticing a flurry of activity in the last 48 hours on the project and am curious to know what new additions are being made.
Next time when you are creating a django based application, look at the kind of search that you are providing to your userbase and see if you can use one of the two (2,3) options above to enhance their experience. Also, do tell me what your experience was - always looking forward to hear that.
