feat: 支持FAQ两级优先级标签过滤
添加FAQ搜索支持两级优先级标签过滤功能,新增first_priority_tag_ids和second_priority_tag_ids字段,更新各检索器实现以支持按标签ID过滤,优化搜索结果排序确保优先级标签结果优先展示。
This commit is contained in:
@@ -14,6 +14,7 @@ import (
|
||||
"slices"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/Tencent/WeKnora/docreader/client"
|
||||
@@ -3915,18 +3916,101 @@ func (s *knowledgeService) SearchFAQEntries(ctx context.Context,
|
||||
req.MatchCount = 50
|
||||
}
|
||||
|
||||
// Prepare search parameters
|
||||
searchParams := types.SearchParams{
|
||||
QueryText: secutils.SanitizeForLog(req.QueryText),
|
||||
VectorThreshold: req.VectorThreshold,
|
||||
MatchCount: req.MatchCount,
|
||||
DisableKeywordsMatch: true,
|
||||
// Build priority tag sets for sorting
|
||||
hasFirstPriority := len(req.FirstPriorityTagIDs) > 0
|
||||
hasSecondPriority := len(req.SecondPriorityTagIDs) > 0
|
||||
hasPriorityFilter := hasFirstPriority || hasSecondPriority
|
||||
|
||||
firstPrioritySet := make(map[string]struct{}, len(req.FirstPriorityTagIDs))
|
||||
for _, tagID := range req.FirstPriorityTagIDs {
|
||||
firstPrioritySet[tagID] = struct{}{}
|
||||
}
|
||||
secondPrioritySet := make(map[string]struct{}, len(req.SecondPriorityTagIDs))
|
||||
for _, tagID := range req.SecondPriorityTagIDs {
|
||||
secondPrioritySet[tagID] = struct{}{}
|
||||
}
|
||||
|
||||
// Call HybridSearch
|
||||
searchResults, err := s.kbService.HybridSearch(ctx, kbID, searchParams)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
// Perform separate searches for each priority level to ensure FirstPriority results
|
||||
// are not crowded out by higher-scoring SecondPriority results in TopK truncation
|
||||
var searchResults []*types.SearchResult
|
||||
|
||||
if hasPriorityFilter {
|
||||
// Use goroutines to search both priority levels concurrently
|
||||
var (
|
||||
firstResults []*types.SearchResult
|
||||
secondResults []*types.SearchResult
|
||||
firstErr error
|
||||
secondErr error
|
||||
wg sync.WaitGroup
|
||||
)
|
||||
|
||||
if hasFirstPriority {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
firstParams := types.SearchParams{
|
||||
QueryText: secutils.SanitizeForLog(req.QueryText),
|
||||
VectorThreshold: req.VectorThreshold,
|
||||
MatchCount: req.MatchCount,
|
||||
DisableKeywordsMatch: true,
|
||||
TagIDs: req.FirstPriorityTagIDs,
|
||||
}
|
||||
firstResults, firstErr = s.kbService.HybridSearch(ctx, kbID, firstParams)
|
||||
}()
|
||||
}
|
||||
|
||||
if hasSecondPriority {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
secondParams := types.SearchParams{
|
||||
QueryText: secutils.SanitizeForLog(req.QueryText),
|
||||
VectorThreshold: req.VectorThreshold,
|
||||
MatchCount: req.MatchCount,
|
||||
DisableKeywordsMatch: true,
|
||||
TagIDs: req.SecondPriorityTagIDs,
|
||||
}
|
||||
secondResults, secondErr = s.kbService.HybridSearch(ctx, kbID, secondParams)
|
||||
}()
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
|
||||
// Check errors
|
||||
if firstErr != nil {
|
||||
return nil, firstErr
|
||||
}
|
||||
if secondErr != nil {
|
||||
return nil, secondErr
|
||||
}
|
||||
|
||||
// Merge results: FirstPriority first, then SecondPriority (deduplicated)
|
||||
seenChunkIDs := make(map[string]struct{})
|
||||
for _, result := range firstResults {
|
||||
if _, exists := seenChunkIDs[result.ID]; !exists {
|
||||
seenChunkIDs[result.ID] = struct{}{}
|
||||
searchResults = append(searchResults, result)
|
||||
}
|
||||
}
|
||||
for _, result := range secondResults {
|
||||
if _, exists := seenChunkIDs[result.ID]; !exists {
|
||||
seenChunkIDs[result.ID] = struct{}{}
|
||||
searchResults = append(searchResults, result)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// No priority filter, search all
|
||||
searchParams := types.SearchParams{
|
||||
QueryText: secutils.SanitizeForLog(req.QueryText),
|
||||
VectorThreshold: req.VectorThreshold,
|
||||
MatchCount: req.MatchCount,
|
||||
DisableKeywordsMatch: true,
|
||||
}
|
||||
var err error
|
||||
searchResults, err = s.kbService.HybridSearch(ctx, kbID, searchParams)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
if len(searchResults) == 0 {
|
||||
@@ -3982,9 +4066,52 @@ func (s *knowledgeService) SearchFAQEntries(ctx context.Context,
|
||||
entries = append(entries, entry)
|
||||
}
|
||||
|
||||
slices.SortFunc(entries, func(a, b *types.FAQEntry) int {
|
||||
return int(b.Score - a.Score)
|
||||
})
|
||||
// Sort entries with two-level priority tag support
|
||||
if hasPriorityFilter {
|
||||
// getPriorityLevel returns: 0 = first priority, 1 = second priority, 2 = no priority
|
||||
getPriorityLevel := func(tagID string) int {
|
||||
if _, ok := firstPrioritySet[tagID]; ok {
|
||||
return 0
|
||||
}
|
||||
if _, ok := secondPrioritySet[tagID]; ok {
|
||||
return 1
|
||||
}
|
||||
return 2
|
||||
}
|
||||
|
||||
slices.SortFunc(entries, func(a, b *types.FAQEntry) int {
|
||||
aPriority := getPriorityLevel(a.TagID)
|
||||
bPriority := getPriorityLevel(b.TagID)
|
||||
|
||||
// Compare by priority level first
|
||||
if aPriority != bPriority {
|
||||
return aPriority - bPriority // Lower level = higher priority
|
||||
}
|
||||
|
||||
// Same priority level, sort by score descending
|
||||
if b.Score > a.Score {
|
||||
return 1
|
||||
} else if b.Score < a.Score {
|
||||
return -1
|
||||
}
|
||||
return 0
|
||||
})
|
||||
} else {
|
||||
// No priority tags, sort by score only
|
||||
slices.SortFunc(entries, func(a, b *types.FAQEntry) int {
|
||||
if b.Score > a.Score {
|
||||
return 1
|
||||
} else if b.Score < a.Score {
|
||||
return -1
|
||||
}
|
||||
return 0
|
||||
})
|
||||
}
|
||||
|
||||
// Limit results to requested match count
|
||||
if len(entries) > req.MatchCount {
|
||||
entries = entries[:req.MatchCount]
|
||||
}
|
||||
|
||||
return entries, nil
|
||||
}
|
||||
@@ -4504,6 +4631,7 @@ func (s *knowledgeService) buildFAQIndexInfoList(
|
||||
KnowledgeID: chunk.KnowledgeID,
|
||||
KnowledgeBaseID: chunk.KnowledgeBaseID,
|
||||
KnowledgeType: types.KnowledgeTypeFAQ,
|
||||
TagID: chunk.TagID,
|
||||
IsEnabled: chunk.IsEnabled,
|
||||
},
|
||||
}, nil
|
||||
@@ -4531,6 +4659,7 @@ func (s *knowledgeService) buildFAQIndexInfoList(
|
||||
KnowledgeID: chunk.KnowledgeID,
|
||||
KnowledgeBaseID: chunk.KnowledgeBaseID,
|
||||
KnowledgeType: types.KnowledgeTypeFAQ,
|
||||
TagID: chunk.TagID,
|
||||
IsEnabled: chunk.IsEnabled,
|
||||
})
|
||||
|
||||
@@ -4555,6 +4684,7 @@ func (s *knowledgeService) buildFAQIndexInfoList(
|
||||
KnowledgeID: chunk.KnowledgeID,
|
||||
KnowledgeBaseID: chunk.KnowledgeBaseID,
|
||||
KnowledgeType: types.KnowledgeTypeFAQ,
|
||||
TagID: chunk.TagID,
|
||||
IsEnabled: chunk.IsEnabled,
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user