`title: 前端全文索引与离线检索:倒排索引持久化``categories: Web 开发/前端/数据管理``keywords: 倒排索引,全文检索,IndexedDB,离线,检索``description: 在前端构建简化的倒排索引并持久化到 IndexedDB,实现离线搜索能力与快速查询(不依赖第三方库)。`索引结构`docs(id,text)` 与 `index(term -> [docId])`;分词与归一化需根据语言选择策略。插入示例async function addDoc(db, id, text) { const terms = tokenize(text); return new Promise((resolve, reject) => { const tx = db.transaction(['docs','index'], 'readwrite'); tx.objectStore('docs').put({ id, text }); const idx = tx.objectStore('index'); for (const t of terms) { const r = idx.get(t); r.onsuccess = () => { const list = r.result?.list || []; idx.put({ term: t, list: [...new Set([...list, id])] }); }; } tx.oncomplete = () => resolve(); tx.onerror = () => reject(tx.error); }); }

发表评论 取消回复