WeKnora/internal
linus-liu-web e39043f34b 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),属另一类问题,不在本次改动范围内。
2026-09-29 01:31:37 +08:00
..
agent fix(compaction): 流因取消而关闭时不得当成空摘要成功返回 2026-09-29 01:30:01 +08:00
application fix(repository): 关键词 LIKE/ILIKE 检索补显式 ESCAPE,修复 Lite 上静默漏结果 2026-09-29 01:31:37 +08:00
assets feat(assets): add ASR test audio file and embed it in the application 2026-04-02 21:27:27 +08:00
browserskill fix(browser): refine connection and preview UI with extension version 2026-09-23 10:57:59 +08:00
common fix(markdown): sanitize invalid UTF-8 and NUL bytes 2026-08-31 12:24:35 +08:00
config docs: 迁移有效文档至 website-docs 并清理旧文档 (#3580) 2026-09-22 23:25:40 +08:00
container fix(server): run ResourceCleaner on signal shutdown instead of racing Fatalf exit (#3804) 2026-09-28 23:37:36 +08:00
database revert: take the plugin system back out of main 2026-09-26 14:58:18 +08:00
datasource fix(rss): 修复全文回退后增量同步不重试 2026-09-29 00:53:27 +08:00
embedpolicy fix(embed): enforce host origin allowlists on framed pages 2026-09-14 18:03:57 +08:00
errors feat(knowledge): flag documents that stop making progress (#3557) 2026-09-22 21:34:08 +08:00
event fix(agent): end the turn when the completion cap truncates an answer (#3449) 2026-09-20 13:17:50 +08:00
filetransport refactor(resource-access): unify scoped authorization and transfer recovery (#3097) 2026-09-08 15:36:33 +08:00
handler fix(sandbox): pass desktop slot timing to the renew goroutine 2026-09-28 22:01:39 +08:00
im revert: take the plugin system back out of main 2026-09-26 14:58:18 +08:00
infrastructure fix(citations): 修复原文定位、高亮范围与 DocReader 启动失败 (#3794) 2026-09-27 20:32:26 +08:00
ipclass fix: align client sessions and integration request handling (#3305) 2026-09-16 16:20:57 +08:00
localsandbox feat(lite): install and run skills on the host (#3661) 2026-09-24 21:30:34 +08:00
logger fix(logger): keep LLM debug records inside their directory 2026-09-25 18:22:14 +08:00
mcp revert: take the plugin system back out of main 2026-09-26 14:58:18 +08:00
mcpserver fix(mcp): return accessible image URLs from existing tools (#3650) 2026-09-24 11:56:25 +08:00
middleware fix(knowledge): stop pipeline errors from stranding rows in processing 2026-09-22 20:33:18 +08:00
modelcontext fix(citations): 修复原文定位、高亮范围与 DocReader 启动失败 (#3794) 2026-09-27 20:32:26 +08:00
models fix(models): keep Retry-After on HTTPError and honour it in agent LLM retries 2026-09-29 01:22:15 +08:00
ratelimit feat(embed): add backend embed channels with auth and rate limiting (#1657) 2026-06-12 23:10:39 +08:00
reranking fix(retrieval): fix recall correctness, score scales, cost and agent tools (#3687) 2026-09-25 10:48:18 +08:00
router revert: take the plugin system back out of main 2026-09-26 14:58:18 +08:00
runtime fix(server): run ResourceCleaner on signal shutdown instead of racing Fatalf exit (#3804) 2026-09-28 23:37:36 +08:00
sandbox feat(lite): install and run skills on the host (#3661) 2026-09-24 21:30:34 +08:00
searchutil feat(wiki): 增加跨知识库 Wiki 搜索 POST /wiki-search (#3535) 2026-09-25 22:22:58 +08:00
sourceloc fix(citations): 修复原文定位、高亮范围与 DocReader 启动失败 (#3794) 2026-09-27 20:32:26 +08:00
storageallowlist fix(storage): align default provider fallback with STORAGE_ALLOW_LIST 2026-07-15 11:05:00 +08:00
storageurl fix(mcp): return accessible image URLs from existing tools (#3650) 2026-09-24 11:56:25 +08:00
stream feat(session): in-place rewind with workspace checkpoint reset (#3469) 2026-09-20 21:01:00 +08:00
textconv fix: remove GPL converter and bundle third-party license sources 2026-09-08 15:19:10 +08:00
tracing/langfuse fix(milvus): preserve keyword retrieval scores 2026-09-17 11:59:40 +08:00
types fix(server): run ResourceCleaner on signal shutdown instead of racing Fatalf exit (#3804) 2026-09-28 23:37:36 +08:00
utils feat(security): add a whitelist-only outbound mode that refuses before DNS (#3393) 2026-09-23 12:02:06 +08:00