feat(search): rank titles and filter by kind (#274)
ci / web (push) Successful in 1m27s
ci / rust (push) Successful in 2m46s
e2e / e2e (push) Failing after 1m48s
ci / image (push) Successful in 2m49s

This commit was merged in pull request #274.
This commit is contained in:
2026-09-20 18:12:37 +01:00
parent 3c6c07843e
commit dab3c06dd7
5 changed files with 401 additions and 92 deletions
+282 -75
View File
@@ -192,44 +192,7 @@ pub async fn search(
}
let database = state.database().ok_or(ApiError::Unavailable)?;
let tokens = like_tokens(input);
// §9.2 keeps the two result sets grouped and the library first, so each
// kind lands in its own block: movies, then series.
let mut library: Vec<LibraryResult> = if matches!(kind, SearchInputKind::TmdbId) {
let tmdb_id = input
.strip_prefix("tmdb:")
.unwrap_or(input)
.trim()
.parse::<i64>()
.map_err(|_| ApiError::Invalid("invalid TMDB id".into()))?;
sqlx::query_as!(Movie, r#"SELECT id AS "id!: i64", tmdb_id AS "tmdb_id!: i64", title AS "title!: String", year, original_language, root_id AS "root_id!: i64", wanted AS "wanted!: bool", overrides AS "overrides!: serde_json::Value", state AS "state!: String", blocked AS "blocked!: bool", search_attempts AS "search_attempts!: i64", last_searched_at, poster_path, vote_average, (SELECT f.waiver FROM media_files f WHERE f.owner_kind = 'movie' AND f.owner_id = movies.id AND f.waiver IS NOT NULL ORDER BY f.id LIMIT 1) AS "waiver?: serde_json::Value" FROM movies WHERE tmdb_id = ? OR NOT EXISTS (SELECT 1 FROM json_each(?) token WHERE title NOT LIKE '%' || token.value || '%' ESCAPE '\') ORDER BY title, year, id"#, tmdb_id, tokens)
.fetch_all(database.pool()).await?
.into_iter()
.map(LibraryResult::Movie)
.collect()
} else {
sqlx::query_as!(Movie, r#"SELECT id AS "id!: i64", tmdb_id AS "tmdb_id!: i64", title AS "title!: String", year, original_language, root_id AS "root_id!: i64", wanted AS "wanted!: bool", overrides AS "overrides!: serde_json::Value", state AS "state!: String", blocked AS "blocked!: bool", search_attempts AS "search_attempts!: i64", last_searched_at, poster_path, vote_average, (SELECT f.waiver FROM media_files f WHERE f.owner_kind = 'movie' AND f.owner_id = movies.id AND f.waiver IS NOT NULL ORDER BY f.id LIMIT 1) AS "waiver?: serde_json::Value" FROM movies WHERE NOT EXISTS (SELECT 1 FROM json_each(?) token WHERE title NOT LIKE '%' || token.value || '%' ESCAPE '\') ORDER BY title, year, id"#, tokens)
.fetch_all(database.pool()).await?
.into_iter()
.map(LibraryResult::Movie)
.collect()
};
let series_rows = if matches!(kind, SearchInputKind::TmdbId) {
let tmdb_id = input
.strip_prefix("tmdb:")
.unwrap_or(input)
.trim()
.parse::<i64>()
.map_err(|_| ApiError::Invalid("invalid TMDB id".into()))?;
sqlx::query_as!(LibrarySeries, r#"SELECT s.id AS "id!: i64", s.tmdb_id AS "tmdb_id!: i64", s.title AS "title!: String", s.year, s.original_language, s.root_id AS "root_id!: i64", s.blocked AS "blocked!: bool", s.poster_path, s.vote_average FROM series s WHERE s.tmdb_id = ? OR NOT EXISTS (SELECT 1 FROM json_each(?) token WHERE s.title NOT LIKE '%' || token.value || '%' ESCAPE '\') ORDER BY s.title, s.year, s.id"#, tmdb_id, tokens)
.fetch_all(database.pool()).await?
} else {
sqlx::query_as!(LibrarySeries, r#"SELECT s.id AS "id!: i64", s.tmdb_id AS "tmdb_id!: i64", s.title AS "title!: String", s.year, s.original_language, s.root_id AS "root_id!: i64", s.blocked AS "blocked!: bool", s.poster_path, s.vote_average FROM series s WHERE NOT EXISTS (SELECT 1 FROM json_each(?) token WHERE s.title NOT LIKE '%' || token.value || '%' ESCAPE '\') ORDER BY s.title, s.year, s.id"#, tokens)
.fetch_all(database.pool()).await?
};
library.extend(series_rows.into_iter().map(LibraryResult::Series));
let mut library = library_hits(database, kind, input).await?;
// A degraded TMDB must not discard the library results already in hand
// (issue #168): the group goes empty and `tmdb_status` says why, so the
@@ -253,28 +216,19 @@ pub async fn search(
Err(error) => return Err(error),
};
if matches!(kind, SearchInputKind::ImdbId) {
for result in &results {
match result {
TmdbResult::Movie(movie) => {
let tmdb_id = i64::from(movie.tmdb_id);
if let Some(found) = sqlx::query_as!(Movie, r#"SELECT id AS "id!: i64", tmdb_id AS "tmdb_id!: i64", title AS "title!: String", year, original_language, root_id AS "root_id!: i64", wanted AS "wanted!: bool", overrides AS "overrides!: serde_json::Value", state AS "state!: String", blocked AS "blocked!: bool", search_attempts AS "search_attempts!: i64", last_searched_at, poster_path, vote_average, (SELECT f.waiver FROM media_files f WHERE f.owner_kind = 'movie' AND f.owner_id = movies.id AND f.waiver IS NOT NULL ORDER BY f.id LIMIT 1) AS "waiver?: serde_json::Value" FROM movies WHERE tmdb_id = ?"#, tmdb_id)
.fetch_optional(database.pool()).await?
{
library.push(LibraryResult::Movie(found));
}
}
TmdbResult::Series(series) => {
let tmdb_id = i64::from(series.tmdb_id);
if let Some(found) = sqlx::query_as!(LibrarySeries, r#"SELECT s.id AS "id!: i64", s.tmdb_id AS "tmdb_id!: i64", s.title AS "title!: String", s.year, s.original_language, s.root_id AS "root_id!: i64", s.blocked AS "blocked!: bool", s.poster_path, s.vote_average FROM series s WHERE s.tmdb_id = ?"#, tmdb_id)
.fetch_optional(database.pool()).await?
{
library.push(LibraryResult::Series(found));
}
}
}
}
push_tracked(database, &results, &mut library).await?;
}
// The SQL orders each half by title so the two halves merge predictably;
// the merged set is then ranked against the query, same as the TMDB one.
let query_tokens = tokens(input);
library.sort_by(|left, right| {
let (left, right) = (library_title(left), library_title(right));
match_tier(&tokens(left), &query_tokens)
.cmp(&match_tier(&tokens(right), &query_tokens))
.then_with(|| left.cmp(right))
});
Ok(Json(SearchResponse {
kind,
library,
@@ -284,6 +238,89 @@ pub async fn search(
}))
}
/// Every tracked title the query names (§9.2): movies, then series, each
/// ordered by title so the merge below has something stable to rank.
async fn library_hits(
database: &arr_db::Db,
kind: SearchInputKind,
input: &str,
) -> Result<Vec<LibraryResult>, ApiError> {
let like_tokens_json = like_tokens(input);
let mut library: Vec<LibraryResult> = if matches!(kind, SearchInputKind::TmdbId) {
let tmdb_id = input
.strip_prefix("tmdb:")
.unwrap_or(input)
.trim()
.parse::<i64>()
.map_err(|_| ApiError::Invalid("invalid TMDB id".into()))?;
sqlx::query_as!(Movie, r#"SELECT id AS "id!: i64", tmdb_id AS "tmdb_id!: i64", title AS "title!: String", year, original_language, root_id AS "root_id!: i64", wanted AS "wanted!: bool", overrides AS "overrides!: serde_json::Value", state AS "state!: String", blocked AS "blocked!: bool", search_attempts AS "search_attempts!: i64", last_searched_at, poster_path, vote_average, (SELECT f.waiver FROM media_files f WHERE f.owner_kind = 'movie' AND f.owner_id = movies.id AND f.waiver IS NOT NULL ORDER BY f.id LIMIT 1) AS "waiver?: serde_json::Value" FROM movies WHERE tmdb_id = ? OR NOT EXISTS (SELECT 1 FROM json_each(?) token WHERE title NOT LIKE '%' || token.value || '%' ESCAPE '\') ORDER BY title, year, id"#, tmdb_id, like_tokens_json)
.fetch_all(database.pool()).await?
.into_iter()
.map(LibraryResult::Movie)
.collect()
} else {
sqlx::query_as!(Movie, r#"SELECT id AS "id!: i64", tmdb_id AS "tmdb_id!: i64", title AS "title!: String", year, original_language, root_id AS "root_id!: i64", wanted AS "wanted!: bool", overrides AS "overrides!: serde_json::Value", state AS "state!: String", blocked AS "blocked!: bool", search_attempts AS "search_attempts!: i64", last_searched_at, poster_path, vote_average, (SELECT f.waiver FROM media_files f WHERE f.owner_kind = 'movie' AND f.owner_id = movies.id AND f.waiver IS NOT NULL ORDER BY f.id LIMIT 1) AS "waiver?: serde_json::Value" FROM movies WHERE NOT EXISTS (SELECT 1 FROM json_each(?) token WHERE title NOT LIKE '%' || token.value || '%' ESCAPE '\') ORDER BY title, year, id"#, like_tokens_json)
.fetch_all(database.pool()).await?
.into_iter()
.map(LibraryResult::Movie)
.collect()
};
let series_rows = if matches!(kind, SearchInputKind::TmdbId) {
let tmdb_id = input
.strip_prefix("tmdb:")
.unwrap_or(input)
.trim()
.parse::<i64>()
.map_err(|_| ApiError::Invalid("invalid TMDB id".into()))?;
sqlx::query_as!(LibrarySeries, r#"SELECT s.id AS "id!: i64", s.tmdb_id AS "tmdb_id!: i64", s.title AS "title!: String", s.year, s.original_language, s.root_id AS "root_id!: i64", s.blocked AS "blocked!: bool", s.poster_path, s.vote_average FROM series s WHERE s.tmdb_id = ? OR NOT EXISTS (SELECT 1 FROM json_each(?) token WHERE s.title NOT LIKE '%' || token.value || '%' ESCAPE '\') ORDER BY s.title, s.year, s.id"#, tmdb_id, like_tokens_json)
.fetch_all(database.pool()).await?
} else {
sqlx::query_as!(LibrarySeries, r#"SELECT s.id AS "id!: i64", s.tmdb_id AS "tmdb_id!: i64", s.title AS "title!: String", s.year, s.original_language, s.root_id AS "root_id!: i64", s.blocked AS "blocked!: bool", s.poster_path, s.vote_average FROM series s WHERE NOT EXISTS (SELECT 1 FROM json_each(?) token WHERE s.title NOT LIKE '%' || token.value || '%' ESCAPE '\') ORDER BY s.title, s.year, s.id"#, like_tokens_json)
.fetch_all(database.pool()).await?
};
library.extend(series_rows.into_iter().map(LibraryResult::Series));
Ok(library)
}
/// §9.2: an `IMDb` id names one title, so what the library set owes the
/// operator is whether TMDB's answer is already tracked.
async fn push_tracked(
database: &arr_db::Db,
results: &[TmdbResult],
library: &mut Vec<LibraryResult>,
) -> Result<(), ApiError> {
for result in results {
match result {
TmdbResult::Movie(movie) => {
let tmdb_id = i64::from(movie.tmdb_id);
if let Some(found) = sqlx::query_as!(Movie, r#"SELECT id AS "id!: i64", tmdb_id AS "tmdb_id!: i64", title AS "title!: String", year, original_language, root_id AS "root_id!: i64", wanted AS "wanted!: bool", overrides AS "overrides!: serde_json::Value", state AS "state!: String", blocked AS "blocked!: bool", search_attempts AS "search_attempts!: i64", last_searched_at, poster_path, vote_average, (SELECT f.waiver FROM media_files f WHERE f.owner_kind = 'movie' AND f.owner_id = movies.id AND f.waiver IS NOT NULL ORDER BY f.id LIMIT 1) AS "waiver?: serde_json::Value" FROM movies WHERE tmdb_id = ?"#, tmdb_id)
.fetch_optional(database.pool()).await?
{
library.push(LibraryResult::Movie(found));
}
}
TmdbResult::Series(series) => {
let tmdb_id = i64::from(series.tmdb_id);
if let Some(found) = sqlx::query_as!(LibrarySeries, r#"SELECT s.id AS "id!: i64", s.tmdb_id AS "tmdb_id!: i64", s.title AS "title!: String", s.year, s.original_language, s.root_id AS "root_id!: i64", s.blocked AS "blocked!: bool", s.poster_path, s.vote_average FROM series s WHERE s.tmdb_id = ?"#, tmdb_id)
.fetch_optional(database.pool()).await?
{
library.push(LibraryResult::Series(found));
}
}
}
}
Ok(())
}
fn library_title(result: &LibraryResult) -> &str {
match result {
LibraryResult::Movie(movie) => &movie.title,
LibraryResult::Series(series) => &series.title,
}
}
/// Movie and series TMDB ids live in separate namespaces, so deduplication
/// keys on the kind as well as the id.
fn already_matched(matches: &[TmdbResult], candidate: &TmdbResult) -> bool {
@@ -376,28 +413,52 @@ async fn search_tmdb(
);
results
}
SearchInputKind::Text => {
let mut results: Vec<TmdbResult> = tmdb
.search_movies(input, None)
.await
.map_err(|error| upstream_error(&error))?
.into_iter()
.map(|movie| TmdbResult::Movie(movie.into()))
.collect();
results.extend(
tmdb.search_series(input)
.await
.map_err(|error| upstream_error(&error))?
.into_iter()
.map(|series| TmdbResult::Series(series.into())),
);
results
}
SearchInputKind::Text => search_text(tmdb, input).await?,
SearchInputKind::Magnet | SearchInputKind::TorrentUrl => Vec::new(),
};
Ok(results)
}
/// Both TMDB endpoints for one title query, merged and ranked together.
async fn search_text(
tmdb: &arr_meta::TmdbClient,
input: &str,
) -> Result<Vec<TmdbResult>, ApiError> {
let query = tokens(input);
let mut ranked: Vec<(Relevance, TmdbResult)> = tmdb
.search_movies(input, None)
.await
.map_err(|error| upstream_error(&error))?
.into_iter()
.map(|movie| {
let relevance = Relevance::of(
&[&movie.title, &movie.original_title],
movie.popularity,
movie.vote_count,
&query,
);
(relevance, TmdbResult::Movie(movie.into()))
})
.collect();
ranked.extend(
tmdb.search_series(input)
.await
.map_err(|error| upstream_error(&error))?
.into_iter()
.map(|series| {
let relevance = Relevance::of(
&[&series.title],
series.popularity,
series.vote_count,
&query,
);
(relevance, TmdbResult::Series(series.into()))
}),
);
ranked.sort_by(|(left, _), (right, _)| left.order(right));
Ok(ranked.into_iter().map(|(_, result)| result).collect())
}
#[utoipa::path(
get, path = "/api/releases", tag = "search", params(ReleasesQuery),
responses(
@@ -656,6 +717,79 @@ fn input_kind(input: &str) -> SearchInputKind {
}
}
/// Where one result sits against the query that found it.
///
/// TMDB answers movies and series on two endpoints, each sorted on its own.
/// Concatenating them buries a series behind every loosely-matching film —
/// "dexter" put *Dexter: Resurrection* below twenty Dexter Gordon concert
/// recordings. Both halves are ranked together instead: how squarely the
/// title matches first, then TMDB's own popularity.
struct Relevance {
tier: u8,
popularity: f64,
vote_count: u32,
}
/// Matched some way TMDB understood and these tiers do not.
const TIER_OTHER: u8 = 4;
impl Relevance {
/// The best tier any of the title's spellings earns — a film listed
/// under its original title should not be punished for the translation.
fn of(titles: &[&str], popularity: f64, vote_count: u32, query: &[String]) -> Self {
let tier = titles
.iter()
.map(|title| match_tier(&tokens(title), query))
.min()
.unwrap_or(TIER_OTHER);
Self {
tier,
popularity,
vote_count,
}
}
fn order(&self, other: &Self) -> std::cmp::Ordering {
self.tier
.cmp(&other.tier)
.then_with(|| other.popularity.total_cmp(&self.popularity))
.then_with(|| other.vote_count.cmp(&self.vote_count))
}
}
/// How squarely `title` answers `query`, lower being better: the whole
/// title, the start of it, every query word somewhere in it, every query
/// word starting one of its words, or none of those.
fn match_tier(title: &[String], query: &[String]) -> u8 {
if query.is_empty() {
return TIER_OTHER;
}
if title == query {
0
} else if title.starts_with(query) {
1
} else if query.iter().all(|word| title.contains(word)) {
2
} else if query
.iter()
.all(|word| title.iter().any(|token| token.starts_with(word.as_str())))
{
3
} else {
TIER_OTHER
}
}
/// Lowercased words, with punctuation dropped: "Dexter: Resurrection" and
/// "dexter resurrection" are the same query.
fn tokens(value: &str) -> Vec<String> {
value
.split(|character: char| !character.is_alphanumeric())
.filter(|token| !token.is_empty())
.map(str::to_lowercase)
.collect()
}
/// A multi-word title query needs word-wise matching, not one literal
/// phrase. The query's tokens travel as a JSON array the queries walk with
/// `json_each`; LIKE metacharacters are escaped here rather than in SQL.
@@ -1600,6 +1734,79 @@ mod tests {
assert_ne!(classified.score, core_score.total);
}
/// A series is answered on TMDB's other endpoint, so concatenating the
/// two halves buried it behind every film that merely shares a word.
/// The merged set leads with the titles that answer the query.
#[tokio::test]
async fn text_results_rank_across_both_tmdb_endpoints() {
let tmdb = MockServer::start().await;
let prowlarr = MockServer::start().await;
Mock::given(method("GET"))
.and(path("/search/movie"))
.respond_with(
ResponseTemplate::new(200).set_body_json(serde_json::json!({"results":[
{"id": 1, "title": "Dexter Gordon: More Than You Know",
"original_title": "Dexter Gordon: More Than You Know",
"original_language": "en", "popularity": 0.4},
{"id": 2, "title": "Dexter's Laboratory: Ego Trip",
"original_title": "Dexter's Laboratory: Ego Trip",
"original_language": "en", "popularity": 3.2}
]})),
)
.mount(&tmdb)
.await;
Mock::given(method("GET"))
.and(path("/search/tv"))
.respond_with(
ResponseTemplate::new(200).set_body_json(serde_json::json!({"results":[
{"id": 3, "name": "Dexter: Resurrection", "original_language": "en",
"first_air_date": "2025-07-11", "popularity": 90.0},
{"id": 4, "name": "Dexter", "original_language": "en",
"first_air_date": "2006-10-01", "popularity": 120.0}
]})),
)
.mount(&tmdb)
.await;
let (_dir, _state, base) = application(&tmdb, &prowlarr).await;
let response: serde_json::Value = reqwest::get(format!("{base}/api/search?q=dexter"))
.await
.expect("search")
.json()
.await
.expect("json");
let order: Vec<&str> = response["tmdb"]
.as_array()
.expect("tmdb")
.iter()
.map(|hit| hit["title"].as_str().expect("title"))
.collect();
assert_eq!(
order,
[
"Dexter",
"Dexter: Resurrection",
"Dexter's Laboratory: Ego Trip",
"Dexter Gordon: More Than You Know",
]
);
}
/// Punctuation is not part of the query: "dexter resurrection" and
/// "Dexter: Resurrection" are the same two words.
#[test]
fn punctuation_does_not_break_a_title_match() {
let query = tokens("dexter resurrection");
assert_eq!(match_tier(&tokens("Dexter: Resurrection"), &query), 0);
assert_eq!(
match_tier(&tokens("Dexter: Resurrection (2025)"), &query),
1
);
assert_eq!(match_tier(&tokens("Resurrection of Dexter"), &query), 2);
assert_eq!(match_tier(&tokens("Dexter's Resurrections"), &query), 3);
assert_eq!(match_tier(&tokens("Dexter"), &query), TIER_OTHER);
}
#[test]
fn manual_inputs_skip_title_upstreams() {
assert!(matches!(