2023-06-01 15:19:36 +00:00
|
|
|
import logging
|
|
|
|
|
import time
|
|
|
|
|
|
|
|
|
|
import click
|
2025-08-24 15:07:22 +00:00
|
|
|
from celery import shared_task
|
2026-01-21 05:43:06 +00:00
|
|
|
from sqlalchemy import delete, select
|
2024-02-06 05:21:13 +00:00
|
|
|
|
2026-01-21 05:43:06 +00:00
|
|
|
from core.db.session_factory import session_factory
|
2024-09-11 08:40:52 +00:00
|
|
|
from core.indexing_runner import DocumentIsPausedError, IndexingRunner
|
2024-02-22 15:31:57 +00:00
|
|
|
from core.rag.index_processor.index_processor_factory import IndexProcessorFactory
|
2025-08-22 15:53:05 +00:00
|
|
|
from libs.datetime_utils import naive_utc_now
|
2024-01-12 04:34:01 +00:00
|
|
|
from models.dataset import Dataset, Document, DocumentSegment
|
2023-06-01 15:19:36 +00:00
|
|
|
|
2025-08-26 10:10:31 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
2023-06-01 15:19:36 +00:00
|
|
|
|
2024-08-26 05:38:37 +00:00
|
|
|
@shared_task(queue="dataset")
|
2023-06-01 15:19:36 +00:00
|
|
|
def document_indexing_update_task(dataset_id: str, document_id: str):
|
|
|
|
|
"""
|
|
|
|
|
Async update document
|
|
|
|
|
:param dataset_id:
|
|
|
|
|
:param document_id:
|
|
|
|
|
|
|
|
|
|
Usage: document_indexing_update_task.delay(dataset_id, document_id)
|
|
|
|
|
"""
|
2025-08-26 10:10:31 +00:00
|
|
|
logger.info(click.style(f"Start update document: {document_id}", fg="green"))
|
2023-06-01 15:19:36 +00:00
|
|
|
start_at = time.perf_counter()
|
|
|
|
|
|
2026-02-05 06:42:34 +00:00
|
|
|
with session_factory.create_session() as session, session.begin():
|
2026-01-21 05:43:06 +00:00
|
|
|
document = session.query(Document).where(Document.id == document_id, Document.dataset_id == dataset_id).first()
|
2023-06-01 15:19:36 +00:00
|
|
|
|
2026-01-21 05:43:06 +00:00
|
|
|
if not document:
|
|
|
|
|
logger.info(click.style(f"Document not found: {document_id}", fg="red"))
|
|
|
|
|
return
|
2023-06-01 15:19:36 +00:00
|
|
|
|
2026-01-21 05:43:06 +00:00
|
|
|
document.indexing_status = "parsing"
|
|
|
|
|
document.processing_started_at = naive_utc_now()
|
2023-06-01 15:19:36 +00:00
|
|
|
|
2026-02-09 02:49:23 +00:00
|
|
|
dataset = session.query(Dataset).where(Dataset.id == dataset_id).first()
|
|
|
|
|
if not dataset:
|
|
|
|
|
return
|
2023-06-01 15:19:36 +00:00
|
|
|
|
2026-02-09 02:49:23 +00:00
|
|
|
index_type = document.doc_form
|
|
|
|
|
segments = session.scalars(select(DocumentSegment).where(DocumentSegment.document_id == document_id)).all()
|
|
|
|
|
index_node_ids = [segment.index_node_id for segment in segments]
|
2026-02-05 06:42:34 +00:00
|
|
|
|
2026-02-09 02:49:23 +00:00
|
|
|
clean_success = False
|
|
|
|
|
try:
|
|
|
|
|
index_processor = IndexProcessorFactory(index_type).init_index_processor()
|
|
|
|
|
if index_node_ids:
|
|
|
|
|
index_processor.clean(dataset, index_node_ids, with_keywords=True, delete_child_chunks=True)
|
2026-01-21 05:43:06 +00:00
|
|
|
end_at = time.perf_counter()
|
|
|
|
|
logger.info(
|
|
|
|
|
click.style(
|
|
|
|
|
"Cleaned document when document update data source or process rule: {} latency: {}".format(
|
|
|
|
|
document_id, end_at - start_at
|
|
|
|
|
),
|
|
|
|
|
fg="green",
|
|
|
|
|
)
|
2024-08-26 05:38:37 +00:00
|
|
|
)
|
2026-02-09 02:49:23 +00:00
|
|
|
clean_success = True
|
|
|
|
|
except Exception:
|
|
|
|
|
logger.exception("Failed to clean document index during update, document_id: %s", document_id)
|
2023-06-25 08:49:14 +00:00
|
|
|
|
2026-02-09 02:49:23 +00:00
|
|
|
if clean_success:
|
|
|
|
|
with session_factory.create_session() as session, session.begin():
|
|
|
|
|
segment_delete_stmt = delete(DocumentSegment).where(DocumentSegment.document_id == document_id)
|
|
|
|
|
session.execute(segment_delete_stmt)
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
indexing_runner = IndexingRunner()
|
|
|
|
|
indexing_runner.run([document])
|
|
|
|
|
end_at = time.perf_counter()
|
|
|
|
|
logger.info(click.style(f"update document: {document.id} latency: {end_at - start_at}", fg="green"))
|
|
|
|
|
except DocumentIsPausedError as ex:
|
|
|
|
|
logger.info(click.style(str(ex), fg="yellow"))
|
|
|
|
|
except Exception:
|
|
|
|
|
logger.exception("document_indexing_update_task failed, document_id: %s", document_id)
|