Skip to content

Django-haystack #
Find similar titles

Structured data

Category
Programming
Database

Django-haystack #

django-haystack is django app which adds search functionality to any Django project. It supports different backends, such as Solr, ElasticSearch, Whoosh, Xapian and more.

Installation #

Installation is easy as following:

$ pip install django-haystack

Add haystack To INSTALLED_APPS As with most Django applications, you should add Haystack to the INSTALLED_APPS within your settings file (usually settings.py).

Example:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',

    # Added.
    'haystack',

    # Then your usual apps...
    'blog',
]

Configuration #

Modify Your settings.py Within your settings.py, you’ll need to add a setting to indicate where your site configuration file will live and which backend to use, as well as other settings for that backend.

HAYSTACK_CONNECTIONS is a required setting and should be at least one of the following:

Solr #

Example:

HAYSTACK_CONNECTIONS = {
    'default': {
        'ENGINE': 'haystack.backends.solr_backend.SolrEngine',
        'URL': 'http://127.0.0.1:8983/solr'
        # ...or for multicore...
        # 'URL': 'http://127.0.0.1:8983/solr/mysite',
    },
}

Elasticsearch #

Example:

HAYSTACK_CONNECTIONS = {
    'default': {
        'ENGINE': 'haystack.backends.elasticsearch_backend.ElasticsearchSearchEngine',
        'URL': 'http://127.0.0.1:9200/',
        'INDEX_NAME': 'haystack',
    },
}

Whoosh #

Requires setting PATH to the place on your filesystem where the Whoosh index should be located. Standard warnings about permissions and keeping it out of a place your webserver may serve documents out of apply.

Example:

import os
HAYSTACK_CONNECTIONS = {
    'default': {
        'ENGINE': 'haystack.backends.whoosh_backend.WhooshEngine',
        'PATH': os.path.join(os.path.dirname(__file__), 'whoosh_index'),
    },
}

Xapian #

First, install the Xapian backend (via http://github.com/notanumber/xapian-haystack/tree/master) per the instructions included with the backend.

Requires setting PATH to the place on your filesystem where the Xapian index should be located. Standard warnings about permissions and keeping it out of a place your webserver may serve documents out of apply.

Example:

import os
HAYSTACK_CONNECTIONS = {
    'default': {
        'ENGINE': 'xapian_backend.XapianEngine',
        'PATH': os.path.join(os.path.dirname(__file__), 'xapian_index'),
    },
}

Usage #

For example purposes, we’ll be adding search functionality to a simple todo application. Here is myapp/models.py:

from django.db import models
from django.contrib.auth.models import User


class Task(models.Model):
    user = models.ForeignKey(User)
    created_date = models.DateTimeField()
    name = models.CharField(max_length=200)

    def __unicode__(self):
        return self.name

We’ll create the following TaskIndex to correspond to our Task model. This code generally goes in a search_indexes.py file within the app it applies to, though that is not required. This allows Haystack to automatically pick it up. The TaskIndex should look like:

import datetime
from haystack import indexes
from myapp.models import Task


class TaskIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True)
    author = indexes.CharField(model_attr='user')
    pub_date = indexes.DateTimeField(model_attr='created_date')

    def get_model(self):
        return Task

    def index_queryset(self, using=None):
        return self.get_model().objects.filter( \
        pub_date__lte=datetime.datetime.now())

Running #

Simply run ./manage.py rebuild_index. You’ll get some totals of how many models were processed and placed in the index.

References #

  1. http://haystacksearch.org/
  2. http://django-haystack.readthedocs.io/en/latest/toc.html
0.0.1_20210630_7_v33