fix(knowledge-base): 停用文档时同步清理索引
This commit is contained in:
@@ -153,6 +153,7 @@ def create_document_from_upload(
|
||||
|
||||
def update_document(document: KnowledgeBaseDocument, payload: dict[str, Any]) -> KnowledgeBaseDocument:
|
||||
update_fields = []
|
||||
active_changed = False
|
||||
if "display_name" in payload:
|
||||
document.display_name = str(payload.get("display_name") or "").strip() or document.original_name
|
||||
update_fields.append("display_name")
|
||||
@@ -160,12 +161,21 @@ def update_document(document: KnowledgeBaseDocument, payload: dict[str, Any]) ->
|
||||
document.description = str(payload.get("description") or "").strip()
|
||||
update_fields.append("description")
|
||||
if "is_active" in payload:
|
||||
document.is_active = bool(payload.get("is_active"))
|
||||
document.status = KnowledgeBaseDocument.Status.ACTIVE if document.is_active else KnowledgeBaseDocument.Status.DISABLED
|
||||
next_is_active = bool(payload.get("is_active"))
|
||||
active_changed = document.is_active != next_is_active
|
||||
document.is_active = next_is_active
|
||||
document.status = KnowledgeBaseDocument.Status.ACTIVE if next_is_active else KnowledgeBaseDocument.Status.DISABLED
|
||||
update_fields.extend(["is_active", "status"])
|
||||
if not next_is_active:
|
||||
remove_managed_document_from_index(document)
|
||||
document.indexed_chunk_count = 0
|
||||
document.metadata = {**(document.metadata or {}), "index_status": "disabled", "index_error": ""}
|
||||
update_fields.extend(["indexed_chunk_count", "metadata"])
|
||||
if update_fields:
|
||||
update_fields.append("updated_at")
|
||||
document.save(update_fields=update_fields)
|
||||
if active_changed and document.is_active:
|
||||
index_managed_document(document)
|
||||
return document
|
||||
|
||||
|
||||
@@ -198,6 +208,12 @@ def serialize_document(document: KnowledgeBaseDocument) -> dict[str, Any]:
|
||||
|
||||
|
||||
def index_managed_document(document: KnowledgeBaseDocument) -> int:
|
||||
if document.status != KnowledgeBaseDocument.Status.ACTIVE or not document.is_active:
|
||||
remove_managed_document_from_index(document)
|
||||
document.indexed_chunk_count = 0
|
||||
document.metadata = {**(document.metadata or {}), "index_status": "disabled", "index_error": ""}
|
||||
document.save(update_fields=["indexed_chunk_count", "metadata", "updated_at"])
|
||||
return 0
|
||||
path = Path(document.storage_path)
|
||||
if not path.is_absolute():
|
||||
path = Path(settings.MEDIA_ROOT) / document.storage_path
|
||||
|
||||
Reference in New Issue
Block a user