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.
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.