fix(server): case-insensitive doc keyword search + not-found classification

Make document keyword search case-insensitive (LOWER(...) LIKE LOWER(?)) in the
list filter and SearchKnowledge / SearchKnowledgeInScopes paths so a query and
its differently-cased variant return the same results.

Classify not-found consistently as HTTP 404 (not 500) so clients read a
permanent resource.not_found instead of a retryable 5xx: map the document /
chunk not-found sentinels in the KB-access middleware (typed ErrChunkNotFound
via errors.Is), and return 404 for a missing message on the continue-stream and
message-delete endpoints.
This commit is contained in:
nullkey
2026-06-26 20:40:59 +08:00
committed by lyingbug
parent 8f36ea2229
commit 445a4330fa
8 changed files with 77 additions and 20 deletions
+10 -3
View File
@@ -14,6 +14,13 @@ import (
"gorm.io/gorm"
)
// ErrChunkNotFound is returned when a chunk lookup finds no row. A typed
// sentinel (matching the ErrXNotFound convention used by the other repos)
// so callers can errors.Is it safely through wrapping — replacing the
// previous bare errors.New("chunk not found") that forced fragile
// string-equality matching in the service layer.
var ErrChunkNotFound = errors.New("chunk not found")
// chunkRepository implements the ChunkRepository interface
type chunkRepository struct {
db *gorm.DB
@@ -58,7 +65,7 @@ func (r *chunkRepository) GetChunkByID(ctx context.Context, tenantID uint64, id
var chunk types.Chunk
if err := r.db.WithContext(ctx).Where("tenant_id = ? AND id = ?", tenantID, id).First(&chunk).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, errors.New("chunk not found")
return nil, ErrChunkNotFound
}
return nil, err
}
@@ -70,7 +77,7 @@ func (r *chunkRepository) GetChunkByIDOnly(ctx context.Context, id string) (*typ
var chunk types.Chunk
if err := r.db.WithContext(ctx).Where("id = ?", id).First(&chunk).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, errors.New("chunk not found")
return nil, ErrChunkNotFound
}
return nil, err
}
@@ -82,7 +89,7 @@ func (r *chunkRepository) GetChunkBySeqID(ctx context.Context, tenantID uint64,
var chunk types.Chunk
if err := r.db.WithContext(ctx).Where("tenant_id = ? AND seq_id = ?", tenantID, seqID).First(&chunk).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, errors.New("chunk not found")
return nil, ErrChunkNotFound
}
return nil, err
}
+12 -7
View File
@@ -105,8 +105,13 @@ func applyKnowledgeListFilter(query *gorm.DB, filter types.KnowledgeListFilter)
)
}
if filter.Keyword != "" {
escaped := escapeLikeKeyword(filter.Keyword)
query = query.Where("(file_name LIKE ? OR title LIKE ?)", "%"+escaped+"%", "%"+escaped+"%")
// Case-insensitive (LOWER … LIKE LOWER) so keyword search matches
// regardless of the stored casing — consistent with the sibling
// LOWER() filters in this file and with the client-side `search kb`
// / `search sessions` filters. Plain LIKE is case-sensitive in
// Postgres, which surprised callers searching with lowercase.
escaped := strings.ToLower(escapeLikeKeyword(filter.Keyword))
query = query.Where("(LOWER(file_name) LIKE ? OR LOWER(title) LIKE ?)", "%"+escaped+"%", "%"+escaped+"%")
}
// FileType and Source share the same special-case routing onto `type` for
// the "manual" / "url" values, so callers can pick either control.
@@ -530,10 +535,10 @@ func (r *knowledgeRepository) SearchKnowledge(
Where("knowledge_bases.type = ?", types.KnowledgeBaseTypeDocument).
Where("knowledges.deleted_at IS NULL")
// If keyword is provided, filter by file_name or title
// If keyword is provided, filter by file_name or title (case-insensitive).
if keyword != "" {
escaped := escapeLikeKeyword(keyword)
query = query.Where("(knowledges.file_name LIKE ? OR knowledges.title LIKE ?)", "%"+escaped+"%", "%"+escaped+"%")
escaped := strings.ToLower(escapeLikeKeyword(keyword))
query = query.Where("(LOWER(knowledges.file_name) LIKE ? OR LOWER(knowledges.title) LIKE ?)", "%"+escaped+"%", "%"+escaped+"%")
}
// If fileTypes is provided, filter by file extension or type
@@ -651,8 +656,8 @@ func (r *knowledgeRepository) SearchKnowledgeInScopes(
Where("knowledges.deleted_at IS NULL")
if keyword != "" {
escaped := escapeLikeKeyword(keyword)
query = query.Where("(knowledges.file_name LIKE ? OR knowledges.title LIKE ?)", "%"+escaped+"%", "%"+escaped+"%")
escaped := strings.ToLower(escapeLikeKeyword(keyword))
query = query.Where("(LOWER(knowledges.file_name) LIKE ? OR LOWER(knowledges.title) LIKE ?)", "%"+escaped+"%", "%"+escaped+"%")
}
if len(fileTypes) > 0 {
+4 -1
View File
@@ -5,6 +5,7 @@ package service
import (
"context"
"errors"
"fmt"
"github.com/Tencent/WeKnora/internal/application/service/retriever"
@@ -107,7 +108,9 @@ func (s *chunkService) GetChunkByID(ctx context.Context, id string) (*types.Chun
func (s *chunkService) GetChunkByIDOnly(ctx context.Context, id string) (*types.Chunk, error) {
chunk, err := s.chunkRepository.GetChunkByIDOnly(ctx, id)
if err != nil {
if err != nil && err.Error() == "chunk not found" {
// errors.Is (not string equality) so the sentinel survives wrapping.
// ErrChunkNotFound aliases the repo sentinel, so this matches directly.
if errors.Is(err, ErrChunkNotFound) {
return nil, ErrChunkNotFound
}
logger.ErrorWithFields(ctx, err, map[string]interface{}{"chunk_id": id})
+5 -2
View File
@@ -26,8 +26,11 @@ var (
ErrInvalidFileType = errors.New("unsupported file type")
// ErrInvalidURL is returned when an invalid URL is provided
ErrInvalidURL = errors.New("invalid URL")
// ErrChunkNotFound is returned when a requested chunk cannot be found
ErrChunkNotFound = errors.New("chunk not found")
// ErrChunkNotFound is returned when a requested chunk cannot be found.
// Aliases the repository sentinel so a chunk-not-found from the repo
// errors.Is-matches at the service and middleware layers (a single
// identity instead of two string-equal-but-distinct errors).
ErrChunkNotFound = repository.ErrChunkNotFound
// ErrDuplicateFile is returned when trying to add a file that already exists
ErrDuplicateFile = errors.New("file already exists")
// ErrDuplicateURL is returned when trying to add a URL that already exists