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

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

内容简介:翻译自: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


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

查看所有标签

猜你喜欢:

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

Web Operations

Web Operations

John Allspaw、Jesse Robbins / O'Reilly Media / 2010-6-28 / USD 39.99

A web application involves many specialists, but it takes people in web ops to ensure that everything works together throughout an application's lifetime. It's the expertise you need when your start-u......一起来看看 《Web Operations》 这本书的介绍吧!

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

SHA 加密
SHA 加密

SHA 加密工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具