fix(search): drop results whose title shares no query token (TMDB loose-match noise)
This commit is contained in:
1 parent
5df24ff113
commit
87513bb6a8
1 file changed
+23
-1
+23
-1
@@ -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)
|
||||
|
||||
Reference in new issue
Block a user