feat: 优化知识块分页查询功能,支持按知识类型区分搜索和排序逻辑

This commit is contained in:
wizardchen
2025-12-26 12:30:06 +08:00
parent c9a6c3134a
commit 3905b6fe5d
8 changed files with 37 additions and 269 deletions
+23 -4
View File
@@ -82,6 +82,7 @@ func (r *chunkRepository) ListPagedChunksByKnowledgeID(
keyword string,
searchField string,
sortOrder string,
knowledgeType string,
) ([]*types.Chunk, int64, error) {
var chunks []*types.Chunk
var total int64
@@ -98,6 +99,14 @@ func (r *chunkRepository) ListPagedChunksByKnowledgeID(
}
if keyword != "" {
like := "%" + keyword + "%"
// Document type: search content only
if knowledgeType != types.KnowledgeTypeFAQ {
db = db.Where("content LIKE ?", like)
return db
}
// FAQ type: search based on searchField
// 根据数据库类型使用不同的 JSON 查询语法
isPostgres := db.Dialector.Name() == "postgres"
@@ -147,10 +156,20 @@ func (r *chunkRepository) ListPagedChunksByKnowledgeID(
// Then query the paginated data
dataQuery := baseFilter(r.db.WithContext(ctx))
// Default is time descending, "asc" for time ascending
orderClause := "updated_at DESC"
if sortOrder == "asc" {
orderClause = "updated_at ASC"
// Determine sort order based on knowledge type
var orderClause string
if knowledgeType == types.KnowledgeTypeFAQ {
// FAQ: sort by updated_at
orderClause = "updated_at DESC"
if sortOrder == "asc" {
orderClause = "updated_at ASC"
}
} else {
// Document: sort by chunk_index
orderClause = "chunk_index ASC"
if sortOrder == "desc" {
orderClause = "chunk_index DESC"
}
}
if err := dataQuery.
+1
View File
@@ -153,6 +153,7 @@ func (s *chunkService) ListPagedChunksByKnowledgeID(ctx context.Context,
"",
"",
"",
"",
)
if err != nil {
logger.ErrorWithFields(ctx, err, map[string]interface{}{
+2 -1
View File
@@ -2550,6 +2550,7 @@ func (s *knowledgeService) CloneChunk(ctx context.Context, src, dst *types.Knowl
"",
"",
"",
"",
)
chunkPage++
if err != nil {
@@ -2664,7 +2665,7 @@ func (s *knowledgeService) ListFAQEntries(ctx context.Context,
}
chunkType := []types.ChunkType{types.ChunkTypeFAQ}
chunks, total, err := s.chunkRepo.ListPagedChunksByKnowledgeID(
ctx, tenantID, faqKnowledge.ID, page, chunkType, tagID, keyword, searchField, sortOrder,
ctx, tenantID, faqKnowledge.ID, page, chunkType, tagID, keyword, searchField, sortOrder, types.KnowledgeTypeFAQ,
)
if err != nil {
return nil, err