Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

feat: Replace模式清理未使用标签

wizardchenbebeb2a643aa
diff --git a/internal/application/repository/tag.go b/internal/application/repository/tag.go
index 778170bf6..10e651f34 100644
--- a/internal/application/repository/tag.go
+++ b/internal/application/repository/tag.go
@@ -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
+}
diff --git a/internal/application/service/knowledge.go b/internal/application/service/knowledge.go
index e93377861..1c9f286d2 100644
--- a/internal/application/service/knowledge.go
+++ b/internal/application/service/knowledge.go
@@ -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模式:查询已存在的条目,跳过未变化的
diff --git a/internal/types/interfaces/tag.go b/internal/types/interfaces/tag.go
index 36be449a2..40aa37adb 100644
--- a/internal/types/interfaces/tag.go
+++ b/internal/types/interfaces/tag.go
@@ -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)
}