我正在尝试使用Flask python框架使用Elasticsearch实例创建服务器。我有下面的简单代码:
from flask import Flask, render_template
from flask_bootstrap import Bootstrap
from datetime import datetime
from pyelasticsearch import ElasticSearch
""" Insert some code here that creates/runs the Elasticsearch instance """
es = ElasticSearch('http://localhost:9200/')
doc = {
'author': 'kimchy',
'text': 'Elasticsearch: cool. bonsai cool.',
'timestamp': datetime(2010, 10, 10, 10, 10, 10)
}
es.index(index="test-index", doc_type='tweet', doc=doc,id=1,overwrite_existing=True)
def create_app():
app = Flask(__name__)
Bootstrap(app)
return app
mainApp = create_app()
@mainApp.route('/')
def index():
return render_template('index.html')
if __name__ == '__main__':
mainApp.run(debug=True)
但是,如果我在应用程序之外启动它(即在localhost 9200上运行),则只能使mainApp连接到Elasticsearch实例。但是,我想发生的事情是,当我运行app.py时,我的Flask服务器和与其连接的Elasticsearch实例都被创建了。我将如何在python中做到这一点?在此先感谢您的帮助。
最佳答案
您应该先运行elasticsearch的服务sudo service elasticsearch start
然后为您的应用服务
关于python - 如何在Flask python应用程序中运行Elasticsearch的实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26822438/