refactor: simplify document deletion logic and remove redundant chunk handling

This commit is contained in:
mlogclub
2026-06-02 11:33:24 +08:00
parent 7a2f673a02
commit 62c55f5a47
2 changed files with 4 additions and 13 deletions
-4
View File
@@ -120,10 +120,6 @@ func (s *index) IndexFAQByID(ctx context.Context, faqID int64) error {
func (s *index) RemoveDocumentIndex(ctx context.Context, documentID int64) error {
chunks := repositories.KnowledgeChunkRepository.FindByDocumentID(sqls.DB(), documentID)
return s.RemoveDocumentIndexByChunks(ctx, documentID, chunks)
}
func (s *index) RemoveDocumentIndexByChunks(ctx context.Context, documentID int64, chunks []models.KnowledgeChunk) error {
if len(chunks) == 0 {
return nil
}
@@ -167,18 +167,13 @@ func (s *knowledgeDocumentService) UpdateKnowledgeDocument(req request.UpdateKno
}
func (s *knowledgeDocumentService) DeleteKnowledgeDocument(id int64) error {
chunks := repositories.KnowledgeChunkRepository.FindByDocumentID(sqls.DB(), id)
if err := sqls.WithTransaction(func(ctx *sqls.TxContext) error {
_ = repositories.KnowledgeDocumentRepository.Updates(ctx.Tx, id, map[string]any{
"status": enums.StatusDeleted,
"updated_at": time.Now(),
})
ctx.Tx.Delete(&models.KnowledgeChunk{}, "document_id = ?", id)
return nil
if err := repositories.KnowledgeDocumentRepository.Updates(sqls.DB(), id, map[string]any{
"status": enums.StatusDeleted,
"updated_at": time.Now(),
}); err != nil {
return err
}
return rag.Index.RemoveDocumentIndexByChunks(context.Background(), id, chunks)
return rag.Index.RemoveDocumentIndex(context.Background(), id)
}
func (s *knowledgeDocumentService) buildKnowledgeDocumentModel(req request.CreateKnowledgeDocumentRequest) (*models.KnowledgeDocument, error) {