From 4ed40acfbe1aff91f357305adee5c31707d62069 Mon Sep 17 00:00:00 2001 From: Jason Dove Date: Thu, 28 Apr 2022 13:49:06 -0500 Subject: [PATCH] rebuild corrupt search index (#770) --- CHANGELOG.md | 1 + .../Commands/RebuildSearchIndexHandler.cs | 2 +- .../Interfaces/Search/ISearchIndex.cs | 2 +- ErsatzTV.Infrastructure/Search/SearchIndex.cs | 33 +++++++++++++++++-- 4 files changed, 34 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 60da6e6eb..cbb0d82e4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Fix health check crash when trash contains a show or a season - Fix ability of health check crash to crash home page - Remove and ignore Season 0/Specials from Plex shows that have no specials +- Automatically delete and rebuild the search index on startup if it has become corrupt ### Changed - Update Plex, Jellyfin and Emby movie and show library scanners to share a significant amount of code diff --git a/ErsatzTV.Application/Search/Commands/RebuildSearchIndexHandler.cs b/ErsatzTV.Application/Search/Commands/RebuildSearchIndexHandler.cs index 412449dc8..6129680c3 100644 --- a/ErsatzTV.Application/Search/Commands/RebuildSearchIndexHandler.cs +++ b/ErsatzTV.Application/Search/Commands/RebuildSearchIndexHandler.cs @@ -35,7 +35,7 @@ public class RebuildSearchIndexHandler : IRequestHandler(ConfigElementKey.SearchIndexVersion) < diff --git a/ErsatzTV.Core/Interfaces/Search/ISearchIndex.cs b/ErsatzTV.Core/Interfaces/Search/ISearchIndex.cs index 4607128d8..329c9bb0f 100644 --- a/ErsatzTV.Core/Interfaces/Search/ISearchIndex.cs +++ b/ErsatzTV.Core/Interfaces/Search/ISearchIndex.cs @@ -8,7 +8,7 @@ namespace ErsatzTV.Core.Interfaces.Search; public interface ISearchIndex : IDisposable { public int Version { get; } - Task Initialize(ILocalFileSystem localFileSystem); + Task Initialize(ILocalFileSystem localFileSystem, IConfigElementRepository configElementRepository); Task Rebuild(ISearchRepository searchRepository); Task RebuildItems(ISearchRepository searchRepository, List itemIds); Task UpdateItems(ISearchRepository searchRepository, List items); diff --git a/ErsatzTV.Infrastructure/Search/SearchIndex.cs b/ErsatzTV.Infrastructure/Search/SearchIndex.cs index 0aab72cdd..1b5afef39 100644 --- a/ErsatzTV.Infrastructure/Search/SearchIndex.cs +++ b/ErsatzTV.Infrastructure/Search/SearchIndex.cs @@ -18,6 +18,7 @@ using Lucene.Net.Search; using Lucene.Net.Store; using Lucene.Net.Util; using Microsoft.Extensions.Logging; +using Directory = System.IO.Directory; using Query = Lucene.Net.Search.Query; namespace ErsatzTV.Infrastructure.Search; @@ -86,12 +87,22 @@ public sealed class SearchIndex : ISearchIndex public int Version => 22; - public Task Initialize(ILocalFileSystem localFileSystem) + public async Task Initialize( + ILocalFileSystem localFileSystem, + IConfigElementRepository configElementRepository) { if (!_initialized) { localFileSystem.EnsureFolderExists(FileSystemLayout.SearchIndexFolder); + if (!ValidateDirectory(FileSystemLayout.SearchIndexFolder)) + { + _logger.LogWarning("Search index failed to initialize; will delete and recreate"); + await configElementRepository.Upsert(ConfigElementKey.SearchIndexVersion, 0); + Directory.Delete(FileSystemLayout.SearchIndexFolder, true); + localFileSystem.EnsureFolderExists(FileSystemLayout.SearchIndexFolder); + } + _directory = FSDirectory.Open(FileSystemLayout.SearchIndexFolder); var analyzer = new StandardAnalyzer(AppLuceneVersion); var indexConfig = new IndexWriterConfig(AppLuceneVersion, analyzer) @@ -100,7 +111,7 @@ public sealed class SearchIndex : ISearchIndex _initialized = true; } - return Task.FromResult(_initialized); + return _initialized; } public async Task UpdateItems(ISearchRepository searchRepository, List items) @@ -228,6 +239,24 @@ public sealed class SearchIndex : ISearchIndex return Unit.Default; } + private bool ValidateDirectory(string folder) + { + try + { + using var d = FSDirectory.Open(folder); + var analyzer = new StandardAnalyzer(AppLuceneVersion); + var indexConfig = new IndexWriterConfig(AppLuceneVersion, analyzer) + { OpenMode = OpenMode.CREATE_OR_APPEND }; + using var w = new IndexWriter(_directory, indexConfig); + using DirectoryReader r = w.GetReader(true); + return true; + } + catch + { + return false; + } + } + private async Task RebuildItem(ISearchRepository searchRepository, MediaItem mediaItem) { switch (mediaItem)