python - 如何在Flask python应用程序中运行Elasticsearch的实例?

标签 python elasticsearch flask

我正在尝试使用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/

相关文章:

python - Flask-SQLAlchemy:sqlite3 IntegrityError

python - celery 不与 flask 应用程序一起运行

python - 像 Google 日历一样解析日历事件条目

python - 在多维 Pandas DataFrame 中明智地获取最小列数 - Python

sorting - Elasticsearch 5:按最近批发商的价格排序

html - Flask:无法接收 HTML 表单 POST 数据

python - with block 或 close() 更 Pythonic 吗?

python - 导入和初始化 GCP googleapis/google-cloud-python 客户端

elasticsearch - Elasticsearch 中同一字段上的语言分析器和同义词

security - Docker上的Elasticsearch 1.7.3和Shield