Setup Django 1.7, Haystack and ElasticSearch


Installing Django, Haystack and ElasticSearch is easy. But then i found problems which using Elasticsearch, my search results is empty. Also i have several headache problems like SearchQueryset returning 0 meanwhile indexing multiple apps is working.

Anyway, here is the correct way to setup Django, Haystack and ElasticSearch.

1. Django
We need to install elasticsearch python module with :

1
pip install elasticsearch

2. Elasticsearch in Windows
There are several helpful tutorials. For me:
1. I just download the zip files
2. setup JAVA_HOME (Download java first) and add java in PATH (Global environment system)
3. execute elasticsearch service in /bin

3. Haystack
Make sure follow this :

settings.py

1
2
3
4
5
6
7
8
9
10
11
12
13
HAYSTACK_CONNECTIONS = {
    ‘default’: {
        ‘ENGINE’: ‘haystack.backends.elasticsearch_backend.ElasticsearchSearchEngine’,
        ‘URL’: ‘http://127.0.0.1:9200/’,
        ‘INDEX_NAME’: ‘your-project-name’,
    },
}

HAYSTACK_LIMIT_TO_REGISTERED_MODELS = False

# increase the default number of results (from 20)
HAYSTACK_SEARCH_RESULTS_PER_PAGE = 40
HAYSTACK_SIGNAL_PROCESSOR = ‘haystack.signals.RealtimeSignalProcessor’

search_indexes.py

1
2
3
4
5
6
7
8
9
10
11
12
13
import datetime
from haystack import indexes
from .models import AppModel

class AppModelIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.SearchField(document=True, use_template=True)

    def get_model(self):
        return AppModel

    def index_queryset(self, using=None):
        """Used when the entire index for model is updated."""
        return self.get_model().objects.filter(created__lte=datetime.datetime.now())

Use this for multple apps (just need to rename appname)

And it’s should be works!


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.