【问题标题】:Automate Elasticsearch index creation on django app startup在 django 应用启动时自动创建 Elasticsearch 索引
【发布时间】:2019-11-04 08:47:10
【问题描述】:

我想在启动时自动检查并创建(如果不存在)我的应用程序的弹性搜索索引,这是我目前无法正常工作的情况:

echo "Checking if Elasticsearch index is setup"
{
cat <<EOF | python /manage.py shell
from elasticsearch import Elasticsearch
HOST_URLS = ["elasticsearch:9200"]
es_conn = Elasticsearch(HOST_URLS)
INDEX_NAME = "posts"
res = es_conn.indices.exists(index=INDEX_NAME)

if res == True:
  print("Elasicsearch index seems already setup, skipping")
else:
  print("Elasicsearch index not setup yet, creating ...")
  import subprocess
  subprocess.Popen("y | python manage.py search_index --rebuild", shell=True, stdout=subprocess.PIPE).communicate()[
                0].decode('utf-8').strip()
EOF
}>/dev/null

这再次导致:

"'{}' 索引?[n/Y]: ".format(", ".join(index_names)))
| EOFError:读取一行时出现EOF

python manage.py search_index --rebuild 来自:https://github.com/sabricot/django-elasticsearch-dsl/

【问题讨论】:

    标签: python django elasticsearch automation startup


    【解决方案1】:

    我在运行该命令时遇到了同样的问题,当我尝试手动运行时我意识到它正在提示询问

    Are you sure you want to delete the 'clubs' indexes? [n/Y]:
    

    这意味着当它自动运行时,它没有得到一个导致一切崩溃的答案。您需要将 -f 标志添加到命令的末尾,这基本上只是绕过它询问此提示。例如: python manage.py search_index --rebuild -f

    【讨论】:

      猜你喜欢
      • 2021-09-08
      • 1970-01-01
      • 1970-01-01
      • 2021-06-03
      • 2016-02-03
      • 2017-12-17
      • 1970-01-01
      • 2020-06-12
      • 1970-01-01
      相关资源
      最近更新 更多