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
@@ -3468,6 +3468,127 @@ select:disabled {
|
||||
box-shadow: none !important;
|
||||
}
|
||||
|
||||
/* ============================================================
|
||||
🔍 帖子搜索
|
||||
============================================================ */
|
||||
|
||||
.feed-search {
|
||||
position: relative;
|
||||
flex: 1;
|
||||
min-width: 140px;
|
||||
max-width: 280px;
|
||||
/* 夹在排序标签与发布按钮之间,靠右对齐 */
|
||||
margin-left: auto;
|
||||
}
|
||||
|
||||
.feed-search input {
|
||||
width: 100%;
|
||||
box-sizing: border-box;
|
||||
/* 右侧留出清除按钮的位置 */
|
||||
padding-right: 34px !important;
|
||||
margin-bottom: 0 !important;
|
||||
}
|
||||
|
||||
/* 有内容时才出现的清除按钮 */
|
||||
.feed-search-clear {
|
||||
position: absolute;
|
||||
right: 6px;
|
||||
top: 50%;
|
||||
transform: translateY(-50%);
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
width: 22px;
|
||||
height: 22px;
|
||||
padding: 0;
|
||||
border: none;
|
||||
border-radius: 50%;
|
||||
background: transparent;
|
||||
color: var(--glass-text-light);
|
||||
cursor: pointer;
|
||||
transition: background var(--dur-fast, 0.16s) ease, color var(--dur-fast, 0.16s) ease;
|
||||
}
|
||||
|
||||
.feed-search-clear:hover {
|
||||
background: rgba(127, 127, 147, 0.18);
|
||||
color: var(--glass-text);
|
||||
}
|
||||
|
||||
/* 搜索中:输入框右侧显示一个细进度条,替代整块骨架屏
|
||||
(搜索通常很快,画骨架反而闪得厉害) */
|
||||
.feed-search.is-searching::after {
|
||||
content: '';
|
||||
position: absolute;
|
||||
left: 2px;
|
||||
right: 2px;
|
||||
bottom: -3px;
|
||||
height: 2px;
|
||||
border-radius: 2px;
|
||||
background: linear-gradient(90deg, transparent, var(--primary), transparent);
|
||||
background-size: 200% 100%;
|
||||
animation: searchProgress 0.9s linear infinite;
|
||||
}
|
||||
|
||||
@keyframes searchProgress {
|
||||
from { background-position: 200% 0; }
|
||||
to { background-position: -200% 0; }
|
||||
}
|
||||
|
||||
@media (prefers-reduced-motion: reduce) {
|
||||
.feed-search.is-searching::after {
|
||||
animation: none;
|
||||
background: var(--primary);
|
||||
}
|
||||
}
|
||||
|
||||
/* 窄屏时搜索框独占一行,避免与标签、按钮挤在一起 */
|
||||
@media (max-width: 640px) {
|
||||
.feed-search {
|
||||
order: 3;
|
||||
width: 100%;
|
||||
max-width: none;
|
||||
margin-left: 0;
|
||||
margin-top: 10px;
|
||||
}
|
||||
}
|
||||
|
||||
/* 搜索结果提示条 */
|
||||
.search-summary {
|
||||
font-size: 13px;
|
||||
color: var(--glass-text-light);
|
||||
margin-bottom: 12px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
|
||||
.search-summary strong {
|
||||
color: var(--glass-text);
|
||||
}
|
||||
|
||||
.search-summary button {
|
||||
border: none;
|
||||
background: none;
|
||||
padding: 0;
|
||||
color: var(--primary);
|
||||
cursor: pointer;
|
||||
font-size: 13px;
|
||||
text-decoration: underline;
|
||||
}
|
||||
|
||||
/* 无搜索结果 */
|
||||
.search-empty {
|
||||
text-align: center;
|
||||
color: var(--glass-text-light);
|
||||
padding: 60px 0;
|
||||
line-height: 1.9;
|
||||
}
|
||||
|
||||
.search-empty strong {
|
||||
color: var(--glass-text);
|
||||
}
|
||||
|
||||
/* ===== 列表容器高度稳定 =====
|
||||
骨架屏按「理想内容」绘制,实际帖子往往更短,数据到达时容器高度会
|
||||
骤降,导致其下方的内容(分页控件、页脚)整体上跳——这就是首屏
|
||||
|
||||
Reference in new issue
Block a user