feat: add matched question field to FAQ search results

This commit is contained in:
wizardchen
2026-01-21 17:58:06 +08:00
committed by lyingbug
parent 9b1381a844
commit faf1c9a37b
11 changed files with 75 additions and 9 deletions
+8 -1
View File
@@ -4937,16 +4937,18 @@ func (s *knowledgeService) SearchFAQEntries(ctx context.Context,
return []*types.FAQEntry{}, nil
}
// Extract chunk IDs and build score/match type maps
// Extract chunk IDs and build score/match type/matched content maps
chunkIDs := make([]string, 0, len(searchResults))
chunkScores := make(map[string]float64)
chunkMatchTypes := make(map[string]types.MatchType)
chunkMatchedContents := make(map[string]string)
for _, result := range searchResults {
// SearchResult.ID is the chunk ID
chunkID := result.ID
chunkIDs = append(chunkIDs, chunkID)
chunkScores[chunkID] = result.Score
chunkMatchTypes[chunkID] = result.MatchType
chunkMatchedContents[chunkID] = result.MatchedContent
}
// Batch fetch chunks
@@ -5003,6 +5005,11 @@ func (s *knowledgeService) SearchFAQEntries(ctx context.Context,
entry.MatchType = matchType
}
// Set MatchedQuestion from search result's matched content
if matchedContent, ok := chunkMatchedContents[chunk.ID]; ok && matchedContent != "" {
entry.MatchedQuestion = matchedContent
}
entries = append(entries, entry)
}
@@ -998,6 +998,7 @@ func (s *knowledgeBaseService) processSearchResults(ctx context.Context,
var chunkIDs []string
chunkScores := make(map[string]float64)
chunkMatchTypes := make(map[string]types.MatchType)
chunkMatchedContents := make(map[string]string)
processedKnowledgeIDs := make(map[string]bool)
// Collect all knowledge and chunk IDs
@@ -1010,6 +1011,7 @@ func (s *knowledgeBaseService) processSearchResults(ctx context.Context,
chunkIDs = append(chunkIDs, chunk.ChunkID)
chunkScores[chunk.ChunkID] = chunk.Score
chunkMatchTypes[chunk.ChunkID] = chunk.MatchType
chunkMatchedContents[chunk.ChunkID] = chunk.Content
}
// Batch fetch knowledge data
@@ -1110,7 +1112,8 @@ func (s *knowledgeBaseService) processSearchResults(ctx context.Context,
score := chunkScores[chunk.ID]
if knowledge, ok := knowledgeMap[chunk.KnowledgeID]; ok {
matchType := chunkMatchTypes[chunk.ID]
searchResults = append(searchResults, s.buildSearchResult(chunk, knowledge, score, matchType))
matchedContent := chunkMatchedContents[chunk.ID]
searchResults = append(searchResults, s.buildSearchResult(chunk, knowledge, score, matchType, matchedContent))
addedChunkIDs[chunk.ID] = true
} else {
logger.Warnf(ctx, "Knowledge not found for chunk: %s, knowledge_id: %s", chunk.ID, chunk.KnowledgeID)
@@ -1136,7 +1139,8 @@ func (s *knowledgeBaseService) processSearchResults(ctx context.Context,
logger.Warnf(ctx, "Unkonwn match type for chunk: %s", chunkID)
continue
}
searchResults = append(searchResults, s.buildSearchResult(chunk, knowledge, score, matchType))
matchedContent := chunkMatchedContents[chunkID]
searchResults = append(searchResults, s.buildSearchResult(chunk, knowledge, score, matchType, matchedContent))
}
}
logger.Infof(ctx, "Search results processed, total: %d", len(searchResults))
@@ -1166,6 +1170,7 @@ func (s *knowledgeBaseService) buildSearchResult(chunk *types.Chunk,
knowledge *types.Knowledge,
score float64,
matchType types.MatchType,
matchedContent string,
) *types.SearchResult {
return &types.SearchResult{
ID: chunk.ID,
@@ -1185,6 +1190,7 @@ func (s *knowledgeBaseService) buildSearchResult(chunk *types.Chunk,
KnowledgeFilename: knowledge.FileName,
KnowledgeSource: knowledge.Source,
ChunkMetadata: chunk.Metadata,
MatchedContent: matchedContent,
}
}