diff --git a/src/commands/kb/_search.ts b/src/commands/kb/_search.ts index c3032da..366df10 100644 --- a/src/commands/kb/_search.ts +++ b/src/commands/kb/_search.ts @@ -60,17 +60,25 @@ export async function search(opts: SearchOptions): Promise { `; params = [tsquery]; } else { - // CJK path: pg_trgm + // CJK path: pg_trgm (trigram similarity) + ILIKE (exact substring fallback) + // pg_trgm.similarity_threshold 默认 0.3, 对 3 字 CJK (similarity ≈ 0.17) 太严 + // 双层覆盖: trigram 模糊匹配 + ILIKE 精确子串 (短查询兜底) sql = ` SELECT e.id, d.domain, e.section, e.content, - similarity(e.content, $1) AS rank, + GREATEST( + similarity(e.content, $1), + CASE WHEN e.content ILIKE '%' || $1 || '%' THEN 0.5 ELSE 0 END, + CASE WHEN e.section ILIKE '%' || $1 || '%' THEN 0.6 ELSE 0 END + ) AS rank, COALESCE( substring(e.content from greatest(1, position($1 in e.content) - 30) for 100), substring(e.content from 1 for 100) ) AS snippet FROM ${source} e JOIN ${SCHEMA}.domains d ON d.id = e.domain_id - WHERE e.content % $1 OR e.section % $1 + WHERE e.content % $1 + OR e.content ILIKE '%' || $1 || '%' + OR e.section ILIKE '%' || $1 || '%' `; params = [q]; }