fix: close search index consistency gaps (orphan cleanup, discovery indexing, poster sync)

This commit is contained in:
2026-05-12 19:05:22 +02:00
parent 3fc7f914af
commit 2fd8734d23
11 changed files with 141 additions and 12 deletions

View File

@@ -7,6 +7,8 @@ pub mod movie_resolver;
pub mod ports;
pub mod queries;
pub mod use_cases;
pub mod movie_discovery_indexer;
pub mod search_cleanup;
pub use movie_discovery_indexer::MovieDiscoveryIndexer;
pub use search_cleanup::SearchCleanupHandler;

View File

@@ -0,0 +1,51 @@
use std::sync::Arc;
use async_trait::async_trait;
use domain::{
errors::DomainError,
events::DomainEvent,
models::IndexableDocument,
ports::{EventHandler, MovieRepository, SearchCommand},
};
/// Reacts to `MovieDiscovered` and inserts a bare search index entry immediately,
/// so movies are findable before TMDb enrichment runs.
/// Enrichment will later overwrite this with the full document (cast, genres, etc.).
pub struct MovieDiscoveryIndexer {
movie_repository: Arc<dyn MovieRepository>,
search_command: Arc<dyn SearchCommand>,
}
impl MovieDiscoveryIndexer {
pub fn new(movie_repository: Arc<dyn MovieRepository>, search_command: Arc<dyn SearchCommand>) -> Self {
Self { movie_repository, search_command }
}
}
#[async_trait]
impl EventHandler for MovieDiscoveryIndexer {
async fn handle(&self, event: &DomainEvent) -> Result<(), DomainError> {
let movie_id = match event {
DomainEvent::MovieDiscovered { movie_id, .. } => movie_id.clone(),
_ => return Ok(()),
};
let Some(movie) = self.movie_repository.get_movie_by_id(&movie_id).await? else {
tracing::warn!(movie_id = %movie_id.value(), "MovieDiscoveryIndexer: movie not found");
return Ok(());
};
if let Err(e) = self.search_command
.index(IndexableDocument::Movie {
id: movie_id.clone(),
movie: Box::new(movie),
profile: None,
})
.await
{
tracing::warn!(movie_id = %movie_id.value(), "failed to index movie on discovery: {e}");
}
Ok(())
}
}

View File

@@ -5,16 +5,17 @@ use domain::{
errors::DomainError,
events::DomainEvent,
models::EntityType,
ports::{EventHandler, SearchCommand},
ports::{EventHandler, PersonQuery, SearchCommand},
};
pub struct SearchCleanupHandler {
search_command: Arc<dyn SearchCommand>,
person_query: Arc<dyn PersonQuery>,
}
impl SearchCleanupHandler {
pub fn new(search_command: Arc<dyn SearchCommand>) -> Self {
Self { search_command }
pub fn new(search_command: Arc<dyn SearchCommand>, person_query: Arc<dyn PersonQuery>) -> Self {
Self { search_command, person_query }
}
}
@@ -29,6 +30,20 @@ impl EventHandler for SearchCleanupHandler {
if let Err(e) = self.search_command.remove(EntityType::Movie, &movie_id).await {
tracing::warn!("search cleanup failed for movie {movie_id}: {e}");
}
// Remove persons who have no remaining movie credits (orphaned after cascade delete).
match self.person_query.list_orphaned_persons().await {
Ok(orphans) => {
for person_id in orphans {
let id = person_id.value().to_string();
if let Err(e) = self.search_command.remove(EntityType::Person, &id).await {
tracing::warn!("search cleanup failed for orphaned person {id}: {e}");
}
}
}
Err(e) => tracing::warn!("failed to list orphaned persons after movie {movie_id} deletion: {e}"),
}
Ok(())
}
}

View File

@@ -1,6 +1,7 @@
use domain::{
errors::DomainError,
events::DomainEvent,
models::IndexableDocument,
value_objects::{ExternalMetadataId, MovieId, PosterPath},
};
@@ -53,5 +54,19 @@ pub async fn execute(ctx: &AppContext, cmd: SyncPosterCommand) -> Result<(), Dom
movie.update_poster(poster_path);
ctx.movie_repository.upsert_movie(&movie).await?;
// Refresh search index so the new poster_path is reflected immediately.
// Fetch existing profile if available for a complete index document.
let profile = ctx.movie_profile_repository.get_by_movie_id(&movie_id).await.ok().flatten();
if let Err(e) = ctx.search_command
.index(IndexableDocument::Movie {
id: movie_id.clone(),
movie: Box::new(movie),
profile: profile.map(Box::new),
})
.await
{
tracing::warn!(movie_id = %movie_id.value(), "failed to refresh search index after poster sync: {e}");
}
Ok(())
}