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:
ksmithandClaude Sonnet 5 committed 2026-10-30 16:37:22 -06:00
1 parent 5ed0499845
commit 8653dbeaf9
19 files changed
+569 -20

No files matched your search

+16
View File
@@ -0,0 +1,16 @@
from django.db import connection
from .base import SearchBackend
__all__ = ["SearchBackend", "get_backend"]
def get_backend() -> SearchBackend:
if connection.vendor == "postgresql":
from .postgres import PostgresSearchBackend
return PostgresSearchBackend()
from .sqlite_fts import SQLiteFTSBackend
return SQLiteFTSBackend()
+9
View File
@@ -0,0 +1,9 @@
class SearchBackend:
"""Common interface so views never branch on DB vendor directly —
see apps/search/backends/__init__.py::get_backend()."""
def search(self, query: str, user, limit: int = 50):
"""Returns an iterable of Article instances the user can see
(scoped to their own subscriptions), matching ``query``, most
relevant first."""
raise NotImplementedError
+27
View File
@@ -0,0 +1,27 @@
from django.contrib.postgres.search import SearchQuery, SearchRank, SearchVector
from apps.articles.models import Article
from .base import SearchBackend
class PostgresSearchBackend(SearchBackend):
"""Computes the search vector on the fly at query time rather than
storing/indexing it on Article — keeps the schema identical to the
SQLite backend. Revisit with a stored+GIN-indexed column only if an
instance's archive grows large enough to need it."""
def search(self, query: str, user, limit: int = 50):
if not query.strip():
return Article.objects.none()
vector = SearchVector("title", weight="A") + SearchVector("content_html", weight="B")
search_query = SearchQuery(query)
return (
Article.objects.filter(feed__subscriptions__user=user)
.select_related("feed")
.annotate(search=vector, rank=SearchRank(vector, search_query))
.filter(search=search_query)
.order_by("-rank", "-published_at")[:limit]
)
+48
View File
@@ -0,0 +1,48 @@
import sqlite3
from django.db import connection
from apps.articles.models import Article
from apps.feeds.models import Subscription
from .base import SearchBackend
def _fts_phrase_query(raw_query: str) -> str:
"""Wraps the whole user input as a single FTS5 phrase so arbitrary
punctuation (hyphens, quotes, parentheses...) in a search box doesn't
blow up as invalid FTS5 query syntax."""
escaped = raw_query.replace('"', '""')
return f'"{escaped}"'
class SQLiteFTSBackend(SearchBackend):
def search(self, query: str, user, limit: int = 50):
if not query.strip():
return []
feed_ids = list(Subscription.objects.filter(user=user).values_list("feed_id", flat=True))
if not feed_ids:
return []
placeholders = ",".join(["%s"] * len(feed_ids))
sql = f"""
SELECT a.id
FROM articles_article a
JOIN articles_fts f ON f.rowid = a.id
WHERE articles_fts MATCH %s AND a.feed_id IN ({placeholders})
ORDER BY rank
LIMIT %s
"""
try:
with connection.cursor() as cursor:
cursor.execute(sql, [_fts_phrase_query(query), *feed_ids, limit])
ordered_ids = [row[0] for row in cursor.fetchall()]
except sqlite3.OperationalError:
return []
if not ordered_ids:
return []
articles_by_id = Article.objects.select_related("feed").in_bulk(ordered_ids)
return [articles_by_id[pk] for pk in ordered_ids if pk in articles_by_id]