feat: add FTS5 search
This commit is contained in:
1 parent
c441d770e2
commit
f1624cfde2
17 files changed
+232
-26
No files matched your search
+79
-1
@@ -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,
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user