-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathflask_elasticsearch.py
94 lines (77 loc) · 3.02 KB
/
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
"""
Flask-Elasticsearch
-------------------
An Elasticsearch connector for Flask.
Flask-Elasticsearch should be used in the following way::
from flask import Flask
from flask.ext.elasticsearch import Elasticsearch
app = Flask(__name__)
app.config.from_pyfile('config.cfg')
es = Elasticsearch(app)
or::
es = Elasticsearch()
app = create_app('config.cfg')
es.init_app(app)
and later::
@app.route('/docs')
def docs():
conn = es.connection
conn.search(...)
"""
from elasticsearch import Elasticsearch as Es
from flask import current_app
# The stack on which to store the Elasticsearch connection.
# Flask >= 0.9 -> _app_ctx_stack
# Flask < 0.9 -> _request_ctx_stack
try:
from flask import _app_ctx_stack as stack
except ImportError:
from flask import __request_ctx_stack as stack
class Elasticsearch(object):
"""An Elasticsearch connector
Your application's configuration will need the following parameters:
* ``ELASTICSEARCH_CONNECTION``: a comma-separated list of URLs,
defaults to `http://127.0.0.1:9200`.
* ``ELASTICSEARCH_USERNAME``: the username to connect with, if any;
defaults to `''`.
* ``ELASTICSEARCH_PASSWORD``: the password to use, if any;
defaults to `''`.
* ``ELASTICSEARCH_USE_SSL``: whether to use SSL for the connection,
defaults to `False`.
"""
def __init__(self, app=None):
self.app = app
if app is not None:
self.init_app(app)
def init_app(self, app):
app.config.setdefault('ELASTICSEARCH_CONNECTION',
'http://127.0.0.1:9200')
app.config.setdefault('ELASTICSEARCH_USERNAME', '')
app.config.setdefault('ELASTICSEARCH_PASSWORD', '')
app.config.setdefault('ELASTICSEARCH_USE_SSL', False)
# Use the new style teardown_appcontext if it's available,
# otherwise fall back to the request context.
if hasattr(app, 'teardown_appcontext'):
app.teardown_appcontext(self.teardown)
else:
app.teardown_request(self.teardown)
def connect(self):
return Es(
current_app.config['ELASTICSEARCH_CONNECTION'].split(','),
http_auth=(current_app.config['ELASTICSEARCH_USERNAME'],
current_app.config['ELASTICSEARCH_PASSWORD']),
use_ssl=current_app.config['ELASTICSEARCH_USE_SSL'],
sniff_on_start=True,
sniff_on_connection_fail=True
)
def teardown(self, exception):
ctx = stack.top
if hasattr(ctx, 'elasticsearch_connection'):
ctx.elasticsearch_connection = None
@property
def connection(self):
ctx = stack.top
if ctx is not None:
if not hasattr(ctx, 'elasticsearch_connection'):
ctx.elasticsearch_connection = self.connect()
return ctx.elasticsearch_connection