feat: 优化标签ID处理逻辑,新增批量更新TagID功能
This commit is contained in:
@@ -90,7 +90,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 == "__untagged__" {
|
||||
if tagID == types.UntaggedTagID {
|
||||
// Special value to filter entries without a tag
|
||||
db = db.Where("tag_id = ''")
|
||||
} else if tagID != "" {
|
||||
@@ -601,6 +601,7 @@ func (r *chunkRepository) UpdateChunkFlagsBatch(
|
||||
|
||||
// UpdateChunkFieldsByTagID updates fields for all chunks with the specified tag ID.
|
||||
// Returns the list of affected chunk IDs for syncing with retriever engines.
|
||||
// newTagID: if not nil, updates tag_id to this value (empty string means uncategorized)
|
||||
func (r *chunkRepository) UpdateChunkFieldsByTagID(
|
||||
ctx context.Context,
|
||||
tenantID uint64,
|
||||
@@ -609,6 +610,7 @@ func (r *chunkRepository) UpdateChunkFieldsByTagID(
|
||||
isEnabled *bool,
|
||||
setFlags types.ChunkFlags,
|
||||
clearFlags types.ChunkFlags,
|
||||
newTagID *string,
|
||||
excludeIDs []string,
|
||||
) ([]string, error) {
|
||||
// First, get the IDs of chunks that will be affected (for is_enabled sync)
|
||||
@@ -619,7 +621,7 @@ func (r *chunkRepository) UpdateChunkFieldsByTagID(
|
||||
Select("id").
|
||||
Where("tenant_id = ? AND knowledge_base_id = ? AND chunk_type = ?",
|
||||
tenantID, kbID, types.ChunkTypeFAQ)
|
||||
if tagID == "" {
|
||||
if tagID == "" || tagID == types.UntaggedTagID {
|
||||
query = query.Where("(tag_id = '' OR tag_id IS NULL)")
|
||||
} else {
|
||||
query = query.Where("tag_id = ?", tagID)
|
||||
@@ -648,11 +650,20 @@ func (r *chunkRepository) UpdateChunkFieldsByTagID(
|
||||
updates["is_enabled"] = *isEnabled
|
||||
}
|
||||
|
||||
// Handle newTagID update (__untagged__ is stored as empty string)
|
||||
if newTagID != nil {
|
||||
if *newTagID == types.UntaggedTagID {
|
||||
updates["tag_id"] = ""
|
||||
} else {
|
||||
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 == "" {
|
||||
if tagID == "" || tagID == types.UntaggedTagID {
|
||||
query = query.Where("(tag_id = '' OR tag_id IS NULL)")
|
||||
} else {
|
||||
query = query.Where("tag_id = ?", tagID)
|
||||
|
||||
@@ -3570,7 +3570,7 @@ func (s *knowledgeService) UpdateFAQEntryFieldsBatch(ctx context.Context,
|
||||
// Update all chunks with this tag
|
||||
affectedIDs, err := s.chunkRepo.UpdateChunkFieldsByTagID(
|
||||
ctx, tenantID, kb.ID, tagID,
|
||||
update.IsEnabled, setFlags, clearFlags, req.ExcludeIDs,
|
||||
update.IsEnabled, setFlags, clearFlags, update.TagID, req.ExcludeIDs,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
Reference in New Issue
Block a user