Private
Public Access
Add full-text search with per-backend SQLite/Postgres implementations
Implements the backend-agnostic search interface from the plan, selected at runtime by connection.vendor: - SQLite: an FTS5 virtual table (external-content, mirroring Article.title/content_html) created via a vendor-conditional migration, kept in sync by insert/update/delete triggers. Queries wrap the whole user input as a single FTS5 phrase so punctuation in a search box can't break MATCH syntax. `reindex_search` management command rebuilds it after bulk imports. - Postgres: computes the search vector on the fly per the plan's decision (title weighted above content, no stored/indexed column) rather than diverging the schema from SQLite. Both backends scope results to the requesting user's own subscriptions and share the stream/detail article-row template and read/star/save actions via a new apps.articles.queries.with_user_state() helper, extracted from apps.reader.views to avoid duplicating that annotation logic for search results. Verified live: the SQLite path end-to-end in-browser against the real Hacker News subscription (search UI, phrase matching, no-match case), and the Postgres path against a throwaway Postgres container (search, feed-scoping, and title-vs-content ranking), torn down afterward — not part of this project's own infrastructure. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
5ed0499845
commit
8653dbeaf9
19 files changed
+569
-20
No files matched your search
@@ -0,0 +1,78 @@
|
||||
"""Creates (and on reverse, drops) a SQLite FTS5 virtual table mirroring
|
||||
apps_articles.Article(title, content_html), plus triggers that keep it in
|
||||
sync on insert/update/delete.
|
||||
|
||||
No-ops entirely on Postgres — the Postgres backend computes its search
|
||||
vector on the fly instead of maintaining a separate index table, see
|
||||
apps/search/backends/postgres.py.
|
||||
"""
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
CREATE_FTS_TABLE = """
|
||||
CREATE VIRTUAL TABLE IF NOT EXISTS articles_fts USING fts5(
|
||||
title, content_html, content='articles_article', content_rowid='id'
|
||||
);
|
||||
"""
|
||||
|
||||
POPULATE_FTS_TABLE = """
|
||||
INSERT INTO articles_fts(rowid, title, content_html)
|
||||
SELECT id, title, content_html FROM articles_article;
|
||||
"""
|
||||
|
||||
CREATE_INSERT_TRIGGER = """
|
||||
CREATE TRIGGER IF NOT EXISTS articles_fts_ai AFTER INSERT ON articles_article BEGIN
|
||||
INSERT INTO articles_fts(rowid, title, content_html)
|
||||
VALUES (new.id, new.title, new.content_html);
|
||||
END;
|
||||
"""
|
||||
|
||||
CREATE_DELETE_TRIGGER = """
|
||||
CREATE TRIGGER IF NOT EXISTS articles_fts_ad AFTER DELETE ON articles_article BEGIN
|
||||
INSERT INTO articles_fts(articles_fts, rowid, title, content_html)
|
||||
VALUES('delete', old.id, old.title, old.content_html);
|
||||
END;
|
||||
"""
|
||||
|
||||
CREATE_UPDATE_TRIGGER = """
|
||||
CREATE TRIGGER IF NOT EXISTS articles_fts_au AFTER UPDATE ON articles_article BEGIN
|
||||
INSERT INTO articles_fts(articles_fts, rowid, title, content_html)
|
||||
VALUES('delete', old.id, old.title, old.content_html);
|
||||
INSERT INTO articles_fts(rowid, title, content_html)
|
||||
VALUES (new.id, new.title, new.content_html);
|
||||
END;
|
||||
"""
|
||||
|
||||
DROP_EVERYTHING = """
|
||||
DROP TRIGGER IF EXISTS articles_fts_au;
|
||||
DROP TRIGGER IF EXISTS articles_fts_ad;
|
||||
DROP TRIGGER IF EXISTS articles_fts_ai;
|
||||
DROP TABLE IF EXISTS articles_fts;
|
||||
"""
|
||||
|
||||
|
||||
def create_fts(apps, schema_editor):
|
||||
if schema_editor.connection.vendor != "sqlite":
|
||||
return
|
||||
with schema_editor.connection.cursor() as cursor:
|
||||
cursor.execute(CREATE_FTS_TABLE)
|
||||
cursor.execute(POPULATE_FTS_TABLE)
|
||||
cursor.execute(CREATE_INSERT_TRIGGER)
|
||||
cursor.execute(CREATE_DELETE_TRIGGER)
|
||||
cursor.execute(CREATE_UPDATE_TRIGGER)
|
||||
|
||||
|
||||
def drop_fts(apps, schema_editor):
|
||||
if schema_editor.connection.vendor != "sqlite":
|
||||
return
|
||||
with schema_editor.connection.cursor() as cursor:
|
||||
for statement in DROP_EVERYTHING.strip().split(";"):
|
||||
statement = statement.strip()
|
||||
if statement:
|
||||
cursor.execute(statement)
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
dependencies = [("articles", "0001_initial")]
|
||||
|
||||
operations = [migrations.RunPython(create_fts, drop_fts)]
|
||||
Reference in new issue
Block a user