How to create ElasticSearch index if it doesn't already exist in Python
The following utility will create an index if it doesn’t exist already by ignoring any resource_already_exists_exception
def es_create_index_if_not_exists(es, index):
"""Create the given ElasticSearch index and ignore error if it already exists"""
try:
es.indices.create(index)
except elasticsearch.exceptions.RequestError as ex:
if ex.error == 'resource_already_exists_exception':
pass # Index already exists. Ignore.
else: # Other exception - raise it
raise ex
# Example usage: Create "nodes" index
es_create_index_if_not_exists(es, "nodes")