fix: update creation date in migration file and enhance exception handling in Elasticsearch connector

This commit is contained in:
Anish Sarkar 2025-10-08 14:15:17 +05:30
parent e591cb9c86
commit 6136a13a11
2 changed files with 32 additions and 14 deletions

View file

@ -2,7 +2,7 @@
Revision ID: 22
Revises: 21
Create Date: 2025-10-4 12:00:00.000000
Create Date: 2025-10-08 12:00:00.000000
"""

View file

@ -6,6 +6,19 @@ from typing import Any
from elasticsearch import AsyncElasticsearch
from elasticsearch.exceptions import AuthenticationException, ConnectionError
try:
from elastic_transport import (
ApiError,
ConnectionError as TransportConnectionError,
TlsError,
)
except ImportError:
# Fallback for older versions
TransportConnectionError = ConnectionError
TlsError = Exception
ApiError = Exception
from app.db import DocumentType
logger = logging.getLogger(__name__)
@ -74,9 +87,13 @@ class ElasticsearchConnector:
)
return True
except (ConnectionError, AuthenticationException) as e:
# Updated exception handling for better version compatibility
except (ConnectionError, TransportConnectionError, TlsError, ApiError) as e:
logger.error(f"Failed to connect to Elasticsearch: {e}")
return False
except AuthenticationException as e:
logger.error(f"Authentication failed for Elasticsearch: {e}")
return False
except Exception as e:
logger.error(f"Unexpected error connecting to Elasticsearch: {e}")
return False
@ -140,23 +157,24 @@ class ElasticsearchConnector:
return
# Build search query
search_body = {
"query": {
"multi_match": {
"query": query,
"fields": fields or ["*"],
"type": "best_fields",
"fuzziness": "AUTO",
}
},
"size": size,
"_source": True,
search_query = {
"multi_match": {
"query": query,
"fields": fields or ["*"],
"type": "best_fields",
"fuzziness": "AUTO",
}
}
# Search across specified indices or all indices
index_pattern = ",".join(indices) if indices else "*"
response = await self.client.search(index=index_pattern, body=search_body)
response = await self.client.search(
index=index_pattern,
query=search_query,
size=size,
source=True,
)
for hit in response["hits"]["hits"]:
try: