How to check if an index exists in elasticsearch using a python script and do exception handling?

How to check if index exists or not using python query?

I pass my index as a variable assigned outside of the request, like: -

 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
      }
    }
  }})

Now, some indexes are missing for a specific date, however I want the process to run regardless of this. I get the following error when the index is missing →

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

I'm not sure how exception handling works in elasticsearch.

+4
source share
1

indices. exists , , , .

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

, , .

+10

Source: https://habr.com/ru/post/1659149/


All Articles