mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-07-20 23:21:06 +02:00
fix: update creation date in migration file and enhance exception handling in Elasticsearch connector
This commit is contained in:
parent
e591cb9c86
commit
6136a13a11
2 changed files with 32 additions and 14 deletions
|
|
@ -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
|
||||
|
||||
"""
|
||||
|
||||
|
|
|
|||
|
|
@ -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:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue