-
Notifications
You must be signed in to change notification settings - Fork 7
/
flask_elasticsearch.py
25 lines (20 loc) · 1012 Bytes
/
flask_elasticsearch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
from flask import current_app
from pyelasticsearch import ElasticSearch as PyElasticSearch
class ElasticSearch(object):
"""
A thin wrapper around pyelasticsearch.ElasticSearch()
"""
def __init__(self, app=None):
if app is not None:
self.init_app(app)
def init_app(self, app):
app.config.setdefault('ELASTICSEARCH_URL', 'http://localhost:9200/')
# using the app factory pattern _app_ctx_stack.top is None so what
# do we register on? app.extensions looks a little hackish (I don't
# know flask well enough to be sure), but that's how it's done in
# flask-pymongo so let's use it for now.
app.extensions['elasticsearch'] = PyElasticSearch(app.config['ELASTICSEARCH_URL'])
def __getattr__(self, item):
if not 'elasticsearch' in current_app.extensions.keys():
raise Exception('not initialised, did you forget to call init_app?')
return getattr(current_app.extensions['elasticsearch'], item)