feat: add FTS5 search

This commit is contained in:
strsv committed 2026-11-06 08:01:39 +03:00
1 parent c441d770e2
commit f1624cfde2
17 files changed
+232 -26

No files matched your search

+79 -1
View File
@@ -1,12 +1,14 @@
import html
import json
import os
import re
import sqlite3
from datetime import datetime, timezone
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
DEFAULT_DB = os.path.join(BASE_DIR, "steam.db")
SCHEMA_PATH = os.path.join(BASE_DIR, "tools", "schema.sql")
SCHEMA_VERSION = 1
SCHEMA_VERSION = 2
MOOD_PRESETS = {
"adventure-rpg-party": {
@@ -252,8 +254,84 @@ def _migration_1(con):
_ensure_column(con, "game_details", "source_appid", "INTEGER")
def _plain_text(*parts):
text = " ".join(str(part) for part in parts if part)
text = re.sub(r"<[^>]+>", " ", text)
return " ".join(html.unescape(text).split())
def _search_tags(con, appid):
row = con.execute(
"""SELECT GROUP_CONCAT(tn.name, ' ') AS names
FROM game_tags gt JOIN tag_names tn ON tn.tag_id = gt.tag_id
WHERE gt.appid = ?""", (appid,)).fetchone()
return (row[0] if row and row[0] else "") if row else ""
def refresh_search_index(con):
"""Rebuild the FTS5 index for every game."""
rows = con.execute("""
SELECT g.appid, g.name, d.short_description, d.about,
t.short_description AS short_ru, t.about AS about_ru
FROM games g
LEFT JOIN game_details d ON d.appid = g.appid
LEFT JOIN game_translations t ON t.appid = g.appid
WHERE LOWER(g.type) = 'game'
""").fetchall()
tags = {r[0]: r[1] for r in con.execute("""
SELECT gt.appid, GROUP_CONCAT(tn.name, ' ')
FROM game_tags gt JOIN tag_names tn ON tn.tag_id = gt.tag_id
GROUP BY gt.appid
""")}
con.execute("DELETE FROM game_fts")
con.executemany(
"INSERT INTO game_fts(rowid, appid, name, description, tags) VALUES (?, ?, ?, ?, ?)",
[(r["appid"], r["appid"], r["name"],
_plain_text(r["short_description"], r["about"], r["short_ru"], r["about_ru"]),
tags.get(r["appid"], "")) for r in rows],
)
con.commit()
def refresh_search_game(con, appid):
"""Update one FTS5 row after details or tags change."""
row = con.execute("""
SELECT g.appid, g.name, d.short_description, d.about,
t.short_description AS short_ru, t.about AS about_ru
FROM games g
LEFT JOIN game_details d ON d.appid = g.appid
LEFT JOIN game_translations t ON t.appid = g.appid
WHERE g.appid = ? AND LOWER(g.type) = 'game'
""", (appid,)).fetchone()
con.execute("DELETE FROM game_fts WHERE rowid = ?", (appid,))
if row:
con.execute(
"INSERT INTO game_fts(rowid, appid, name, description, tags) VALUES (?, ?, ?, ?, ?)",
(appid, appid, row["name"],
_plain_text(row["short_description"], row["about"],
row["short_ru"], row["about_ru"]),
_search_tags(con, appid)),
)
con.commit()
def _migration_2(con):
"""Add the FTS5 search index over names, descriptions and tags."""
con.execute("""
CREATE VIRTUAL TABLE IF NOT EXISTS game_fts USING fts5(
appid UNINDEXED,
name,
description,
tags,
tokenize = 'unicode61 remove_diacritics 2'
)
""")
refresh_search_index(con)
MIGRATIONS = {
1: _migration_1,
2: _migration_2,
}
+2
View File
@@ -88,6 +88,7 @@ def save_details(con, appid, details, status="ok", source_appid=None):
fetched_at=excluded.fetched_at, status='missing'""",
(appid, "russian", now),
)
db.refresh_search_game(con, appid)
con.commit()
return
con.execute(
@@ -110,6 +111,7 @@ def save_details(con, appid, details, status="ok", source_appid=None):
details["developers"], details["publishers"], details["release_date"],
details["lang"], now, status, source_appid),
)
db.refresh_search_game(con, appid)
con.commit()
+2
View File
@@ -189,6 +189,8 @@ def main():
con.execute("INSERT OR REPLACE INTO schema_meta (key, value) VALUES ('steam_root', ?)", (root,))
con.execute("INSERT OR REPLACE INTO schema_meta (key, value) VALUES ('steamid', ?)", (steamid,))
db.refresh_search_index(con)
counts = {r["type"]: r["n"] for r in con.execute(
"SELECT COALESCE(LOWER(type), 'unknown') AS type, COUNT(*) AS n FROM games GROUP BY 1")}
con.close()
+8
View File
@@ -96,6 +96,14 @@ CREATE TABLE IF NOT EXISTS moods (
updated_at TEXT
);
CREATE VIRTUAL TABLE IF NOT EXISTS game_fts USING fts5(
appid UNINDEXED,
name,
description,
tags,
tokenize = 'unicode61 remove_diacritics 2'
);
CREATE VIEW IF NOT EXISTS v_library AS
SELECT
g.appid,