feat: Replace模式清理未使用标签
This commit is contained in:
@@ -156,3 +156,15 @@ func (r *knowledgeTagRepository) CountUntaggedReferences(
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// DeleteUnusedTags deletes tags that are not referenced by any knowledge or chunk.
|
||||
// Returns the number of deleted tags.
|
||||
func (r *knowledgeTagRepository) DeleteUnusedTags(ctx context.Context, tenantID uint64, kbID string) (int64, error) {
|
||||
// Delete tags that have no references in both knowledges and chunks tables
|
||||
result := r.db.WithContext(ctx).
|
||||
Where("tenant_id = ? AND knowledge_base_id = ?", tenantID, kbID).
|
||||
Where("id NOT IN (SELECT DISTINCT tag_id FROM knowledges WHERE tenant_id = ? AND knowledge_base_id = ? AND tag_id IS NOT NULL AND tag_id != '')", tenantID, kbID).
|
||||
Where("id NOT IN (SELECT DISTINCT tag_id FROM chunks WHERE tenant_id = ? AND knowledge_base_id = ? AND tag_id IS NOT NULL AND tag_id != '')", tenantID, kbID).
|
||||
Delete(&types.KnowledgeTag{})
|
||||
return result.RowsAffected, result.Error
|
||||
}
|
||||
|
||||
@@ -3067,6 +3067,14 @@ func (s *knowledgeService) executeFAQImport(ctx context.Context, taskID string,
|
||||
return fmt.Errorf("failed to delete chunk vectors: %w", err)
|
||||
}
|
||||
logger.Infof(ctx, "FAQ import task %s: deleted %d chunks (including updates)", taskID, len(chunksToDelete))
|
||||
|
||||
// 清理不再被引用的Tag
|
||||
deletedTags, err := s.tagRepo.DeleteUnusedTags(ctx, tenantID, kb.ID)
|
||||
if err != nil {
|
||||
logger.Warnf(ctx, "FAQ import task %s: failed to cleanup unused tags: %v", taskID, err)
|
||||
} else if deletedTags > 0 {
|
||||
logger.Infof(ctx, "FAQ import task %s: cleaned up %d unused tags", taskID, deletedTags)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Append模式:查询已存在的条目,跳过未变化的
|
||||
|
||||
@@ -54,4 +54,6 @@ type KnowledgeTagRepository interface {
|
||||
tenantID uint64,
|
||||
kbID string,
|
||||
) (knowledgeCount int64, chunkCount int64, err error)
|
||||
// DeleteUnusedTags deletes tags that are not referenced by any knowledge or chunk.
|
||||
DeleteUnusedTags(ctx context.Context, tenantID uint64, kbID string) (int64, error)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user