rebuild corrupt search index (#770)

This commit is contained in:
Jason Dove
2022-04-28 13:49:06 -05:00
committed by GitHub
parent 17f540dc99
commit 4ed40acfbe
4 changed files with 34 additions and 4 deletions
+1
View File
@@ -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
@@ -35,7 +35,7 @@ public class RebuildSearchIndexHandler : IRequestHandler<RebuildSearchIndex, Uni
{
bool indexFolderExists = Directory.Exists(FileSystemLayout.SearchIndexFolder);
await _searchIndex.Initialize(_localFileSystem);
await _searchIndex.Initialize(_localFileSystem, _configElementRepository);
if (!indexFolderExists ||
await _configElementRepository.GetValue<int>(ConfigElementKey.SearchIndexVersion) <
@@ -8,7 +8,7 @@ namespace ErsatzTV.Core.Interfaces.Search;
public interface ISearchIndex : IDisposable
{
public int Version { get; }
Task<bool> Initialize(ILocalFileSystem localFileSystem);
Task<bool> Initialize(ILocalFileSystem localFileSystem, IConfigElementRepository configElementRepository);
Task<Unit> Rebuild(ISearchRepository searchRepository);
Task<Unit> RebuildItems(ISearchRepository searchRepository, List<int> itemIds);
Task<Unit> UpdateItems(ISearchRepository searchRepository, List<MediaItem> items);
+31 -2
View File
@@ -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<bool> Initialize(ILocalFileSystem localFileSystem)
public async Task<bool> 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<Unit> UpdateItems(ISearchRepository searchRepository, List<MediaItem> 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)