I'm currently using django-haystack and elasticsearch in a project and all works as expected when elasticsearch is running.
Haystack Settings:
HAYSTACK_CONNECTIONS = {
'default': {
'ENGINE': 'haystack.backends.elasticsearch_backend.ElasticsearchSearchEngine',
'URL': 'http://127.0.0.1:9200/',
'INDEX_NAME': 'haystack',
},
}
HAYSTACK_SIGNAL_PROCESSOR = 'haystack.signals.RealtimeSignalProcessor'
I'm using RealtimeSignalProcessor for a realtime index update.
The problem comes when elasticsearch is down, because trying to add/update any object gives us the following error:
ConnectionError(('Connection aborted.', error(111, 'Connection refused'))) caused by: ProtocolError(('Connection aborted.', error(111, 'Connection refused')))
Is there a way of catch/manage that error?
It would be useful in production environment in order to allow users to add/update objects without crashing, when elasticsearch is down.
Thanks in advance.
I suggest you subclass
ElasticSearchBackend
and wrap theupdate
,remove
andclear
methods around a decorator that captures the exceptions. That way you keep elasticsearch features, but you are able to override the behaviour of them.I use to wrap them with a decorator, a mute-error one:
Then add it to your project, configure the
HAYSTACK_BACKEND
:Have a look at django-haystack documentation. You should also create a subclass of BaseEngine, this is not properly documented.
Here it is the code:
We are just overriding the engine, and not the SearchQuery subclass, since the one provided by elasticsearch class by default is enough for us, by now.