如何使用python脚本检查elasticsearch中是否存在索引并对其执行异常处理?

栏目: 后端 · 发布时间: 7年前

内容简介:翻译自:https://stackoverflow.com/questions/40300231/how-to-check-if-an-index-exists-in-elasticsearch-using-a-python-script-and-perfo
如何使用 python

查询检查索引是否存在?

我将我的索引作为在查询外部分配的变量传递为: –

i=int(datetime.datetime.now().strftime('%d'))+1
indextring="index"
for m in range (i-10,i):
    d = datetime.datetime(2016, 10, m, 18, 00).strftime('%Y-%m-%d')
    index1=datestring+d
    subfix="_"+datetime.datetime(2016, 10, m, 18, 00).strftime('%Y-%m-%d')
    es=Elasticsearch(['localhost:9200'])
    res = **es.search(index='{0}'.format(index1)**, doc_type="log",size=10000, from_=0, body={ "query": {
    "match": {
     ....Match condition follows
      }
    }
  }})

现在,某些索引在特定日期不存在,但我希望该过程无论如何都要运行.当索引不存在时,我收到以下错误 – >

elasticsearch.exceptions.NotFoundError:TransportError(404,u’index_not_found_exception’)

我不确定异常处理在elasticsearch中是如何工作的.

您必须在 indices 上调用它.目前您使用的是搜索类的 exists

,它告诉您索引中是否存在给定文档而不是索引本身.

试试这个代码

if es.indices.exists(index="index"):
    Your code for search

如果你想使用还有更多 options .

翻译自:https://stackoverflow.com/questions/40300231/how-to-check-if-an-index-exists-in-elasticsearch-using-a-python-script-and-perfo


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Introduction to Computation and Programming Using Python

Introduction to Computation and Programming Using Python

John V. Guttag / The MIT Press / 2013-7 / USD 25.00

This book introduces students with little or no prior programming experience to the art of computational problem solving using Python and various Python libraries, including PyLab. It provides student......一起来看看 《Introduction to Computation and Programming Using Python》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

html转js在线工具
html转js在线工具

html转js在线工具