From 87513bb6a8c699a43feff6b5d2230a4072d43e25 Mon Sep 17 00:00:00 2001 From: ner Date: Fri, 23 Oct 2026 21:21:55 +0300 Subject: [PATCH] fix(search): drop results whose title shares no query token (TMDB loose-match noise) --- data/queries/search.ts | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/data/queries/search.ts b/data/queries/search.ts index 9ae1242..b518609 100644 --- a/data/queries/search.ts +++ b/data/queries/search.ts @@ -12,6 +12,25 @@ const weightedRating = (show: Show) => (show.votes * show.rating + MIN_VOTES * M const byRating = (a: Show, b: Show) => weightedRating(b) - weightedRating(a) || b.votes - a.votes || a.title.localeCompare(b.title); +// TMDB search is loose (e.g. "dune" matches the French "Anatomie d'une chute"). Drop results whose +// title shares no query token, so a zero-overlap match can't ride its rating into the top of the list. +const normalize = (value: string) => + value + .toLowerCase() + .normalize('NFD') + .replace(/[\u0300-\u036f]/g, ''); +const titleKey = (value: string) => normalize(value).replace(/[^a-z0-9]/g, ''); +const queryTokens = (query: string) => + normalize(query) + .split(/[^a-z0-9]+/) + .filter(Boolean); + +const matchesTitle = (show: Show, tokens: string[]) => { + if (!tokens.length) return true; + const title = titleKey(show.title); + return tokens.some((token) => title.includes(token)); +}; + const searchQuery = ({ query }: SearchQueryProps) => ({ queryKey: ['search', query], queryFn: ({ pageParam }: { pageParam: string }) => @@ -21,11 +40,14 @@ const searchQuery = ({ query }: SearchQueryProps) => ({ ]).then(([movies, series]) => { const moviesPage = mapMoviesSeriesResponseToShows(movies, 'movie'); const seriesPage = mapMoviesSeriesResponseToShows(series, 'tv'); + const tokens = queryTokens(query); return { page: Math.max(moviesPage.page, seriesPage.page), total: Math.max(moviesPage.total, seriesPage.total), - results: [...moviesPage.results, ...seriesPage.results].sort(byRating) + results: [...moviesPage.results, ...seriesPage.results] + .filter((show) => matchesTitle(show, tokens)) + .sort(byRating) }; }), enabled: Boolean(query)