[Django]-Django: Use MySQL and ElasticSearch in single application

1πŸ‘

βœ…

Yes, we can configure MySQL and ElasticSearch within a single Django project. Firstly install django-haystack and pyelasticsearch packages from pip.

sudo pip install django-haystack
sudo pip install pyelasticsearch 

In settings.py, add 'haystack' to INSTALLED_APPS and configure HAYSTACK_CONNECTIONS like:

HAYSTACK_CONNECTIONS = {
    'default': {
        'ENGINE': 'haystack.backends.elasticsearch_backend.ElasticsearchSearchEngine',
        'URL': 'http://<ip-of-redis-server>:9200/',
        'TIMEOUT': 60 * 5,
        'INCLUDE_SPELLING':True,
        'INDEX_NAME': '<name-of-index>'
        }
    }

Create a class inheriting hystack.indexes.SearchIndex and hystack.indexes.Indexable classes. In the below sample code, index in created on the fields of MyModelClass model:

from haystack import indexes


class MyIndexClass(indexes.SearchIndex, indexes.Indexable):
    field_1 = indexes.CharField(document=True, use_template=True)
    field_2 = indexes.CharField(model_attr='field_of_MyModelClass_class')
    field_3 = indexes.CharField(model_attr='field_of_MyModelClass_class')

def get_model(self):
    return MyModelClass

Refer HayStack document for further details.

2πŸ‘

Yes, it is absolutely possible. I have implemented haystack with elasticSearch backend and mysql DB myself. Have a look at the haystack docs as a starting point
http://django-haystack.readthedocs.org/en/v2.4.1/tutorial.html

Just install haystack, add it to your installed apps. Then you need to define elastic search as your backend.
After that, create search indexes and add the fields you want to search on.
I am not sure what is your confusion, go through the tutorial, its pretty straight forward.

πŸ‘€Anurag

Leave a comment