feat: 新增帖子搜索功能
支持按标题与正文搜索帖子,入口在首页排序标签与发布按钮之间。 实现方式选择 ILIKE 子串匹配而非 PostgreSQL 全文检索:后者依赖分词, 中文分词需额外安装 zhparser 等扩展,多数部署环境没有;ILIKE 无需任何 扩展即可正确匹配中文子串,英文也天然大小写不敏感。 后端(GET /api/posts/search) - 关键词长度限制 100 字,超长或为空返回 400 - 转义 % 与 _:它们是 LIKE 的通配符,若不过滤,搜索「%」会匹配全部 帖子 - 复用 parsePagination,响应结构与 /api/posts 一致 - 路由必须声明在 /api/posts/:id 之前,否则 search 会被当作 id 匹配 - 新增迁移 005:创建 pg_trgm 扩展与 trigram 索引。 实测索引效果分两种情况——英文关键词(postgresql)走 Bitmap Index Scan,0.2ms,比全表扫描快约 47 倍;中文因 pg_trgm 难以切出有效 trigram,仍走全表扫描,3000 条数据约 7~9ms。即索引对英文有效、 对中文无效,考虑到许多论坛中英混排,予以保留。 创建扩展需要权限,部分托管数据库不开放,故失败时仅提示,不影响 搜索功能本身。 前端 - 首页新增搜索框,含清除按钮;输入防抖 350ms 自动搜索,回车立即搜索, Esc 清空 - 关键词写入 URL(?q=),刷新与分享可保持搜索状态;初始化时先于 renderFeed 设定关键词,避免首屏先按普通列表加载一次 - 搜索时输入框下方显示细进度条,而非整块骨架屏——搜索通常很快, 画骨架反而闪得厉害 - 无结果时给出明确提示与改进建议 - 移动端搜索框独占一行,避免与标签、按钮挤压 - 补充 6 条 i18n 文案;空结果提示中的关键词单独放在 <strong> 内, 使固定文案能作为独立文本节点被词典匹配 实测(Puppeteer + 接口直测): - 中文「论坛」「分词」、英文 resend/RESEND 均正确命中 - 空关键词与超长关键词正确返回 400 - 搜索 % 与 _ 返回 0 条,确认通配符已转义 - 输入「分词」后列表由 5 条筛为 1 条,URL 同步为 ?q=分词 - 清除按钮恢复全部列表并移除 URL 参数 - 通过 ?q=论坛 直接访问时,输入框自动填入且结果为 2 条 完整测试套件 25/25 通过。
This commit is contained in:
1 parent
9844988cf2
commit
fdf90f4b1d
9 files changed
+397
-3
No files matched your search
@@ -1167,6 +1167,72 @@ app.get('/api/posts', async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// 搜索帖子(公开)
|
||||
// 用 ILIKE 做子串匹配:PostgreSQL 的全文检索对中文需要额外分词扩展
|
||||
// (zhparser 等),否则中文搜不出来;ILIKE 无需扩展即可正确匹配中文
|
||||
// 子串,英文也天然大小写不敏感。数据量在万级以内性能足够。
|
||||
//
|
||||
// 转义 % 与 _:它们是 LIKE 的通配符,用户搜索「100%」这类内容时若不
|
||||
// 转义会被当作模式匹配。
|
||||
function escapeLikePattern(value) {
|
||||
return value.replace(/[\\%_]/g, match => '\\' + match);
|
||||
}
|
||||
|
||||
// 注意:本路由必须声明在 /api/posts/:id 之前,否则 search 会被当作 id
|
||||
app.get('/api/posts/search', async (req, res) => {
|
||||
const raw = typeof req.query.q === 'string' ? req.query.q.trim() : '';
|
||||
|
||||
if (raw.length === 0) {
|
||||
return res.status(400).json({ error: '请输入搜索关键词' });
|
||||
}
|
||||
if (raw.length > 100) {
|
||||
return res.status(400).json({ error: '搜索关键词过长(最多 100 字)' });
|
||||
}
|
||||
|
||||
const pagination = parsePagination(req.query);
|
||||
if (!pagination) return res.status(400).json({ error: '分页参数无效' });
|
||||
const { page, limit, offset } = pagination;
|
||||
|
||||
const pattern = '%' + escapeLikePattern(raw) + '%';
|
||||
|
||||
try {
|
||||
const countResult = await pool.query(
|
||||
`SELECT COUNT(*) AS total FROM posts p
|
||||
WHERE p.title ILIKE $1 OR p.content ILIKE $1`,
|
||||
[pattern]
|
||||
);
|
||||
const total = parseInt(countResult.rows[0]?.total || 0);
|
||||
|
||||
const r = await pool.query(
|
||||
`SELECT
|
||||
p.*,
|
||||
u.username,
|
||||
u.avatar_url,
|
||||
u.signature,
|
||||
(SELECT COUNT(*) FROM replies WHERE post_id = p.id) AS reply_count
|
||||
FROM posts p
|
||||
JOIN users u ON p.user_id = u.id
|
||||
WHERE p.title ILIKE $1 OR p.content ILIKE $1
|
||||
ORDER BY p.created_at DESC
|
||||
LIMIT $2 OFFSET $3`,
|
||||
[pattern, limit, offset]
|
||||
);
|
||||
|
||||
res.json({
|
||||
data: r.rows,
|
||||
query: raw,
|
||||
pagination: {
|
||||
page,
|
||||
limit,
|
||||
total,
|
||||
totalPages: Math.ceil(total / limit)
|
||||
}
|
||||
});
|
||||
} catch (err) {
|
||||
res.status(500).json({ error: '服务器错误' });
|
||||
}
|
||||
});
|
||||
|
||||
// 获取单个帖子
|
||||
app.get('/api/posts/:id', validateUuidId, async (req, res) => {
|
||||
try {
|
||||
|
||||
Reference in new issue
Block a user