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:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user