From ae89871ac5d85d21871f074df3833f184dc07b68 Mon Sep 17 00:00:00 2001 From: wizardchen Date: Wed, 25 Mar 2026 11:04:43 +0800 Subject: [PATCH] feat(knowledge): enhance search functionality to include title filtering and URL handling - Updated search queries to filter by both file name and title when a keyword is provided, improving search accuracy. - Added support for filtering by URL and HTML file types in search queries, enhancing the flexibility of the search functionality. - Refactored query construction for file types to streamline the inclusion of additional conditions, ensuring robust handling of various file types. --- internal/application/repository/knowledge.go | 58 +++++++++++++------- internal/application/service/knowledge.go | 1 + 2 files changed, 38 insertions(+), 21 deletions(-) diff --git a/internal/application/repository/knowledge.go b/internal/application/repository/knowledge.go index f88a1708..151b5dd7 100644 --- a/internal/application/repository/knowledge.go +++ b/internal/application/repository/knowledge.go @@ -90,7 +90,7 @@ func (r *knowledgeRepository) ListPagedKnowledgeByKnowledgeBaseID( query = query.Where("tag_id = ?", tagID) } if keyword != "" { - query = query.Where("file_name LIKE ?", "%"+keyword+"%") + query = query.Where("(file_name LIKE ? OR title LIKE ?)", "%"+keyword+"%", "%"+keyword+"%") } if fileType != "" { if fileType == "manual" { @@ -114,7 +114,7 @@ func (r *knowledgeRepository) ListPagedKnowledgeByKnowledgeBaseID( dataQuery = dataQuery.Where("tag_id = ?", tagID) } if keyword != "" { - dataQuery = dataQuery.Where("file_name LIKE ?", "%"+keyword+"%") + dataQuery = dataQuery.Where("(file_name LIKE ? OR title LIKE ?)", "%"+keyword+"%", "%"+keyword+"%") } if fileType != "" { if fileType == "manual" { @@ -337,16 +337,20 @@ func (r *knowledgeRepository) SearchKnowledge( // If keyword is provided, filter by file_name or title if keyword != "" { - query = query.Where("knowledges.file_name LIKE ? ", "%"+keyword+"%") + query = query.Where("(knowledges.file_name LIKE ? OR knowledges.title LIKE ?)", "%"+keyword+"%", "%"+keyword+"%") } - // If fileTypes is provided, filter by file extension + // If fileTypes is provided, filter by file extension or type if len(fileTypes) > 0 { - // Build file extension patterns (e.g., "%.csv", "%.xlsx") seen := make(map[string]bool) var uniquePatterns []string + includeURL := false for _, ft := range fileTypes { ft = strings.ToLower(strings.TrimPrefix(ft, ".")) + if ft == "url" || ft == "html" { + includeURL = true + continue + } pattern := "%." + ft if !seen[pattern] { seen[pattern] = true @@ -377,14 +381,17 @@ func (r *knowledgeRepository) SearchKnowledge( } } } - // Build OR conditions for file extensions - if len(uniquePatterns) > 0 { - orConditions := make([]string, len(uniquePatterns)) - args := make([]interface{}, len(uniquePatterns)) - for i, p := range uniquePatterns { - orConditions[i] = "LOWER(knowledges.file_name) LIKE ?" - args[i] = p - } + var orConditions []string + var args []interface{} + for _, p := range uniquePatterns { + orConditions = append(orConditions, "LOWER(knowledges.file_name) LIKE ?") + args = append(args, p) + } + if includeURL { + orConditions = append(orConditions, "knowledges.type = ?") + args = append(args, "url") + } + if len(orConditions) > 0 { query = query.Where("("+strings.Join(orConditions, " OR ")+")", args...) } } @@ -448,14 +455,19 @@ func (r *knowledgeRepository) SearchKnowledgeInScopes( Where("knowledges.deleted_at IS NULL") if keyword != "" { - query = query.Where("knowledges.file_name LIKE ?", "%"+keyword+"%") + query = query.Where("(knowledges.file_name LIKE ? OR knowledges.title LIKE ?)", "%"+keyword+"%", "%"+keyword+"%") } if len(fileTypes) > 0 { seen := make(map[string]bool) var uniquePatterns []string + includeURL := false for _, ft := range fileTypes { ft = strings.ToLower(strings.TrimPrefix(ft, ".")) + if ft == "url" || ft == "html" { + includeURL = true + continue + } pattern := "%." + ft if !seen[pattern] { seen[pattern] = true @@ -485,13 +497,17 @@ func (r *knowledgeRepository) SearchKnowledgeInScopes( } } } - if len(uniquePatterns) > 0 { - orConditions := make([]string, len(uniquePatterns)) - ftArgs := make([]interface{}, len(uniquePatterns)) - for i, p := range uniquePatterns { - orConditions[i] = "LOWER(knowledges.file_name) LIKE ?" - ftArgs[i] = p - } + var orConditions []string + var ftArgs []interface{} + for _, p := range uniquePatterns { + orConditions = append(orConditions, "LOWER(knowledges.file_name) LIKE ?") + ftArgs = append(ftArgs, p) + } + if includeURL { + orConditions = append(orConditions, "knowledges.type = ?") + ftArgs = append(ftArgs, "url") + } + if len(orConditions) > 0 { query = query.Where("("+strings.Join(orConditions, " OR ")+")", ftArgs...) } } diff --git a/internal/application/service/knowledge.go b/internal/application/service/knowledge.go index 191b8a49..3a6b8947 100644 --- a/internal/application/service/knowledge.go +++ b/internal/application/service/knowledge.go @@ -521,6 +521,7 @@ func (s *knowledgeService) CreateKnowledgeFromURL(ctx context.Context, Channel: defaultChannel(channel), Title: title, Source: url, + FileType: "html", FileHash: fileHash, ParseStatus: "pending", EnableStatus: "disabled",