Add searchable lyrics index

This commit is contained in:
Jonathan Sykes
2026-10-14 02:10:02 +08:00
parent e3821621f3
commit 11b10e38e2
6 changed files with 209 additions and 2 deletions

View File

@@ -20,6 +20,7 @@ const DB_PATH = process.env.DB_PATH || join(process.cwd(), 'data', 'ytplayer.db'
export const db = createClient({
url: 'file:' + DB_PATH,
});
let lyricsFtsAvailable = false;
// ---- Schema ----------------------------------------------------------------
@@ -279,6 +280,7 @@ export async function initDb() {
CREATE INDEX IF NOT EXISTS idx_audit_actor_action ON audit_log (actor, action, ts DESC);
`);
await migrateVideoMeta();
await initLyricsSearch();
try { await db.execute('ALTER TABLE media_cache ADD COLUMN pinned INTEGER NOT NULL DEFAULT 0'); } catch { /* already migrated */ }
// Uploads shared from devices (PWA share target): who sent it, and whether it
// is listed in everyone's search (admin uploads) or reachable only by link.
@@ -307,6 +309,75 @@ export async function initDb() {
}
}
function lyricsSearchContent(value) {
try {
const data = typeof value === 'string' ? JSON.parse(value) : value;
return [...(data?.lines || []).flatMap(line => [line?.text, line?.secondary, line?.phonetic]), ...(data?.tags || [])]
.filter(Boolean).join(' ').slice(0, 500_000);
} catch { return ''; }
}
async function initLyricsSearch() {
await db.execute('CREATE TABLE IF NOT EXISTS lyrics_search_state (id INTEGER PRIMARY KEY CHECK(id=1), indexed_at INTEGER NOT NULL)');
try {
await db.execute("CREATE VIRTUAL TABLE IF NOT EXISTS lyrics_search_fts USING fts5(video_id UNINDEXED, content, tokenize='unicode61 remove_diacritics 2')");
lyricsFtsAvailable = true;
} catch {
await db.execute('CREATE TABLE IF NOT EXISTS lyrics_search_fts (video_id TEXT PRIMARY KEY, content TEXT NOT NULL)');
await db.execute('CREATE INDEX IF NOT EXISTS idx_lyrics_search_id ON lyrics_search_fts(video_id)');
lyricsFtsAvailable = false;
}
const state = await db.execute('SELECT id FROM lyrics_search_state WHERE id=1');
if (state.rows.length) return;
await db.execute('DELETE FROM lyrics_search_fts');
const rows = await db.execute("SELECT video_id,data FROM video_notes WHERE kind='lyrics'");
for (let i = 0; i < rows.rows.length; i += 100) {
const batch = rows.rows.slice(i, i + 100).map(row => ({
sql: lyricsFtsAvailable ? 'INSERT INTO lyrics_search_fts(video_id,content) VALUES(?,?)' : 'INSERT OR REPLACE INTO lyrics_search_fts(video_id,content) VALUES(?,?)',
args: [row.video_id, lyricsSearchContent(row.data)],
}));
if (batch.length) await db.batch(batch, 'write');
}
await db.execute('INSERT INTO lyrics_search_state(id,indexed_at) VALUES(1,unixepoch())');
}
export async function searchNoteLyrics(query, { limit = 30, offset = 0, hasChapters = false, withoutChapters = false } = {}) {
const words = String(query || '').normalize('NFD').replace(/[\u0300-\u036f]/g, '').match(/[\p{L}\p{N}_-]+/gu) || [];
if (!words.length) return [];
const boundedLimit = Math.max(1, Math.min(100, Number(limit) || 30));
const boundedOffset = Math.max(0, Math.min(1_000_000, Number(offset) || 0));
const chapterFilter = hasChapters ? "AND EXISTS (SELECT 1 FROM video_notes chapter WHERE chapter.video_id=lyrics_search_fts.video_id AND chapter.kind='chapters')" : withoutChapters ? "AND NOT EXISTS (SELECT 1 FROM video_notes chapter WHERE chapter.video_id=lyrics_search_fts.video_id AND chapter.kind='chapters')" : '';
if (lyricsFtsAvailable) {
const match = words.map(word => `"${word.replaceAll('"', '""')}"*`).join(' AND ');
const result = await db.execute({ sql: `SELECT video_id, snippet(lyrics_search_fts,1,char(1),char(2),' … ',18) AS snippet
FROM lyrics_search_fts WHERE lyrics_search_fts MATCH ? ${chapterFilter} ORDER BY bm25(lyrics_search_fts) LIMIT ? OFFSET ?`, args: [match, boundedLimit, boundedOffset] });
return result.rows.map(row => ({ videoId: row.video_id, snippet: row.snippet }));
}
const filters = words.map(() => 'content LIKE ?').join(' AND ');
const result = await db.execute({ sql: `SELECT video_id,substr(content,1,220) AS snippet FROM lyrics_search_fts WHERE ${filters} ORDER BY video_id LIMIT ? OFFSET ?`, args: [...words.map(word => `%${word}%`), boundedLimit, boundedOffset] });
return result.rows.map(row => ({ videoId: row.video_id, snippet: row.snippet }));
}
export async function countNoteLyrics(query, { hasChapters = false, withoutChapters = false } = {}) {
const words = String(query || '').normalize('NFD').replace(/[\u0300-\u036f]/g, '').match(/[\p{L}\p{N}_-]+/gu) || [];
if (!words.length) return 0;
const chapterFilter = hasChapters ? "AND EXISTS (SELECT 1 FROM video_notes chapter WHERE chapter.video_id=lyrics_search_fts.video_id AND chapter.kind='chapters')" : withoutChapters ? "AND NOT EXISTS (SELECT 1 FROM video_notes chapter WHERE chapter.video_id=lyrics_search_fts.video_id AND chapter.kind='chapters')" : '';
if (lyricsFtsAvailable) {
const match = words.map(word => `"${word.replaceAll('"', '""')}"*`).join(' AND ');
return Number((await db.execute({ sql: `SELECT COUNT(*) AS n FROM lyrics_search_fts WHERE lyrics_search_fts MATCH ? ${chapterFilter}`, args: [match] })).rows[0]?.n) || 0;
}
const filters = words.map(() => 'content LIKE ?').join(' AND ');
const result = await db.execute({ sql: `SELECT COUNT(*) AS n FROM lyrics_search_fts WHERE ${filters} ${chapterFilter}`, args: words.map(word => `%${word}%`) });
return Number(result.rows[0]?.n) || 0;
}
export async function rebuildLyricsSearch() {
await db.execute('DELETE FROM lyrics_search_fts');
await db.execute('DELETE FROM lyrics_search_state');
await initLyricsSearch();
return Number((await db.execute("SELECT COUNT(*) AS n FROM video_notes WHERE kind='lyrics'")).rows[0]?.n) || 0;
}
const YOUTUBE_ID = /^[\w-]{11}$/;
const folded = value => String(value || '').normalize('NFD').replace(/[\u0300-\u036f]/g, '').toLowerCase();
@@ -395,6 +466,11 @@ export async function saveNote({ videoId, kind, data, baseRev, source, updatedBy
VALUES (?, ?, ?, ?, ?, ?, unixepoch())`,
args: [videoId, kind, rev, json, source, updatedBy || null],
});
if (kind === 'lyrics') {
await tx.execute({ sql: 'DELETE FROM lyrics_search_fts WHERE video_id=?', args: [videoId] });
const content = lyricsSearchContent(data);
if (content) await tx.execute({ sql: 'INSERT INTO lyrics_search_fts(video_id,content) VALUES(?,?)', args: [videoId, content] });
}
await tx.commit();
return { ok: true, rev };
} catch (err) {