Django – Haystack – Solr — Setup Guide

by Yuji

I’ve been needing a search engine and tried Sphinx, Haystack-Whoosh, and Haystack-Solr.

I’m quite happy with Haystack-Solr based on the results it provides out of the box. Whoosh clearly needed some more configuration to weigh multiple OR matches higher.

TERM1 OR TERM2 OR TERM3 OR TERM4 should give a match the highest score that has all 4 terms but it didn’t.

First, install solr.

# I'm on a Mac, so I used homebrew
brew install solr

# ...if you're on Ubuntu install solr-jetty
apt-get install solr-jetty

Next install solr’s python bindings and django-haystack

pip install pysolr
pip install django-haystack

Configure django-haystack, set up the search index classes according to the docs

http://docs.haystacksearch.org/dev/tutorial.html#configuration

Add the required solr fields to settings.py (solr server location)

Setup Solr

Run the solr schema file generation management command and paste it into your solr config directory.

python manage.py build_solr_schema > solr_schema.xml

mv solr_schema.xml /usr/local/Cellar/solr/3.1.0/libexec/example/solr/conf/schema.xml

Start solr

cd /usr/local/Cellar/solr/3.1.0/libexec/example/
java -jar start.jar

Generate index

python manage.py rebuild_index

Done! Start using haystack

from haystack.query import SearchQuerySet
results = SearchQuerySet().auto_query('my search here')
About these ads