In this article, you will learn how to enable Elasticsearch, Solr, Whoosh, or Xapian in your CODENERIX website.

CODENERIX is ready to work with Search Indexes, for this purpose it will require you to configure Haystack.

Haystack is capable of connecting SolrElasticsearchWhoosh, and Xapian to a Django website and CODENERIX will use those Indexes to improve search speed.

For this tutorial we will configure Haystack to work together with Elasticsearch:

HAYSTACK_SIGNAL_PROCESSOR = 'haystack.signals.RealtimeSignalProcessor'
HAYSTACK_CONNECTIONS = {
    'default': {
        'ENGINE': 'codenerix.contrib.haystack_engines.AsciifoldingElasticSearchEngine',
        'URL': 'http://127.0.0.1:9200/',
        'INDEX_NAME': 'haystack',
    },
}

As you can see the search engine is we are configuring is one of the predefined ones we included in CODENERIX: codenerix.contrib.haystack_engines.AsciifoldingElasticSearchEngine

As a second step, remember to add “haystack” to your INSTALLED_APPS.

And this is all you need to do. Your CODENERIX website is ready to work with Haystack.

Indexes

Now that we have ready our CODENERIX website to use Haystack, we will define the desired Indexes.

Inside the folder of any desired app from your CODENERIX project, create a new file named “search_indexes.py” which will be used during access to lists. Add inside this file the desired Indexes you want Haystack to manage, example from an OCR Document Management Software:

from haystack import indexes
from .models import Document

class Document(indexes.SearchIndex, indexes.Indexable):
    text = indexes.EdgeNgramField(document=True, use_template=True)
    year = indexes.IntegerField(model_attr='year')
    number = indexes.IntegerField(model_attr='number')
    subject = indexes.EdgeNgramField(model_attr='subject')
    date_doc = indexes.DateField(model_attr='date_doc')
    date_input = indexes.DateTimeField(model_attr='date_input')
    sender = indexes.EdgeNgramField(model_attr='sender')
    doc_type = indexes.IntegerField(model_attr='doc_type__pk')
    doc_type__name = indexes.EdgeNgramField(model_attr='doc_type__name')
    total_files = indexes.IntegerField(model_attr='total_files')
    total_documents = indexes.IntegerField(model_attr='total_documents')
    seeker = indexes.EdgeNgramField(model_attr='seeker')

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

To let a view work with Haystack just add:

haystack = True

to your GenList, and voilá, the lists are sped up.

What about maintenance of Indexes?

Actually, Haystack is aware of Indexes and search engines and will do the job of keeping your Indexes updated so you don’t have to do anything else.