feat: add matched question field to FAQ search results
This commit is contained in:
@@ -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,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -194,6 +194,9 @@ type FAQEntry struct {
|
||||
Score float64 `json:"score,omitempty"`
|
||||
MatchType MatchType `json:"match_type,omitempty"`
|
||||
ChunkType ChunkType `json:"chunk_type"`
|
||||
// MatchedQuestion is the actual question text that was matched in FAQ search
|
||||
// Could be the standard question or one of the similar questions
|
||||
MatchedQuestion string `json:"matched_question,omitempty"`
|
||||
}
|
||||
|
||||
// FAQEntryPayload 用于创建/更新 FAQ 条目的 payload
|
||||
|
||||
@@ -87,6 +87,10 @@ type SearchResult struct {
|
||||
|
||||
// ChunkMetadata stores chunk-level metadata (e.g., generated questions)
|
||||
ChunkMetadata JSON `json:"chunk_metadata,omitempty"`
|
||||
|
||||
// MatchedContent is the actual content that was matched in vector search
|
||||
// For FAQ: this is the matched question text (standard or similar question)
|
||||
MatchedContent string `json:"matched_content,omitempty"`
|
||||
}
|
||||
|
||||
// SearchParams represents the search parameters
|
||||
|
||||
Reference in New Issue
Block a user