Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.
Go agentagenticaichatbotdsh-pluginembeddingsevaluationgenerative-aigolangknowledge-basellmmulti-tenantollamaopenaiquestion-answeringragrerankingsemantic-searchvector-searchwiki
fix(repository): 关键词 LIKE/ILIKE 检索补显式 ESCAPE,修复 Lite 上静默漏结果
linus-liu-webe39043f34b22
diff --git a/internal/application/repository/chunk.go b/internal/application/repository/chunk.goindex 6b6d340c9..ee27e8414 100644--- a/internal/application/repository/chunk.go+++ b/internal/application/repository/chunk.go@@ -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 onlyif 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 metadataif 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 metadataif 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 metadataif 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)}}}diff --git a/internal/application/repository/knowledge.go b/internal/application/repository/knowledge.goindex eb33a620e..9cfd90fd1 100644--- a/internal/application/repository/knowledge.go+++ b/internal/application/repository/knowledge.go@@ -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 {diff --git a/internal/application/repository/like_escape_sqlite_test.go b/internal/application/repository/like_escape_sqlite_test.gonew file mode 100644index 000000000..f4cdbae36--- /dev/null+++ b/internal/application/repository/like_escape_sqlite_test.go@@ -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", "[email protected]")+ seedMember(uuid.New().String(), "annxbob", "[email protected]")++ 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)+}diff --git a/internal/application/repository/session.go b/internal/application/repository/session.goindex b693487bd..fcba558b4 100644--- a/internal/application/repository/session.go+++ b/internal/application/repository/session.go@@ -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}diff --git a/internal/application/repository/tag.go b/internal/application/repository/tag.goindex ca2256050..7e39f4008 100644--- a/internal/application/repository/tag.go+++ b/internal/application/repository/tag.go@@ -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.KnowledgeTagdiff --git a/internal/application/repository/tenant.go b/internal/application/repository/tenant.goindex 9d026574d..7b0fe99ed 100644--- a/internal/application/repository/tenant.go+++ b/internal/application/repository/tenant.go@@ -83,12 +83,16 @@ func (r *tenantRepository) SearchTenants(ctx context.Context, keyword string, te// Build search conditionsif 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 totaldiff --git a/internal/application/repository/tenant_member.go b/internal/application/repository/tenant_member.goindex 17c34a315..4201f8290 100644--- a/internal/application/repository/tenant_member.go+++ b/internal/application/repository/tenant_member.go@@ -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 {diff --git a/internal/application/repository/wiki_page.go b/internal/application/repository/wiki_page.goindex 28430a72f..22ce428b3 100644--- a/internal/application/repository/wiki_page.go+++ b/internal/application/repository/wiki_page.go@@ -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.