fix(repository): 关键词 LIKE/ILIKE 检索补显式 ESCAPE,修复 Lite 上静默漏结果
SQLite(Lite 版所用数据库)没有默认的 LIKE 转义字符,而 Postgres 的默认转义字符是 反斜杠。escapeLikeKeyword / escapeLikePattern 会把关键词里的 %、_、\ 转义成 \%、\_, 但只有在 SQL 中显式声明 ESCAPE 时才有意义:缺少 ESCAPE 时 SQLite 会把反斜杠当成普通 字符,于是任何含 _ 或 % 的关键词(如 IMG_2534、100%)都匹配 0 行——不报错、不打日志, total 同样为 0,前端只显示"暂无数据"。Postgres 因默认反斜杠转义而表现正常,所以问题 只在 Lite / SQLite 上静默出现。 本次修改统一在 LIKE 表达式后补 ESCAPE,并把 escape 字符作为参数传入(Postgres 的 ILIKE 分支同样处理),写法对齐 message.go / message_artifact.go / chunk_image_assets.go 中已有的正确站点,转义字符复用 knowledge.go 既有的 likeEscapeChar 常量。 修复站点: - knowledge.go applyKnowledgeListFilter 文档列表关键词过滤 (agent 工具 list_documents、mcp-server tools_retrieve 复用) - knowledge.go SearchKnowledge - knowledge.go SearchKnowledgeInScopes(GET /api/v1/knowledge/search) - tag.go ListByKB 标签搜索(count 与 data 两条查询) - tenant.go SearchTenants 租户搜索(带/不带 tenantID 两个分支) - tenant_member.go CountFilteredByTenant / ListPagedByTenant 成员邮箱、用户名搜索 - session.go QueryPaged 会话列表/侧栏标题搜索(LOWER LIKE 与 Postgres ILIKE 分支) - chunk.go ListPagedChunksByKnowledgeID 分块/FAQ 搜索 chunk.go 属反向错误:关键词完全没有转义,% 和 _ 被当作通配符导致多匹配。这里同样改为 escapeLikeKeyword + ESCAPE,而不是把别处的转义去掉——关键词里的 % 和 _ 是用户想按字面 匹配的字符(文档名 IMG_2534、进度 100% 都很常见),去掉转义会让它们又变成通配符、匹配 到无关结果;只有显式 ESCAPE 才能让 Postgres 与 SQLite 行为一致。 顺带把 escapeLikePattern 收敛为 escapeLikeKeyword 的薄封装(wiki_page.go 一处), 两者原本是逐字符等价的重复实现,调用点语义不变。转义字符常量仍复用 likeEscapeChar, 未新增常量。 新增 SQLite 回归测试 internal/application/repository/like_escape_sqlite_test.go,覆盖上述 全部站点:每个用例同时埋入"字面命中行"和"通配符诱饵行",缺 ESCAPE 会因 0 行失败,缺 转义会因多匹配失败。已逐站点反向验证:去掉 ESCAPE(或去掉 chunk.go 的转义)后测试确实 失败,随后逐字节还原。 注:organization.go ListSearchable 与 user.go SearchUsers 的关键词同样未转义,但它们是 Postgres 专用路径(ILIKE / ::text),属另一类问题,不在本次改动范围内。main
parent
436d02fdf3
commit
e39043f34b
|
|
@ -224,11 +224,11 @@ func (r *chunkRepository) ListPagedChunksByKnowledgeID(
|
|||
db = db.Where("is_enabled = ?", *isEnabled)
|
||||
}
|
||||
if keyword != "" {
|
||||
like := "%" + keyword + "%"
|
||||
like := "%" + escapeLikeKeyword(keyword) + "%"
|
||||
|
||||
// Document type: search content only
|
||||
if knowledgeType != types.KnowledgeTypeFAQ {
|
||||
db = db.Where("content LIKE ?", like)
|
||||
db = db.Where("content LIKE ? ESCAPE ?", like, likeEscapeChar)
|
||||
return db
|
||||
}
|
||||
|
||||
|
|
@ -240,32 +240,35 @@ func (r *chunkRepository) ListPagedChunksByKnowledgeID(
|
|||
case "standard_question":
|
||||
// Search only in standard_question field of metadata
|
||||
if isPostgres {
|
||||
db = db.Where("metadata->>'standard_question' ILIKE ?", like)
|
||||
db = db.Where("metadata->>'standard_question' ILIKE ? ESCAPE ?", like, likeEscapeChar)
|
||||
} else {
|
||||
// MySQL: metadata->>'$.standard_question' (MySQL 5.7.13+)
|
||||
// 也可以用 JSON_UNQUOTE(JSON_EXTRACT(metadata, '$.standard_question'))
|
||||
db = db.Where("metadata->>'$.standard_question' LIKE ?", like)
|
||||
db = db.Where("metadata->>'$.standard_question' LIKE ? ESCAPE ?", like, likeEscapeChar)
|
||||
}
|
||||
case "similar_questions":
|
||||
// Search in similar_questions array of metadata
|
||||
if isPostgres {
|
||||
db = db.Where("(metadata->'similar_questions')::text ILIKE ?", like)
|
||||
db = db.Where("(metadata->'similar_questions')::text ILIKE ? ESCAPE ?", like, likeEscapeChar)
|
||||
} else {
|
||||
db = db.Where("JSON_EXTRACT(metadata, '$.similar_questions') LIKE ?", like)
|
||||
db = db.Where("JSON_EXTRACT(metadata, '$.similar_questions') LIKE ? ESCAPE ?",
|
||||
like, likeEscapeChar)
|
||||
}
|
||||
case "answers":
|
||||
// Search in answers array of metadata
|
||||
if isPostgres {
|
||||
db = db.Where("(metadata->'answers')::text ILIKE ?", like)
|
||||
db = db.Where("(metadata->'answers')::text ILIKE ? ESCAPE ?", like, likeEscapeChar)
|
||||
} else {
|
||||
db = db.Where("JSON_EXTRACT(metadata, '$.answers') LIKE ?", like)
|
||||
db = db.Where("JSON_EXTRACT(metadata, '$.answers') LIKE ? ESCAPE ?", like, likeEscapeChar)
|
||||
}
|
||||
default:
|
||||
// Search in all fields (content and metadata)
|
||||
if isPostgres {
|
||||
db = db.Where("(content ILIKE ? OR metadata::text ILIKE ?)", like, like)
|
||||
db = db.Where("(content ILIKE ? ESCAPE ? OR metadata::text ILIKE ? ESCAPE ?)",
|
||||
like, likeEscapeChar, like, likeEscapeChar)
|
||||
} else {
|
||||
db = db.Where("(content LIKE ? OR CAST(metadata AS CHAR) LIKE ?)", like, like)
|
||||
db = db.Where("(content LIKE ? ESCAPE ? OR CAST(metadata AS CHAR) LIKE ? ESCAPE ?)",
|
||||
like, likeEscapeChar, like, likeEscapeChar)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -139,7 +139,10 @@ func applyKnowledgeListFilter(query *gorm.DB, filter types.KnowledgeListFilter)
|
|||
// / `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+"%")
|
||||
query = query.Where(
|
||||
"(LOWER(file_name) LIKE ? ESCAPE ? OR LOWER(title) LIKE ? ESCAPE ?)",
|
||||
"%"+escaped+"%", likeEscapeChar, "%"+escaped+"%", likeEscapeChar,
|
||||
)
|
||||
}
|
||||
// FileType and Source share the same special-case routing onto `type` for
|
||||
// the "manual" / "url" values, so callers can pick either control.
|
||||
|
|
@ -958,7 +961,10 @@ func (r *knowledgeRepository) SearchKnowledge(
|
|||
// If keyword is provided, filter by file_name or title (case-insensitive).
|
||||
if keyword != "" {
|
||||
escaped := strings.ToLower(escapeLikeKeyword(keyword))
|
||||
query = query.Where("(LOWER(knowledges.file_name) LIKE ? OR LOWER(knowledges.title) LIKE ?)", "%"+escaped+"%", "%"+escaped+"%")
|
||||
query = query.Where(
|
||||
"(LOWER(knowledges.file_name) LIKE ? ESCAPE ? OR LOWER(knowledges.title) LIKE ? ESCAPE ?)",
|
||||
"%"+escaped+"%", likeEscapeChar, "%"+escaped+"%", likeEscapeChar,
|
||||
)
|
||||
}
|
||||
|
||||
// If fileTypes is provided, filter by file extension or type
|
||||
|
|
@ -1077,7 +1083,10 @@ func (r *knowledgeRepository) SearchKnowledgeInScopes(
|
|||
|
||||
if keyword != "" {
|
||||
escaped := strings.ToLower(escapeLikeKeyword(keyword))
|
||||
query = query.Where("(LOWER(knowledges.file_name) LIKE ? OR LOWER(knowledges.title) LIKE ?)", "%"+escaped+"%", "%"+escaped+"%")
|
||||
query = query.Where(
|
||||
"(LOWER(knowledges.file_name) LIKE ? ESCAPE ? OR LOWER(knowledges.title) LIKE ? ESCAPE ?)",
|
||||
"%"+escaped+"%", likeEscapeChar, "%"+escaped+"%", likeEscapeChar,
|
||||
)
|
||||
}
|
||||
|
||||
if len(fileTypes) > 0 {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,306 @@
|
|||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
"github.com/google/uuid"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// SQLite — the database behind the Lite build — has no default LIKE escape
|
||||
// character, while Postgres defaults to backslash. escapeLikeKeyword /
|
||||
// escapeLikePattern emit \% and \_ on the assumption that the pattern is
|
||||
// consumed by a predicate with an explicit ESCAPE clause; without one, SQLite
|
||||
// searches for a literal backslash and silently returns zero rows (and a zero
|
||||
// total) for any keyword containing %, _ or \.
|
||||
//
|
||||
// These tests pin that invariant at each fixed call site. Each case seeds a
|
||||
// row whose name/content contains the literal character plus a decoy row that
|
||||
// the same keyword would match if the character were still a wildcard, so a
|
||||
// missing ESCAPE fails on "no rows" and a missing escape fails on "extra row".
|
||||
|
||||
// likeEscapeTestDDL creates the join targets of the keyword searches that are
|
||||
// not part of the AutoMigrate set below.
|
||||
const likeEscapeTestDDL = `
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id VARCHAR(36) PRIMARY KEY,
|
||||
username VARCHAR(100) NOT NULL DEFAULT '',
|
||||
email VARCHAR(255) NOT NULL DEFAULT '',
|
||||
deleted_at DATETIME
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS im_channel_sessions (
|
||||
session_id VARCHAR(36),
|
||||
platform VARCHAR(32),
|
||||
chat_id VARCHAR(255),
|
||||
thread_id VARCHAR(255),
|
||||
user_id VARCHAR(64),
|
||||
agent_id VARCHAR(64),
|
||||
im_channel_id VARCHAR(64)
|
||||
);
|
||||
`
|
||||
|
||||
func setupLikeEscapeTestDB(t *testing.T) *gorm.DB {
|
||||
t.Helper()
|
||||
dsn := "file:" + uuid.New().String() + "?mode=memory&cache=shared&_busy_timeout=5000"
|
||||
db, err := gorm.Open(sqlite.Open(dsn), &gorm.Config{})
|
||||
require.NoError(t, err)
|
||||
sqlDB, err := db.DB()
|
||||
require.NoError(t, err)
|
||||
sqlDB.SetMaxOpenConns(1)
|
||||
t.Cleanup(func() { _ = sqlDB.Close() })
|
||||
|
||||
// The knowledges / knowledge_bases schemas are inlined by the existing
|
||||
// helpers (AutoMigrate does not map their jsonb columns onto SQLite).
|
||||
require.NoError(t, db.Exec(knowledgesTestDDL).Error)
|
||||
require.NoError(t, db.Exec(knowledgeBasesTestDDL).Error)
|
||||
require.NoError(t, db.Exec(likeEscapeTestDDL).Error)
|
||||
require.NoError(t, db.AutoMigrate(
|
||||
&types.KnowledgeTag{},
|
||||
&types.Chunk{},
|
||||
&types.Session{},
|
||||
&types.Tenant{},
|
||||
&types.TenantMember{},
|
||||
))
|
||||
return db
|
||||
}
|
||||
|
||||
func seedLikeEscapeKnowledgeBase(t *testing.T, db *gorm.DB, tenantID uint64, kbID string) {
|
||||
t.Helper()
|
||||
require.NoError(t, db.Exec(`
|
||||
INSERT INTO knowledge_bases (id, name, tenant_id, type, embedding_model_id, summary_model_id)
|
||||
VALUES (?, 'like-escape-kb', ?, 'document', 'test-embedding', 'test-summary')
|
||||
`, kbID, tenantID).Error)
|
||||
}
|
||||
|
||||
func seedLikeEscapeKnowledge(
|
||||
t *testing.T, db *gorm.DB, tenantID uint64, kbID, id, fileName, title string,
|
||||
) {
|
||||
t.Helper()
|
||||
require.NoError(t, db.Exec(`
|
||||
INSERT INTO knowledges (id, tenant_id, knowledge_base_id, type, title, file_name, source, parse_status)
|
||||
VALUES (?, ?, ?, 'file', ?, ?, 'manual', 'completed')
|
||||
`, id, tenantID, kbID, title, fileName).Error)
|
||||
}
|
||||
|
||||
func seedLikeEscapeKnowledgeSet(t *testing.T, db *gorm.DB, tenantID uint64, kbID string) (literalID string) {
|
||||
t.Helper()
|
||||
literalID = uuid.New().String()
|
||||
// Underscore: literal "IMG_2534" must not match "IMGx2534".
|
||||
seedLikeEscapeKnowledge(t, db, tenantID, kbID, literalID, "IMG_2534.png", "IMG_2534")
|
||||
seedLikeEscapeKnowledge(t, db, tenantID, kbID, uuid.New().String(), "IMGx2534.png", "IMGx2534")
|
||||
// Percent: literal "100%" must not match "1000".
|
||||
seedLikeEscapeKnowledge(t, db, tenantID, kbID, uuid.New().String(), "100% done.pdf", "100% done")
|
||||
seedLikeEscapeKnowledge(t, db, tenantID, kbID, uuid.New().String(), "1000 done.pdf", "1000 done")
|
||||
return literalID
|
||||
}
|
||||
|
||||
// Covers the document-list keyword filter in applyKnowledgeListFilter
|
||||
// (reused by the agent list_documents tool and the MCP retrieve tool).
|
||||
func TestListPagedKnowledgeKeywordTreatsLikeWildcardsLiterally(t *testing.T) {
|
||||
db := setupLikeEscapeTestDB(t)
|
||||
repo := NewKnowledgeRepository(db).(*knowledgeRepository)
|
||||
ctx := context.Background()
|
||||
const tenantID = uint64(1)
|
||||
kbID := uuid.New().String()
|
||||
seedLikeEscapeKnowledgeBase(t, db, tenantID, kbID)
|
||||
literalID := seedLikeEscapeKnowledgeSet(t, db, tenantID, kbID)
|
||||
page := &types.Pagination{Page: 1, PageSize: 50}
|
||||
|
||||
for _, keyword := range []string{"IMG_2534", "100%"} {
|
||||
rows, total, err := repo.ListPagedKnowledgeByKnowledgeBaseID(
|
||||
ctx, tenantID, kbID, page, types.KnowledgeListFilter{Keyword: keyword},
|
||||
)
|
||||
require.NoError(t, err, "keyword %q", keyword)
|
||||
require.Len(t, rows, 1, "keyword %q must match the literal row only", keyword)
|
||||
assert.Equal(t, int64(1), total, "keyword %q count", keyword)
|
||||
if keyword == "IMG_2534" {
|
||||
assert.Equal(t, literalID, rows[0].ID)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Covers SearchKnowledge.
|
||||
func TestSearchKnowledgeKeywordTreatsLikeWildcardsLiterally(t *testing.T) {
|
||||
db := setupLikeEscapeTestDB(t)
|
||||
repo := NewKnowledgeRepository(db).(*knowledgeRepository)
|
||||
ctx := context.Background()
|
||||
const tenantID = uint64(1)
|
||||
kbID := uuid.New().String()
|
||||
seedLikeEscapeKnowledgeBase(t, db, tenantID, kbID)
|
||||
literalID := seedLikeEscapeKnowledgeSet(t, db, tenantID, kbID)
|
||||
|
||||
rows, _, err := repo.SearchKnowledge(ctx, tenantID, "IMG_2534", 0, 10, nil)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, rows, 1, "underscore must match literally, not as a wildcard")
|
||||
assert.Equal(t, literalID, rows[0].ID)
|
||||
|
||||
rows, _, err = repo.SearchKnowledge(ctx, tenantID, "100%", 0, 10, nil)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, rows, 1, "percent must match literally, not as a wildcard")
|
||||
assert.Contains(t, rows[0].Title, "100%")
|
||||
}
|
||||
|
||||
// Covers SearchKnowledgeInScopes (GET /api/v1/knowledge/search).
|
||||
func TestSearchKnowledgeInScopesKeywordTreatsLikeWildcardsLiterally(t *testing.T) {
|
||||
db := setupLikeEscapeTestDB(t)
|
||||
repo := NewKnowledgeRepository(db).(*knowledgeRepository)
|
||||
ctx := context.Background()
|
||||
const tenantID = uint64(1)
|
||||
kbID := uuid.New().String()
|
||||
seedLikeEscapeKnowledgeBase(t, db, tenantID, kbID)
|
||||
literalID := seedLikeEscapeKnowledgeSet(t, db, tenantID, kbID)
|
||||
scopes := []types.KnowledgeSearchScope{{TenantID: tenantID, KBID: kbID}}
|
||||
|
||||
rows, hasMore, total, err := repo.SearchKnowledgeInScopes(ctx, scopes, "IMG_2534", 0, 10, nil)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, rows, 1, "underscore must match literally, not as a wildcard")
|
||||
assert.Equal(t, literalID, rows[0].ID)
|
||||
assert.False(t, hasMore)
|
||||
assert.Equal(t, int64(1), total)
|
||||
|
||||
rows, _, total, err = repo.SearchKnowledgeInScopes(ctx, scopes, "100%", 0, 10, nil)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, rows, 1, "percent must match literally, not as a wildcard")
|
||||
assert.Equal(t, int64(1), total)
|
||||
}
|
||||
|
||||
// Covers both the count and the data query of the tag search.
|
||||
func TestListTagsByKBKeywordTreatsLikeWildcardsLiterally(t *testing.T) {
|
||||
db := setupLikeEscapeTestDB(t)
|
||||
repo := NewKnowledgeTagRepository(db).(*knowledgeTagRepository)
|
||||
ctx := context.Background()
|
||||
const tenantID = uint64(1)
|
||||
kbID := uuid.New().String()
|
||||
|
||||
literalID := uuid.New().String()
|
||||
require.NoError(t, db.Create(&types.KnowledgeTag{
|
||||
ID: literalID, TenantID: tenantID, KnowledgeBaseID: kbID, Name: "v1_1",
|
||||
}).Error)
|
||||
require.NoError(t, db.Create(&types.KnowledgeTag{
|
||||
ID: uuid.New().String(), TenantID: tenantID, KnowledgeBaseID: kbID, Name: "v1x1",
|
||||
}).Error)
|
||||
|
||||
tags, total, err := repo.ListByKB(ctx, tenantID, kbID, &types.Pagination{Page: 1, PageSize: 50}, "v1_1")
|
||||
require.NoError(t, err)
|
||||
require.Len(t, tags, 1, "underscore must match literally, not as a wildcard")
|
||||
assert.Equal(t, literalID, tags[0].ID)
|
||||
assert.Equal(t, int64(1), total, "the count query must use the same escape")
|
||||
}
|
||||
|
||||
// Covers both branches of SearchTenants (with and without a tenant id).
|
||||
func TestSearchTenantsKeywordTreatsLikeWildcardsLiterally(t *testing.T) {
|
||||
db := setupLikeEscapeTestDB(t)
|
||||
repo := NewTenantRepository(db).(*tenantRepository)
|
||||
ctx := context.Background()
|
||||
|
||||
literal := &types.Tenant{Name: "Acme_Prod", Description: "literal underscore", Status: "active"}
|
||||
require.NoError(t, db.Create(literal).Error)
|
||||
require.NoError(t, db.Create(&types.Tenant{
|
||||
Name: "AcmeXProd", Description: "wildcard decoy", Status: "active",
|
||||
}).Error)
|
||||
|
||||
// keyword-only branch (tenantID = 0).
|
||||
tenants, total, err := repo.SearchTenants(ctx, "Acme_Prod", 0, 1, 20)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, tenants, 1, "underscore must match literally, not as a wildcard")
|
||||
assert.Equal(t, literal.ID, tenants[0].ID)
|
||||
assert.Equal(t, int64(1), total)
|
||||
|
||||
// tenantID + keyword branch.
|
||||
tenants, total, err = repo.SearchTenants(ctx, "Acme_Prod", literal.ID, 1, 20)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, tenants, 1)
|
||||
assert.Equal(t, literal.ID, tenants[0].ID)
|
||||
assert.Equal(t, int64(1), total)
|
||||
}
|
||||
|
||||
// Covers both member-search paths (count and page) of the email/username join.
|
||||
func TestTenantMemberSearchTreatsLikeWildcardsLiterally(t *testing.T) {
|
||||
db := setupLikeEscapeTestDB(t)
|
||||
repo := NewTenantMemberRepository(db).(*tenantMemberRepository)
|
||||
ctx := context.Background()
|
||||
const tenantID = uint64(1)
|
||||
|
||||
seedMember := func(id, username, email string) {
|
||||
t.Helper()
|
||||
require.NoError(t, db.Exec(
|
||||
`INSERT INTO users (id, username, email) VALUES (?, ?, ?)`, id, username, email).Error)
|
||||
require.NoError(t, db.Create(&types.TenantMember{
|
||||
UserID: id, TenantID: tenantID, Role: types.TenantRoleContributor,
|
||||
Status: types.TenantMemberStatusActive,
|
||||
}).Error)
|
||||
}
|
||||
literalID := uuid.New().String()
|
||||
seedMember(literalID, "ann_bob", "ann_bob@example.com")
|
||||
seedMember(uuid.New().String(), "annxbob", "annxbob@example.com")
|
||||
|
||||
total, err := repo.CountFilteredByTenant(ctx, tenantID, "ann_bob")
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, int64(1), total, "underscore must match literally, not as a wildcard")
|
||||
|
||||
members, err := repo.ListPagedByTenant(ctx, tenantID, "ann_bob", 0, 20)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, members, 1)
|
||||
assert.Equal(t, literalID, members[0].UserID)
|
||||
}
|
||||
|
||||
// Covers the session sidebar keyword filter in QueryPaged.
|
||||
func TestSessionQueryPagedKeywordTreatsLikeWildcardsLiterally(t *testing.T) {
|
||||
db := setupLikeEscapeTestDB(t)
|
||||
repo := NewSessionRepository(db)
|
||||
ctx := context.Background()
|
||||
const tenantID = uint64(7)
|
||||
const userID = "web_user:alice"
|
||||
|
||||
literal := &types.Session{TenantID: tenantID, UserID: userID, Title: "Deploy_Plan v1"}
|
||||
require.NoError(t, db.Create(literal).Error)
|
||||
require.NoError(t, db.Create(&types.Session{
|
||||
TenantID: tenantID, UserID: userID, Title: "DeployXPlan v1",
|
||||
}).Error)
|
||||
|
||||
rows, total, err := repo.QueryPaged(ctx, &types.SessionListQuery{
|
||||
TenantID: tenantID, UserID: userID, Keyword: "Deploy_Plan", Page: 1, PageSize: 20,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.Len(t, rows, 1, "underscore must match literally, not as a wildcard")
|
||||
assert.Equal(t, literal.ID, rows[0].ID)
|
||||
assert.Equal(t, int64(1), total, "the count query must use the same escape")
|
||||
}
|
||||
|
||||
// Covers the document chunk/Faq keyword filter, which previously did not
|
||||
// escape the keyword at all (so % and _ acted as wildcards).
|
||||
func TestListPagedChunksKeywordTreatsLikeWildcardsLiterally(t *testing.T) {
|
||||
db := setupLikeEscapeTestDB(t)
|
||||
repo := NewChunkRepository(db).(*chunkRepository)
|
||||
ctx := context.Background()
|
||||
const tenantID = uint64(1)
|
||||
knowledgeID := uuid.New().String()
|
||||
kbID := uuid.New().String()
|
||||
|
||||
literal := &types.Chunk{
|
||||
ID: uuid.New().String(), TenantID: tenantID, KnowledgeID: knowledgeID,
|
||||
KnowledgeBaseID: kbID, Content: "config v1_1 is active",
|
||||
ChunkType: types.ChunkTypeText, Status: int(types.ChunkStatusIndexed), IsEnabled: true,
|
||||
}
|
||||
require.NoError(t, db.Create(literal).Error)
|
||||
require.NoError(t, db.Create(&types.Chunk{
|
||||
ID: uuid.New().String(), TenantID: tenantID, KnowledgeID: knowledgeID,
|
||||
KnowledgeBaseID: kbID, Content: "config v1x1 is active",
|
||||
ChunkType: types.ChunkTypeText, Status: int(types.ChunkStatusIndexed), IsEnabled: true,
|
||||
}).Error)
|
||||
|
||||
chunks, total, err := repo.ListPagedChunksByKnowledgeID(
|
||||
ctx, tenantID, knowledgeID, &types.Pagination{Page: 1, PageSize: 50},
|
||||
[]types.ChunkType{types.ChunkTypeText}, nil, "v1_1", "", "",
|
||||
types.KnowledgeBaseTypeDocument, nil,
|
||||
)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, chunks, 1, "underscore must match literally, not as a wildcard")
|
||||
assert.Equal(t, literal.ID, chunks[0].ID)
|
||||
assert.Equal(t, int64(1), total)
|
||||
}
|
||||
|
|
@ -164,9 +164,9 @@ func (r *sessionRepository) QueryPaged(
|
|||
) ([]*types.SessionListItem, int64, error) {
|
||||
// Dialect-aware bits so the same query works on Postgres and SQLite (Lite build).
|
||||
isPostgres := r.db.Dialector.Name() == "postgres"
|
||||
titleLikeExpr := "LOWER(s.title) LIKE LOWER(?)"
|
||||
titleLikeExpr := "LOWER(s.title) LIKE LOWER(?) ESCAPE ?"
|
||||
if isPostgres {
|
||||
titleLikeExpr = "s.title ILIKE ?"
|
||||
titleLikeExpr = "s.title ILIKE ? ESCAPE ?"
|
||||
}
|
||||
// SQLite (the driver used by Lite) does not support NULLS LAST; its default
|
||||
// nulls ordering puts NULLs first for DESC, which is actually what we want
|
||||
|
|
@ -193,7 +193,7 @@ func (r *sessionRepository) QueryPaged(
|
|||
types.SkillMaintenanceSessionMarker+"%",
|
||||
)
|
||||
if kw := strings.TrimSpace(q.Keyword); kw != "" {
|
||||
db = db.Where(titleLikeExpr, "%"+escapeLikeKeyword(kw)+"%")
|
||||
db = db.Where(titleLikeExpr, "%"+escapeLikeKeyword(kw)+"%", likeEscapeChar)
|
||||
}
|
||||
return db
|
||||
}
|
||||
|
|
|
|||
|
|
@ -108,7 +108,7 @@ func (r *knowledgeTagRepository) ListByKB(
|
|||
Where("tenant_id = ? AND knowledge_base_id = ?", tenantID, kbID)
|
||||
if keyword != "" {
|
||||
escaped := escapeLikeKeyword(keyword)
|
||||
baseQuery = baseQuery.Where("name LIKE ?", "%"+escaped+"%")
|
||||
baseQuery = baseQuery.Where("name LIKE ? ESCAPE ?", "%"+escaped+"%", likeEscapeChar)
|
||||
}
|
||||
|
||||
if err := baseQuery.Count(&total).Error; err != nil {
|
||||
|
|
@ -119,7 +119,7 @@ func (r *knowledgeTagRepository) ListByKB(
|
|||
Where("tenant_id = ? AND knowledge_base_id = ?", tenantID, kbID)
|
||||
if keyword != "" {
|
||||
escaped := escapeLikeKeyword(keyword)
|
||||
dataQuery = dataQuery.Where("name LIKE ?", "%"+escaped+"%")
|
||||
dataQuery = dataQuery.Where("name LIKE ? ESCAPE ?", "%"+escaped+"%", likeEscapeChar)
|
||||
}
|
||||
|
||||
var tags []*types.KnowledgeTag
|
||||
|
|
|
|||
|
|
@ -83,12 +83,16 @@ func (r *tenantRepository) SearchTenants(ctx context.Context, keyword string, te
|
|||
// Build search conditions
|
||||
if tenantID > 0 && keyword != "" {
|
||||
escaped := escapeLikeKeyword(keyword)
|
||||
query = query.Where("id = ? OR name LIKE ? OR description LIKE ?", tenantID, "%"+escaped+"%", "%"+escaped+"%")
|
||||
query = query.Where(
|
||||
"id = ? OR name LIKE ? ESCAPE ? OR description LIKE ? ESCAPE ?",
|
||||
tenantID, "%"+escaped+"%", likeEscapeChar, "%"+escaped+"%", likeEscapeChar)
|
||||
} else if tenantID > 0 {
|
||||
query = query.Where("id = ?", tenantID)
|
||||
} else if keyword != "" {
|
||||
escaped := escapeLikeKeyword(keyword)
|
||||
query = query.Where("name LIKE ? OR description LIKE ?", "%"+escaped+"%", "%"+escaped+"%")
|
||||
query = query.Where(
|
||||
"name LIKE ? ESCAPE ? OR description LIKE ? ESCAPE ?",
|
||||
"%"+escaped+"%", likeEscapeChar, "%"+escaped+"%", likeEscapeChar)
|
||||
}
|
||||
|
||||
// Count total
|
||||
|
|
|
|||
|
|
@ -109,7 +109,9 @@ func (r *tenantMemberRepository) CountFilteredByTenant(
|
|||
like := "%" + escapeLikePattern(search) + "%"
|
||||
err = q.
|
||||
Joins(`INNER JOIN users ON users.id = tenant_members.user_id AND users.deleted_at IS NULL`).
|
||||
Where(`(LOWER(users.email) LIKE LOWER(?) OR LOWER(users.username) LIKE LOWER(?))`, like, like).
|
||||
Where(
|
||||
`(LOWER(users.email) LIKE LOWER(?) ESCAPE ? OR LOWER(users.username) LIKE LOWER(?) ESCAPE ?)`,
|
||||
like, likeEscapeChar, like, likeEscapeChar).
|
||||
Count(&total).Error
|
||||
}
|
||||
return total, err
|
||||
|
|
@ -134,7 +136,9 @@ func (r *tenantMemberRepository) ListPagedByTenant(
|
|||
like := "%" + escapeLikePattern(search) + "%"
|
||||
err = q.
|
||||
Joins(`INNER JOIN users ON users.id = tenant_members.user_id AND users.deleted_at IS NULL`).
|
||||
Where(`(LOWER(users.email) LIKE LOWER(?) OR LOWER(users.username) LIKE LOWER(?))`, like, like).
|
||||
Where(
|
||||
`(LOWER(users.email) LIKE LOWER(?) ESCAPE ? OR LOWER(users.username) LIKE LOWER(?) ESCAPE ?)`,
|
||||
like, likeEscapeChar, like, likeEscapeChar).
|
||||
Find(&members).Error
|
||||
}
|
||||
if err != nil {
|
||||
|
|
|
|||
|
|
@ -1305,14 +1305,10 @@ func (r *wikiPageRepository) DeleteIssuesByKnowledgeBaseID(
|
|||
|
||||
// escapeLikePattern escapes LIKE / ILIKE metacharacters so the returned string
|
||||
// can be safely concatenated with % wildcards without unintended matches.
|
||||
// Order matters: escape the backslash first, then the wildcards.
|
||||
// It is a thin alias for escapeLikeKeyword, which owns the (order-sensitive)
|
||||
// escaping rules; both must stay paired with an explicit ESCAPE clause.
|
||||
func escapeLikePattern(s string) string {
|
||||
replacer := strings.NewReplacer(
|
||||
`\`, `\\`,
|
||||
`%`, `\%`,
|
||||
`_`, `\_`,
|
||||
)
|
||||
return replacer.Replace(s)
|
||||
return escapeLikeKeyword(s)
|
||||
}
|
||||
|
||||
// Search performs full-text search on wiki pages within a knowledge base.
|
||||
|
|
|
|||
Loading…
Reference in New Issue