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

+2 -19
View File
@@ -1,11 +1,11 @@
from django.contrib.auth.decorators import login_required
from django.db.models import Prefetch
from django.http import Http404
from django.shortcuts import get_object_or_404, render
from django.utils import timezone
from django.views.decorators.http import require_POST
from apps.articles.models import Article, Tag, UserArticleState
from apps.articles.queries import with_user_state
FILTER_CHOICES = [
("unread", "Unread"),
@@ -27,23 +27,6 @@ def _base_queryset(user):
)
def _with_user_state_annotations(queryset, user):
queryset = queryset.prefetch_related(
Prefetch(
"user_states",
queryset=UserArticleState.objects.filter(user=user),
to_attr="my_states",
)
)
articles = list(queryset)
for article in articles:
state = article.my_states[0] if article.my_states else None
article.is_read = state.is_read if state else False
article.is_starred = state.is_starred if state else False
article.is_saved_for_later = state.is_saved_for_later if state else False
return articles
def _get_or_create_state(user, article):
state, _ = UserArticleState.objects.get_or_create(user=user, article=article)
return state
@@ -81,7 +64,7 @@ def stream(request):
start, end = (page - 1) * PAGE_SIZE, page * PAGE_SIZE
total_count = queryset.count()
articles = _with_user_state_annotations(queryset[start:end], request.user)
articles = with_user_state(queryset[start:end], request.user)
context = {
"articles": articles,