Tìm kiếm

search module
Scope: User (theo tên) · Post (theo nội dung) · Hashtag  ·  Công nghệ: MySQL LIKE / FULLTEXT / Pinecone AI
3
Loại tìm kiếm
user · post · hashtag
Stub
Service
3 methods → return {}
Pinecone
AI Search
queue có sẵn, query chưa
0
Autocomplete
chưa có

Proposed: Unified Search Flow


GET /search?q=keyword&type=user|post|hashtag Sanitize + validate query (min 1 char) type? user MySQL: LIKE username LIKE '%q%' OR name LIKE '%q%' post Pinecone ready? yes Pinecone: semantic search embed query → cosine similarity → IDs no (fallback) MySQL: FULLTEXT search MATCH(content) AGAINST(q IN BOOLEAN MODE) hashtag MySQL: Topic table name LIKE '%q%', GROUP BY, count posts Response: { results[], pagination } uniform format: id, type, title, snippet, score

Tìm kiếm 3 loại khác nhau: user (LIKE), post (Pinecone AI hoặc MySQL FULLTEXT), hashtag (Topic table). Response thống nhất format.

Implementation Checklist


#TaskChi tiếtStatus
01
GET /search?q=&type=user
Tìm user theo tên
MySQL LIKE trên usernamename. Return: avatar, name, username, followersCount. Pagination limit 20.stub
02
GET /search?q=&type=post
Tìm bài theo nội dung
Phase 1: content LIKE '%q%'. Phase 2: MySQL FULLTEXT MATCH...AGAINST. Phase 3: Pinecone semantic search.stub
03
GET /search?q=&type=hashtag
Tìm hashtag
Query bảng Topic: name ILIKE '%q%', count bài liên quan qua TopicsPost. Sort theo postCount.stub
04
Pinecone query integration
Dự án đã có addToPineconeQueue để index bài. Cần thêm query endpoint: embed query → search Pinecone → fetch posts by IDs.todo
05
Autocomplete / suggest
Khi user gõ → gợi ý nhanh. Dùng Redis sorted set cache top queries + MySQL LIKE 'q%' (prefix match). Debounce 300ms phía FE.todo
06
Search history
Lưu lịch sử tìm kiếm per user (max 20 gần nhất). API: GET + DELETE search history.todo
07
FULLTEXT index
MySQL FULLTEXT index trên content cho full-text search nhanh. Migration thêm ALTER TABLE Post ADD FULLTEXT(content).todo

Potential Issues


LIKE chậm
LIKE '%keyword%' không dùng index, full table scan. OK cho <50K rows. Cần FULLTEXT index hoặc ngram parser cho scale.
Tiếng Việt
MySQL FULLTEXT mặc định dùng ngram parser cho CJK/Vietnamese. Cấu hình ngram_token_size=2 trong my.cnf. Pinecone xử lý tốt hơn nhờ multilingual embedding model.
Injection
Query q cần sanitize: trim, remove SQL special chars, escape %_ cho LIKE. Zod validate min 1 char, max 100 char.
Rate limit
Search là endpoint tốn tài nguyên. Rate limit 30 req/phút per user. Autocomplete debounce 300ms phía FE.