Browse Source

rebuild corrupt search index (#770)

pull/773/head
Jason Dove 4 years ago committed by GitHub
parent
commit
4ed40acfbe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      CHANGELOG.md
  2. 2
      ErsatzTV.Application/Search/Commands/RebuildSearchIndexHandler.cs
  3. 2
      ErsatzTV.Core/Interfaces/Search/ISearchIndex.cs
  4. 33
      ErsatzTV.Infrastructure/Search/SearchIndex.cs

1
CHANGELOG.md

@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). @@ -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

2
ErsatzTV.Application/Search/Commands/RebuildSearchIndexHandler.cs

@ -35,7 +35,7 @@ public class RebuildSearchIndexHandler : IRequestHandler<RebuildSearchIndex, Uni @@ -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) <

2
ErsatzTV.Core/Interfaces/Search/ISearchIndex.cs

@ -8,7 +8,7 @@ namespace ErsatzTV.Core.Interfaces.Search; @@ -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);

33
ErsatzTV.Infrastructure/Search/SearchIndex.cs

@ -18,6 +18,7 @@ using Lucene.Net.Search; @@ -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 @@ -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 @@ -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 @@ -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)

Loading…
Cancel
Save