refactor(knowledge): optimize tag retrieval in buildTagMap function

- Changed the tag retrieval process to support pagination, allowing for more efficient handling of large sets of tags.
- Adjusted the page size to 100 for better performance and reduced memory usage.
- Ensured that the function continues fetching tags until all are retrieved, improving the completeness of the tag map.
This commit is contained in:
wizardchen
2026-03-23 19:51:50 +08:00
committed by lyingbug
parent f368b987bb
commit a6c38123b6
+9 -4
View File
@@ -5901,19 +5901,24 @@ func (s *knowledgeService) ExportFAQEntries(ctx context.Context, kbID string) ([
// buildTagMap builds a map from tag_id to tag_name for the given knowledge base. // buildTagMap builds a map from tag_id to tag_name for the given knowledge base.
func (s *knowledgeService) buildTagMap(ctx context.Context, tenantID uint64, kbID string) (map[string]string, error) { func (s *knowledgeService) buildTagMap(ctx context.Context, tenantID uint64, kbID string) (map[string]string, error) {
// Get all tags for this knowledge base (no pagination limit) const pageSize = 100
page := &types.Pagination{Page: 1, PageSize: 10000} tagMap := make(map[string]string)
for pageNum := 1; ; pageNum++ {
page := &types.Pagination{Page: pageNum, PageSize: pageSize}
tags, _, err := s.tagRepo.ListByKB(ctx, tenantID, kbID, page, "") tags, _, err := s.tagRepo.ListByKB(ctx, tenantID, kbID, page, "")
if err != nil { if err != nil {
return nil, err return nil, err
} }
tagMap := make(map[string]string, len(tags))
for _, tag := range tags { for _, tag := range tags {
if tag != nil { if tag != nil {
tagMap[tag.ID] = tag.Name tagMap[tag.ID] = tag.Name
} }
} }
if len(tags) < pageSize {
break
}
}
return tagMap, nil return tagMap, nil
} }