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.
This commit is contained in:
wizardchen
2026-03-25 11:04:43 +08:00
committed by lyingbug
parent ac04a8efcb
commit ae89871ac5
2 changed files with 38 additions and 21 deletions
+37 -21
View File
@@ -90,7 +90,7 @@ func (r *knowledgeRepository) ListPagedKnowledgeByKnowledgeBaseID(
query = query.Where("tag_id = ?", tagID) query = query.Where("tag_id = ?", tagID)
} }
if keyword != "" { if keyword != "" {
query = query.Where("file_name LIKE ?", "%"+keyword+"%") query = query.Where("(file_name LIKE ? OR title LIKE ?)", "%"+keyword+"%", "%"+keyword+"%")
} }
if fileType != "" { if fileType != "" {
if fileType == "manual" { if fileType == "manual" {
@@ -114,7 +114,7 @@ func (r *knowledgeRepository) ListPagedKnowledgeByKnowledgeBaseID(
dataQuery = dataQuery.Where("tag_id = ?", tagID) dataQuery = dataQuery.Where("tag_id = ?", tagID)
} }
if keyword != "" { if keyword != "" {
dataQuery = dataQuery.Where("file_name LIKE ?", "%"+keyword+"%") dataQuery = dataQuery.Where("(file_name LIKE ? OR title LIKE ?)", "%"+keyword+"%", "%"+keyword+"%")
} }
if fileType != "" { if fileType != "" {
if fileType == "manual" { if fileType == "manual" {
@@ -337,16 +337,20 @@ func (r *knowledgeRepository) SearchKnowledge(
// If keyword is provided, filter by file_name or title // If keyword is provided, filter by file_name or title
if keyword != "" { 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 { if len(fileTypes) > 0 {
// Build file extension patterns (e.g., "%.csv", "%.xlsx")
seen := make(map[string]bool) seen := make(map[string]bool)
var uniquePatterns []string var uniquePatterns []string
includeURL := false
for _, ft := range fileTypes { for _, ft := range fileTypes {
ft = strings.ToLower(strings.TrimPrefix(ft, ".")) ft = strings.ToLower(strings.TrimPrefix(ft, "."))
if ft == "url" || ft == "html" {
includeURL = true
continue
}
pattern := "%." + ft pattern := "%." + ft
if !seen[pattern] { if !seen[pattern] {
seen[pattern] = true seen[pattern] = true
@@ -377,14 +381,17 @@ func (r *knowledgeRepository) SearchKnowledge(
} }
} }
} }
// Build OR conditions for file extensions var orConditions []string
if len(uniquePatterns) > 0 { var args []interface{}
orConditions := make([]string, len(uniquePatterns)) for _, p := range uniquePatterns {
args := make([]interface{}, len(uniquePatterns)) orConditions = append(orConditions, "LOWER(knowledges.file_name) LIKE ?")
for i, p := range uniquePatterns { args = append(args, p)
orConditions[i] = "LOWER(knowledges.file_name) LIKE ?" }
args[i] = p if includeURL {
} orConditions = append(orConditions, "knowledges.type = ?")
args = append(args, "url")
}
if len(orConditions) > 0 {
query = query.Where("("+strings.Join(orConditions, " OR ")+")", args...) query = query.Where("("+strings.Join(orConditions, " OR ")+")", args...)
} }
} }
@@ -448,14 +455,19 @@ func (r *knowledgeRepository) SearchKnowledgeInScopes(
Where("knowledges.deleted_at IS NULL") Where("knowledges.deleted_at IS NULL")
if keyword != "" { 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 { if len(fileTypes) > 0 {
seen := make(map[string]bool) seen := make(map[string]bool)
var uniquePatterns []string var uniquePatterns []string
includeURL := false
for _, ft := range fileTypes { for _, ft := range fileTypes {
ft = strings.ToLower(strings.TrimPrefix(ft, ".")) ft = strings.ToLower(strings.TrimPrefix(ft, "."))
if ft == "url" || ft == "html" {
includeURL = true
continue
}
pattern := "%." + ft pattern := "%." + ft
if !seen[pattern] { if !seen[pattern] {
seen[pattern] = true seen[pattern] = true
@@ -485,13 +497,17 @@ func (r *knowledgeRepository) SearchKnowledgeInScopes(
} }
} }
} }
if len(uniquePatterns) > 0 { var orConditions []string
orConditions := make([]string, len(uniquePatterns)) var ftArgs []interface{}
ftArgs := make([]interface{}, len(uniquePatterns)) for _, p := range uniquePatterns {
for i, p := range uniquePatterns { orConditions = append(orConditions, "LOWER(knowledges.file_name) LIKE ?")
orConditions[i] = "LOWER(knowledges.file_name) LIKE ?" ftArgs = append(ftArgs, p)
ftArgs[i] = p }
} if includeURL {
orConditions = append(orConditions, "knowledges.type = ?")
ftArgs = append(ftArgs, "url")
}
if len(orConditions) > 0 {
query = query.Where("("+strings.Join(orConditions, " OR ")+")", ftArgs...) query = query.Where("("+strings.Join(orConditions, " OR ")+")", ftArgs...)
} }
} }
@@ -521,6 +521,7 @@ func (s *knowledgeService) CreateKnowledgeFromURL(ctx context.Context,
Channel: defaultChannel(channel), Channel: defaultChannel(channel),
Title: title, Title: title,
Source: url, Source: url,
FileType: "html",
FileHash: fileHash, FileHash: fileHash,
ParseStatus: "pending", ParseStatus: "pending",
EnableStatus: "disabled", EnableStatus: "disabled",