Dec 12, 2008

Django fulltext search -- part 1

I have been recently working on adding full text search to a django based web application since this is not yet particularly well documented. Here it is my little stone to the amazing pyramid already in place waiting for you to use it. In order to add this "must have" feature to your great web application you will need 3 components :

Starting by the latest the first challenge is to get it installed, I haven't had luck with the usual "apt-get install solr". It must be me but after an installation I was only getting a blank page instead of solr admin interface. On #solr channel someone advise me to just grab the latest release tar ball and to run with it. Then I have performed the tutorial this help me a lot to get started with solr. Believe it or not when you are there you have done the hardest part of it.

Now you need to go inside the django project you want to work on and follow the following recipe :
  1. download and put into your PYHTONPATH djangosearch and pysolr
  2. Add djangosearch inside the INSTALLED_APPS tupple
  3. add the following settings :
  • SEARCH_ENGINE = "solr" # solr, lucene, xapian, estraier
  • SOLR_URL= "http://localhost:8983/solr"

At this point you are almost done the last operations you will need to do is to define inside the fields you want to index with solr. In order to do this djangosearch come with a class called : ModelIndex
For example if you want to index : tease, and story fields of your Story model you will do something like this :

from djangosearch import ModelIndex
class Story(models.Model):
tease = models.TextField(_('tease'), blank=True)
story = models.TextField(_('story'), blank=True)
index = ModelIndex(fields=["tease","story"])

Now you need to tell solr about this 2 new fields this is in fact easier that it seems to be, the theory is explained there. In practice what you need to do open this file (apache-solr-1.3.0/example/solr/conf/schema.xml) with your editor of choice and add this 2 lines inside the fields section:

You will now need to restart solr in order to take this modification into account.

The meal is now ready you can go and taste it.
  • Open a browser go to the admin interface and add a new story or update an existing one.
  • Open a django shell and enjoy your new full text search

from dajngosearch import search
search_results = search("foo")

I hope this will be useful to someone and will avoid you the pain I had to get all these pieces dancing together.
I would be glad to read from you what can be improve in this raw recipe and also what you are doing with your search and search_results.
blog comments powered by Disqus