feat(feed): per-user search finds in Mine scope + full-text relevance search
Two related search improvements: 1) Your own live-search results now belong to your Mine feed. A new per-user search_finds table (migration 0030) records each video you surface via your YouTube search (the route inserts them idempotently). The Mine feed becomes 'your non-hidden subscriptions OR your search finds', and the Source filter now applies in Mine too: organic = subscriptions, search = your search finds, all = both (default stays organic, so the main feed is unchanged). The shared Library keeps using the global via_search flag. 2) Feed search ranks by relevance instead of a whole-phrase substring. A custom unaccent_simple text-search config + GIN index (migration 0031) back a YouTube-like fuzzy match: word-order-independent, multi-word AND, prefix on the word being typed, accent-insensitive. A new 'relevance' sort orders by ts_rank; the channel name still matches as a substring. The rank is scaled to an integer so the keyset cursor pages it exactly (a raw float4 breaks paging). _filtered_query returns the rank expr so only the feed list uses it.
This commit is contained in:
parent
a4abfa402f
commit
6cef826ecc
5 changed files with 208 additions and 22 deletions
47
backend/alembic/versions/0030_search_finds.py
Normal file
47
backend/alembic/versions/0030_search_finds.py
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
"""per-user search finds
|
||||
|
||||
Revision ID: 0030_search_finds
|
||||
Revises: 0029_unaccent_search
|
||||
Create Date: 2026-06-30
|
||||
|
||||
Adds the `search_finds` table: a per-user record of which videos a user surfaced via their own
|
||||
live YouTube search. This lets "videos I searched for" appear in that user's Mine feed (and the
|
||||
Mine Source filter), independent of the global `videos.via_search` flag (which marks a video as
|
||||
search-discovered by anyone, for the shared Library's Source filter).
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
revision: str = "0030_search_finds"
|
||||
down_revision: Union[str, None] = "0029_unaccent_search"
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.create_table(
|
||||
"search_finds",
|
||||
sa.Column("id", sa.Integer(), nullable=False),
|
||||
sa.Column("user_id", sa.Integer(), nullable=False),
|
||||
sa.Column("video_id", sa.String(), nullable=False),
|
||||
sa.Column(
|
||||
"created_at",
|
||||
sa.DateTime(timezone=True),
|
||||
server_default=sa.text("now()"),
|
||||
nullable=False,
|
||||
),
|
||||
sa.ForeignKeyConstraint(["user_id"], ["users.id"], ondelete="CASCADE"),
|
||||
sa.ForeignKeyConstraint(["video_id"], ["videos.id"], ondelete="CASCADE"),
|
||||
sa.PrimaryKeyConstraint("id"),
|
||||
sa.UniqueConstraint("user_id", "video_id", name="uq_user_search_video"),
|
||||
)
|
||||
op.create_index("ix_search_finds_user_id", "search_finds", ["user_id"])
|
||||
op.create_index("ix_search_finds_video_id", "search_finds", ["video_id"])
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.drop_index("ix_search_finds_video_id", table_name="search_finds")
|
||||
op.drop_index("ix_search_finds_user_id", table_name="search_finds")
|
||||
op.drop_table("search_finds")
|
||||
49
backend/alembic/versions/0031_title_fts.py
Normal file
49
backend/alembic/versions/0031_title_fts.py
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
"""full-text relevance search on video titles
|
||||
|
||||
Revision ID: 0031_title_fts
|
||||
Revises: 0030_search_finds
|
||||
Create Date: 2026-06-30
|
||||
|
||||
Adds a PostgreSQL full-text search index over video titles so the feed search box can rank by
|
||||
relevance (a YouTube-like "fuzzy" search: word-order-independent, multi-word AND, prefix on the
|
||||
word being typed) instead of a whole-phrase substring match.
|
||||
|
||||
A custom text-search configuration `unaccent_simple` = the `simple` config (no stemming, no
|
||||
stopwords — right for a multilingual HU/EN/DE catalog) plus the `unaccent` dictionary, so the
|
||||
search stays accent-insensitive ("tiesto" matches "Tiësto"). A GIN expression index on
|
||||
`to_tsvector('public.unaccent_simple', coalesce(title,''))` makes the @@ match fast; the feed
|
||||
query uses the exact same expression so the planner picks the index. `unaccent` is already
|
||||
enabled (migration 0029). The config-based 2-arg to_tsvector is IMMUTABLE, so it's index-safe.
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
|
||||
revision: str = "0031_title_fts"
|
||||
down_revision: Union[str, None] = "0030_search_finds"
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.execute(
|
||||
"CREATE TEXT SEARCH CONFIGURATION public.unaccent_simple (COPY = pg_catalog.simple)"
|
||||
)
|
||||
op.execute(
|
||||
"""
|
||||
ALTER TEXT SEARCH CONFIGURATION public.unaccent_simple
|
||||
ALTER MAPPING FOR asciiword, asciihword, hword_asciipart, word, hword, hword_part
|
||||
WITH unaccent, simple
|
||||
"""
|
||||
)
|
||||
op.execute(
|
||||
"""
|
||||
CREATE INDEX ix_videos_title_fts ON videos
|
||||
USING gin (to_tsvector('public.unaccent_simple', coalesce(title, '')))
|
||||
"""
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.execute("DROP INDEX IF EXISTS ix_videos_title_fts")
|
||||
op.execute("DROP TEXT SEARCH CONFIGURATION IF EXISTS public.unaccent_simple")
|
||||
Loading…
Add table
Add a link
Reference in a new issue