feat: 将未分类FAQ条目转为"未分类"标签

This commit is contained in:
wizardchen
2026-01-07 11:08:59 +08:00
parent 9af1fc18f5
commit b71e7084fd
16 changed files with 187 additions and 179 deletions
+5 -16
View File
@@ -91,10 +91,7 @@ func (r *chunkRepository) ListPagedChunksByKnowledgeID(
baseFilter := func(db *gorm.DB) *gorm.DB {
db = db.Where("tenant_id = ? AND knowledge_id = ? AND chunk_type IN (?) AND status in (?)",
tenantID, knowledgeID, chunkType, []int{int(types.ChunkStatusIndexed), int(types.ChunkStatusDefault)})
if tagID == types.UntaggedTagID {
// Special value to filter entries without a tag
db = db.Where("tag_id = ''")
} else if tagID != "" {
if tagID != "" {
db = db.Where("tag_id = ?", tagID)
}
if keyword != "" {
@@ -640,9 +637,7 @@ func (r *chunkRepository) UpdateChunkFieldsByTagID(
Select("id").
Where("tenant_id = ? AND knowledge_base_id = ? AND chunk_type = ?",
tenantID, kbID, types.ChunkTypeFAQ)
if tagID == "" || tagID == types.UntaggedTagID {
query = query.Where("(tag_id = '' OR tag_id IS NULL)")
} else {
if tagID != "" {
query = query.Where("tag_id = ?", tagID)
}
@@ -669,22 +664,16 @@ func (r *chunkRepository) UpdateChunkFieldsByTagID(
updates["is_enabled"] = *isEnabled
}
// Handle newTagID update (__untagged__ is stored as empty string)
// Handle newTagID update
if newTagID != nil {
if *newTagID == types.UntaggedTagID || *newTagID == "" {
updates["tag_id"] = ""
} else {
updates["tag_id"] = *newTagID
}
updates["tag_id"] = *newTagID
}
query := r.db.WithContext(ctx).Model(&types.Chunk{}).
Where("tenant_id = ? AND knowledge_base_id = ? AND chunk_type = ?",
tenantID, kbID, types.ChunkTypeFAQ)
if tagID == "" || tagID == types.UntaggedTagID {
query = query.Where("(tag_id = '' OR tag_id IS NULL)")
} else {
if tagID != "" {
query = query.Where("tag_id = ?", tagID)
}
+2 -12
View File
@@ -72,12 +72,7 @@ func (r *knowledgeRepository) ListPagedKnowledgeByKnowledgeBaseID(
query := r.db.WithContext(ctx).Model(&types.Knowledge{}).
Where("tenant_id = ? AND knowledge_base_id = ?", tenantID, kbID)
if tagID != "" {
if tagID == types.UntaggedTagID {
// Special value to filter entries without a tag
query = query.Where("tag_id = '' OR tag_id IS NULL")
} else {
query = query.Where("tag_id = ?", tagID)
}
query = query.Where("tag_id = ?", tagID)
}
if keyword != "" {
query = query.Where("file_name LIKE ?", "%"+keyword+"%")
@@ -101,12 +96,7 @@ func (r *knowledgeRepository) ListPagedKnowledgeByKnowledgeBaseID(
dataQuery := r.db.WithContext(ctx).
Where("tenant_id = ? AND knowledge_base_id = ?", tenantID, kbID)
if tagID != "" {
if tagID == types.UntaggedTagID {
// Special value to filter entries without a tag
dataQuery = dataQuery.Where("tag_id = '' OR tag_id IS NULL")
} else {
dataQuery = dataQuery.Where("tag_id = ?", tagID)
}
dataQuery = dataQuery.Where("tag_id = ?", tagID)
}
if keyword != "" {
dataQuery = dataQuery.Where("file_name LIKE ?", "%"+keyword+"%")
-21
View File
@@ -136,27 +136,6 @@ func (r *knowledgeTagRepository) CountReferences(
return
}
// CountUntaggedReferences returns the number of knowledges and chunks without a tag
func (r *knowledgeTagRepository) CountUntaggedReferences(
ctx context.Context,
tenantID uint64,
kbID string,
) (knowledgeCount int64, chunkCount int64, err error) {
if err = r.db.WithContext(ctx).
Model(&types.Knowledge{}).
Where("tenant_id = ? AND knowledge_base_id = ? AND (tag_id = '' OR tag_id IS NULL)", tenantID, kbID).
Count(&knowledgeCount).Error; err != nil {
return
}
if err = r.db.WithContext(ctx).
Model(&types.Chunk{}).
Where("tenant_id = ? AND knowledge_base_id = ? AND (tag_id = '' OR tag_id IS NULL)", tenantID, kbID).
Count(&chunkCount).Error; err != nil {
return
}
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) {