Moinuddin Quadri
Moinuddin Quadri

Reputation: 48090

Django: Use MySQL and ElasticSearch in single application

I want to use MySQL and ElasticSearch using Haystack within single Django application. Our's application deals with a huge set of relational data with 300+ tables and MySql is the best choice to serve that. But we want fast searching on few columns of tables with 10 Million+ rows.

Is it possible in Django to use MySQL as primary data storage and use ElasticSearch for search related queries? I searched on internet but couldn't find any related information.

Upvotes: 3

Views: 1859

Answers (2)

Moinuddin Quadri
Moinuddin Quadri

Reputation: 48090

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.

Upvotes: 1

Anurag
Anurag

Reputation: 1531

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.

Upvotes: 2

Related Questions