feat(knowledge): enhance FAQ entry filtering and tagging

- Updated the listFAQEntries function to support multiple tag IDs for filtering.
- Modified the frontend components to accommodate the new tag_ids parameter.
- Enhanced the KbTagManageDrawer and FAQEntryManager components to reflect changes in tag handling.
- Improved API documentation to clarify the new tag filtering capabilities.

This commit enhances the flexibility of FAQ entry retrieval, allowing users to filter by multiple tags for more precise results.
This commit is contained in:
wizardchen
2026-07-23 10:27:35 +08:00
committed by lyingbug
parent b6a3b347da
commit 8ca9c117bb
17 changed files with 485 additions and 744 deletions
+1 -1
View File
@@ -392,7 +392,7 @@ const buildQuery = (params?: Record<string, any>) => {
export function listFAQEntries(
kbId: string,
params?: { page?: number; page_size?: number; tag_id?: number; keyword?: string },
params?: { page?: number; page_size?: number; tag_id?: number; tag_ids?: string; keyword?: string },
) {
const query = buildQuery(params);
return get(`/api/v1/knowledge-bases/${kbId}/faq/entries${query}`);
@@ -2330,6 +2330,7 @@ async function createNewSession(value: string): Promise<void> {
@open-manage="openTagManageFromEditDialog" />
<KbTagManageDrawer
v-if="!isFAQ"
v-model:visible="tagManageDrawerVisible"
:kb-id="kbId"
:is-faq="isFAQ"
File diff suppressed because it is too large Load Diff
@@ -153,7 +153,7 @@
<span class="tag-tile__count">
{{
isFaq
? $t('knowledgeBase.tagManageFaqCount', { count: tag.knowledge_count || 0 })
? $t('knowledgeBase.tagManageFaqCount', { count: tag.chunk_count || 0 })
: $t('knowledgeBase.tagManageDocCount', { count: tag.knowledge_count || 0 })
}}
</span>
@@ -222,6 +222,7 @@ type TagRow = {
seq_id: number;
name: string;
knowledge_count?: number;
chunk_count?: number;
};
type TagInputInstance = ComponentPublicInstance<{ focus: () => void; select: () => void }>;
+1 -1
View File
@@ -72,7 +72,7 @@ func (t *DataSchemaTool) Execute(ctx context.Context, args json.RawMessage) (*ty
input.KnowledgeID,
page,
chunkTypes,
"", // tagID
nil, // tagIDs
"", // keyword
"", // searchField
"", // sortOrder
+1 -1
View File
@@ -209,7 +209,7 @@ func (t *GetDocumentInfoTool) Execute(ctx context.Context, args json.RawMessage)
ListPagedChunksByKnowledgeID(ctx, knowledge.TenantID, id, &types.Pagination{
Page: 1,
PageSize: 1,
}, []types.ChunkType{types.ChunkTypeText, types.ChunkTypeFAQ}, "", "", "", "", "")
}, []types.ChunkType{types.ChunkTypeText, types.ChunkTypeFAQ}, nil, "", "", "", "")
if err != nil {
mu.Lock()
results[id] = &docInfo{
+1 -1
View File
@@ -1175,7 +1175,7 @@ func (t *KnowledgeSearchTool) formatOutput(
_, total, err := t.chunkService.GetRepository().ListPagedChunksByKnowledgeID(ctx,
effectiveTenantID, result.KnowledgeID,
&types.Pagination{Page: 1, PageSize: 1},
[]types.ChunkType{types.ChunkTypeText, types.ChunkTypeFAQ}, "", "", "", "", "",
[]types.ChunkType{types.ChunkTypeText, types.ChunkTypeFAQ}, nil, "", "", "", "",
)
if err != nil {
logger.Warnf(ctx, "[Tool][KnowledgeSearch] Failed to get total chunks for knowledge %s: %v", result.KnowledgeID, err)
@@ -168,7 +168,7 @@ func (t *ListKnowledgeChunksTool) Execute(ctx context.Context, args json.RawMess
}
chunks, total, err := t.chunkService.GetRepository().ListPagedChunksByKnowledgeID(ctx,
effectiveTenantID, knowledgeID, pagination, []types.ChunkType{types.ChunkTypeText, types.ChunkTypeFAQ}, "", "", "", "", "")
effectiveTenantID, knowledgeID, pagination, []types.ChunkType{types.ChunkTypeText, types.ChunkTypeFAQ}, nil, "", "", "", "")
if err != nil {
return &types.ToolResult{
Success: false,
+1 -1
View File
@@ -195,7 +195,7 @@ func (t *wikiReadSourceDocTool) Execute(ctx context.Context, args json.RawMessag
knowledgeID,
pagination,
[]types.ChunkType{types.ChunkTypeText, types.ChunkTypeFAQ},
"", "", "", "", "",
nil, "", "", "", "",
)
if err != nil {
return &types.ToolResult{Success: false, Error: fmt.Sprintf("Failed to list chunks: %v", err)}, nil
+3 -3
View File
@@ -158,7 +158,7 @@ func (r *chunkRepository) ListPagedChunksByKnowledgeID(
knowledgeID string,
page *types.Pagination,
chunkType []types.ChunkType,
tagID string,
tagIDs []string,
keyword string,
searchField string,
sortOrder string,
@@ -171,8 +171,8 @@ 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 != "" {
db = db.Where("tag_id = ?", tagID)
if len(tagIDs) > 0 {
db = db.Where("tag_id IN ?", tagIDs)
}
if keyword != "" {
like := "%" + keyword + "%"
@@ -726,7 +726,7 @@ func (s *agentService) getKnowledgeBaseInfos(ctx context.Context, kbIDs []string
pageResult, err := s.knowledgeService.ListFAQEntries(ctx, kbID, &types.Pagination{
Page: 1,
PageSize: 10,
}, 0, "", "", "")
}, nil, 0, "", "", "")
if err == nil && pageResult != nil {
docCount = int(pageResult.Total)
if entries, ok := pageResult.Data.([]*types.FAQEntry); ok {
+1 -1
View File
@@ -168,7 +168,7 @@ func (s *chunkService) ListPagedChunksByKnowledgeID(ctx context.Context,
knowledgeID,
page,
chunkType,
"",
nil,
"",
"",
"",
@@ -317,7 +317,7 @@ func (s *knowledgeService) CloneChunk(ctx context.Context, src, dst *types.Knowl
PageSize: chunkPageSize,
},
chunkType,
"",
nil,
"",
"",
"",
@@ -21,7 +21,7 @@ import (
// ListFAQEntries lists FAQ entries under a FAQ knowledge base.
func (s *knowledgeService) ListFAQEntries(ctx context.Context,
kbID string, page *types.Pagination, tagSeqID int64, keyword string, searchField string, sortOrder string,
kbID string, page *types.Pagination, tagUUIDs []string, legacyTagSeqID int64, keyword string, searchField string, sortOrder string,
) (*types.PageResult, error) {
if page == nil {
page = &types.Pagination{}
@@ -68,19 +68,17 @@ func (s *knowledgeService) ListFAQEntries(ctx context.Context,
return types.NewPageResult(0, page, []*types.FAQEntry{}), nil
}
// Convert tagSeqID to tagID (UUID)
var tagID string
if tagSeqID > 0 {
tag, err := s.tagRepo.GetBySeqID(ctx, effectiveTenantID, tagSeqID)
if len(tagUUIDs) == 0 && legacyTagSeqID > 0 {
tag, err := s.tagRepo.GetBySeqID(ctx, effectiveTenantID, legacyTagSeqID)
if err != nil {
return nil, werrors.NewNotFoundError("标签不存在")
}
tagID = tag.ID
tagUUIDs = []string{tag.ID}
}
chunkType := []types.ChunkType{types.ChunkTypeFAQ}
chunks, total, err := s.chunkRepo.ListPagedChunksByKnowledgeID(
ctx, effectiveTenantID, faqKnowledge.ID, page, chunkType, tagID, keyword, searchField, sortOrder, types.KnowledgeTypeFAQ,
ctx, effectiveTenantID, faqKnowledge.ID, page, chunkType, tagUUIDs, keyword, searchField, sortOrder, types.KnowledgeTypeFAQ,
)
if err != nil {
return nil, err
+6 -4
View File
@@ -67,7 +67,8 @@ type updateLastFAQImportResultDisplayStatusRequest struct {
// @Param id path string true "知识库ID"
// @Param page query int false "页码"
// @Param page_size query int false "每页数量"
// @Param tag_id query int false "标签ID筛选(seq_id)"
// @Param tag_id query int false "标签ID筛选(seq_id),兼容旧版单标签"
// @Param tag_ids query string false "标签UUID筛选,逗号分隔(OR语义)"
// @Param keyword query string false "关键词搜索"
// @Param search_field query string false "搜索字段: standard_question(标准问题), similar_questions(相似问法), answers(答案), 默认搜索全部"
// @Param sort_order query string false "排序方式: asc(按更新时间正序), 默认按更新时间倒序"
@@ -87,11 +88,12 @@ func (h *FAQHandler) ListEntries(c *gin.Context) {
return
}
var tagSeqID int64
tagUUIDs := parseCommaSeparatedTagIDs(c.Query("tag_ids"))
var legacyTagSeqID int64
tagIDStr := c.Query("tag_id")
if tagIDStr != "" {
var err error
tagSeqID, err = strconv.ParseInt(tagIDStr, 10, 64)
legacyTagSeqID, err = strconv.ParseInt(tagIDStr, 10, 64)
if err != nil {
c.Error(errors.NewBadRequestError("tag_id 必须是整数"))
return
@@ -101,7 +103,7 @@ func (h *FAQHandler) ListEntries(c *gin.Context) {
searchField := secutils.SanitizeForLog(c.Query("search_field"))
sortOrder := secutils.SanitizeForLog(c.Query("sort_order"))
result, err := h.knowledgeService.ListFAQEntries(ctx, kbID, &page, tagSeqID, keyword, searchField, sortOrder)
result, err := h.knowledgeService.ListFAQEntries(ctx, kbID, &page, tagUUIDs, legacyTagSeqID, keyword, searchField, sortOrder)
if err != nil {
logger.ErrorWithFields(ctx, err, nil)
c.Error(err)
+2 -2
View File
@@ -31,7 +31,7 @@ type ChunkRepository interface {
// ListChunksByKnowledgeID lists chunks by knowledge id
ListChunksByKnowledgeID(ctx context.Context, tenantID uint64, knowledgeID string) ([]*types.Chunk, error)
// ListPagedChunksByKnowledgeID lists paged chunks by knowledge id.
// When tagID is non-empty, results are filtered by tag_id.
// When tagIDs is non-empty, results are filtered by tag_id (OR semantics).
// knowledgeType: "faq" or "manual" - determines sort order and search behavior
// - FAQ: sorts by updated_at, searchField can be "standard_question", "similar_questions", "answers", or "" for all
// - Document (manual): sorts by chunk_index, keyword searches content only
@@ -43,7 +43,7 @@ type ChunkRepository interface {
knowledgeID string,
page *types.Pagination,
chunkType []types.ChunkType,
tagID string,
tagIDs []string,
keyword string,
searchField string,
sortOrder string,
+3 -2
View File
@@ -119,14 +119,15 @@ type KnowledgeService interface {
// UpdateImageInfo updates image information for a knowledge chunk.
UpdateImageInfo(ctx context.Context, knowledgeID string, chunkID string, imageInfo string) error
// ListFAQEntries lists FAQ entries under a FAQ knowledge base.
// When tagSeqID is non-zero, results are filtered by tag seq_id on FAQ chunks.
// When tagUUIDs is non-empty, results are filtered by tag UUID on FAQ chunks (OR semantics).
// searchField: specifies which field to search in ("standard_question", "similar_questions", "answers", "" for all)
// sortOrder: "asc" for time ascending (updated_at ASC), default is time descending (updated_at DESC)
ListFAQEntries(
ctx context.Context,
kbID string,
page *types.Pagination,
tagSeqID int64,
tagUUIDs []string,
legacyTagSeqID int64,
keyword string,
searchField string,
sortOrder string,