PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, October 24, 2022

[FIXED] How to check if an index exists in elasticsearch using a python script and perform exception handling over it?

 October 24, 2022     elasticsearch, elasticsearch-2.0, elasticsearch-net, elasticsearch-plugin, python     No comments   

Issue

How do I check whether an index exists or not using a python query?

I'm passing my index as a variable assigned outside the query as :-

 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 of the index are not present for a particular date, however I want the process to run irrespective of that. I'm getting the following error when the index is not present-->

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

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


Solution

You have to call it on indices. Currently you are using exists of search class which tells you whether given document exists in the index and not the index itself.

Try this code

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

There are also more options if you want to use.



Answered By - ChintanShah25
Answer Checked By - Cary Denson (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing