Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
Returns:
a list of matching documents.
"""
try:
# TODO: move the ES API call logic to ES helper
es_query = self._build_es_query(size=size)
response = self.client.search(index=self.index, body=es_query)
results = [hit["_source"] for hit in response["hits"]["hits"]]
return results
except EsConnectionError as e:
logger.error(
"Unable to connect to Elasticsearch: %s details: %s", e.error, e.info
)
raise KnowledgeBaseConnectionError(es_host=self.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
):
"""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