Load every items from postgres on meilisearch creation

This commit is contained in:
Zoe Roux 2023-11-01 17:12:06 +01:00
parent 1698de332c
commit 167e2853f0
3 changed files with 37 additions and 13 deletions

View File

@ -103,8 +103,11 @@ namespace Kyoo.Host
.ConfigureContainer(configure)
.Build();
PostgresModule.Initialize(host.Services);
await MeilisearchModule.Initialize(host.Services);
await using (AsyncServiceScope scope = host.Services.CreateAsyncScope())
{
PostgresModule.Initialize(scope.ServiceProvider);
await MeilisearchModule.Initialize(scope.ServiceProvider);
}
await _StartWithHost(host);
}

View File

@ -137,6 +137,27 @@ namespace Kyoo.Meiliseach
await _CreateIndex(client, "items", true);
await _CreateIndex(client, nameof(Episode), false);
await _CreateIndex(client, nameof(Studio), false);
IndexStats info = await client.Index("items").GetStatsAsync();
// If there is no documents in meilisearch, if a db exist and is not empty, add items to meilisearch.
if (info.NumberOfDocuments == 0)
{
ILibraryManager database = provider.GetRequiredService<ILibraryManager>();
SearchManager search = provider.GetRequiredService<SearchManager>();
// This is a naive implementation that absolutly does not care about performances.
// This will run only once on users that already had a database when they upgrade.
foreach (Movie movie in await database.Movies.GetAll(limit: 0))
await search.CreateOrUpdate("items", movie, nameof(Movie));
foreach (Show show in await database.Shows.GetAll(limit: 0))
await search.CreateOrUpdate("items", show, nameof(Show));
foreach (Collection collection in await database.Collections.GetAll(limit: 0))
await search.CreateOrUpdate("items", collection, nameof(Collection));
foreach (Episode episode in await database.Episodes.GetAll(limit: 0))
await search.CreateOrUpdate(nameof(Episode), episode);
foreach (Studio studio in await database.Studios.GetAll(limit: 0))
await search.CreateOrUpdate(nameof(Studio), studio);
}
}
private static async Task _CreateIndex(MeilisearchClient client, string index, bool hasKind)

View File

@ -51,26 +51,26 @@ public class SearchManager : ISearchManager
_client = client;
_libraryManager = libraryManager;
IRepository<Movie>.OnCreated += (x) => _CreateOrUpdate("items", x, nameof(Movie));
IRepository<Movie>.OnEdited += (x) => _CreateOrUpdate("items", x, nameof(Movie));
IRepository<Movie>.OnCreated += (x) => CreateOrUpdate("items", x, nameof(Movie));
IRepository<Movie>.OnEdited += (x) => CreateOrUpdate("items", x, nameof(Movie));
IRepository<Movie>.OnDeleted += (x) => _Delete("items", x.Id, nameof(Movie));
IRepository<Show>.OnCreated += (x) => _CreateOrUpdate("items", x, nameof(Show));
IRepository<Show>.OnEdited += (x) => _CreateOrUpdate("items", x, nameof(Show));
IRepository<Show>.OnCreated += (x) => CreateOrUpdate("items", x, nameof(Show));
IRepository<Show>.OnEdited += (x) => CreateOrUpdate("items", x, nameof(Show));
IRepository<Show>.OnDeleted += (x) => _Delete("items", x.Id, nameof(Show));
IRepository<Collection>.OnCreated += (x) => _CreateOrUpdate("items", x, nameof(Collection));
IRepository<Collection>.OnEdited += (x) => _CreateOrUpdate("items", x, nameof(Collection));
IRepository<Collection>.OnCreated += (x) => CreateOrUpdate("items", x, nameof(Collection));
IRepository<Collection>.OnEdited += (x) => CreateOrUpdate("items", x, nameof(Collection));
IRepository<Collection>.OnDeleted += (x) => _Delete("items", x.Id, nameof(Collection));
IRepository<Episode>.OnCreated += (x) => _CreateOrUpdate(nameof(Episode), x);
IRepository<Episode>.OnEdited += (x) => _CreateOrUpdate(nameof(Episode), x);
IRepository<Episode>.OnCreated += (x) => CreateOrUpdate(nameof(Episode), x);
IRepository<Episode>.OnEdited += (x) => CreateOrUpdate(nameof(Episode), x);
IRepository<Episode>.OnDeleted += (x) => _Delete(nameof(Episode), x.Id);
IRepository<Studio>.OnCreated += (x) => _CreateOrUpdate(nameof(Studio), x);
IRepository<Studio>.OnEdited += (x) => _CreateOrUpdate(nameof(Studio), x);
IRepository<Studio>.OnCreated += (x) => CreateOrUpdate(nameof(Studio), x);
IRepository<Studio>.OnEdited += (x) => CreateOrUpdate(nameof(Studio), x);
IRepository<Studio>.OnDeleted += (x) => _Delete(nameof(Studio), x.Id);
}
private async Task _CreateOrUpdate(string index, IResource item, string? kind = null)
public async Task CreateOrUpdate(string index, IResource item, string? kind = null)
{
if (kind != null)
{