Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def does_index_exist(
app_namespace, index_name, es_host=None, es_client=None, connect_timeout=2
):
"""Return boolean flag to indicate whether the specified index exists."""
es_client = es_client or create_es_client(es_host)
scoped_index_name = get_scoped_index_name(app_namespace, index_name)
try:
# Confirm ES connection with a shorter timeout
es_client.cluster.health(request_timeout=connect_timeout)
return es_client.indices.exists(index=scoped_index_name)
except EsConnectionError as e:
logger.debug(
"Unable to connect to Elasticsearch: %s details: %s", e.error, e.info
)
raise KnowledgeBaseConnectionError(es_host=es_client.transport.hosts)
except TransportError as e:
logger.error(
"Unexpected error occurred when sending requests to Elasticsearch: %s "
"Status code: %s details: %s",
e.error,
e.status_code,
e.info,
)
raise KnowledgeBaseError
except ElasticsearchException:
raise KnowledgeBaseError
# load field info from local cache
index_info = self._es_field_info.get(index, {})
if not index_info:
try:
# TODO: move the ES API call logic to ES helper
self._es_field_info[index] = {}
res = self._es_client.indices.get(index=index)
all_field_info = res[index]["mappings"]["document"]["properties"]
for field_name in all_field_info:
field_type = all_field_info[field_name].get("type")
self._es_field_info[index][field_name] = FieldInfo(
field_name, field_type
)
except EsConnectionError as e:
logger.error(
"Unable to connect to Elasticsearch: %s details: %s",
e.error,
e.info,
)
raise KnowledgeBaseConnectionError(
es_host=self._es_client.transport.hosts
)
except TransportError as e:
logger.error(
"Unexpected error occurred when sending requests to Elasticsearch: %s "
"Status code: %s details: %s",
e.error,
e.status_code,
e.info,
)