Skip to content

OpenRAG is a comprehensive, single package Retrieval-Augmented Generation platform built on Langflow, Docling, and Opensearch.

Python

fix: Index creation on ingest/check duplicate if it doesn't exist (#978)

Mike Fortman3e5b77dc2e3b
diff --git a/src/api/connectors.py b/src/api/connectors.py
index da76541d2..c5578a802 100644
--- a/src/api/connectors.py
+++ b/src/api/connectors.py
@@ -178,6 +178,8 @@ async def connector_sync(request: Request, connector_service, session_manager):
if selected_files:
# Explicit files selected (e.g., from file picker) - sync those specific files
+ from .documents import _ensure_index_exists
+ await _ensure_index_exists()
task_id = await connector_service.sync_specific_files(
working_connection.connection_id,
user.user_id,
diff --git a/src/api/documents.py b/src/api/documents.py
index 8d7d386ec..31f1b2981 100644
--- a/src/api/documents.py
+++ b/src/api/documents.py
@@ -6,6 +6,12 @@ from config.settings import get_index_name
logger = get_logger(__name__)
+async def _ensure_index_exists():
+ """Create the OpenSearch index if it doesn't exist yet."""
+ from main import init_index
+ await init_index()
+
+
async def check_filename_exists(request: Request, document_service, session_manager):
"""Check if a document with a specific filename already exists"""
filename = request.query_params.get("filename")
@@ -29,10 +35,21 @@ async def check_filename_exists(request: Request, document_service, session_mana
logger.debug(f"Checking filename existence", filename=filename, index_name=get_index_name())
- response = await opensearch_client.search(
- index=get_index_name(),
- body=search_body
- )
+ try:
+ response = await opensearch_client.search(
+ index=get_index_name(),
+ body=search_body
+ )
+ except Exception as search_err:
+ if "index_not_found_exception" in str(search_err):
+ logger.info("Index does not exist, creating it now before upload")
+ await _ensure_index_exists()
+ # Index was just created so no duplicates can exist
+ return JSONResponse({
+ "exists": False,
+ "filename": filename
+ }, status_code=200)
+ raise
# Check if any hits were found
hits = response.get("hits", {}).get("hits", [])
diff --git a/src/api/upload.py b/src/api/upload.py
index 76a137fdc..3eab9b4c4 100644
--- a/src/api/upload.py
+++ b/src/api/upload.py
@@ -74,6 +74,9 @@ async def upload_path(request: Request, task_service, session_manager):
owner_name = user.name
owner_email = user.email
+ from .documents import _ensure_index_exists
+ await _ensure_index_exists()
+
task_id = await task_service.create_upload_task(
owner_user_id,
file_paths,
@@ -185,6 +188,9 @@ async def upload_bucket(request: Request, task_service, session_manager):
owner_email = user.email
task_user_id = user.user_id
+ from .documents import _ensure_index_exists
+ await _ensure_index_exists()
+
processor = S3FileProcessor(
task_service.document_service,
bucket,