diff --git a/back/src/Kyoo.Host/Application.cs b/back/src/Kyoo.Host/Application.cs index b406cd1ed..9263fa6f9 100644 --- a/back/src/Kyoo.Host/Application.cs +++ b/back/src/Kyoo.Host/Application.cs @@ -103,8 +103,11 @@ public async Task Start(string[] args, Action configure) .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); } diff --git a/back/src/Kyoo.Meilisearch/MeilisearchModule.cs b/back/src/Kyoo.Meilisearch/MeilisearchModule.cs index 642d9617f..56e98a7f6 100644 --- a/back/src/Kyoo.Meilisearch/MeilisearchModule.cs +++ b/back/src/Kyoo.Meilisearch/MeilisearchModule.cs @@ -137,6 +137,27 @@ public static async Task Initialize(IServiceProvider provider) 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(); + SearchManager search = provider.GetRequiredService(); + + // 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) diff --git a/back/src/Kyoo.Meilisearch/SearchManager.cs b/back/src/Kyoo.Meilisearch/SearchManager.cs index 22c18d091..20fa7af04 100644 --- a/back/src/Kyoo.Meilisearch/SearchManager.cs +++ b/back/src/Kyoo.Meilisearch/SearchManager.cs @@ -51,26 +51,26 @@ public SearchManager(MeilisearchClient client, ILibraryManager libraryManager) _client = client; _libraryManager = libraryManager; - IRepository.OnCreated += (x) => _CreateOrUpdate("items", x, nameof(Movie)); - IRepository.OnEdited += (x) => _CreateOrUpdate("items", x, nameof(Movie)); + IRepository.OnCreated += (x) => CreateOrUpdate("items", x, nameof(Movie)); + IRepository.OnEdited += (x) => CreateOrUpdate("items", x, nameof(Movie)); IRepository.OnDeleted += (x) => _Delete("items", x.Id, nameof(Movie)); - IRepository.OnCreated += (x) => _CreateOrUpdate("items", x, nameof(Show)); - IRepository.OnEdited += (x) => _CreateOrUpdate("items", x, nameof(Show)); + IRepository.OnCreated += (x) => CreateOrUpdate("items", x, nameof(Show)); + IRepository.OnEdited += (x) => CreateOrUpdate("items", x, nameof(Show)); IRepository.OnDeleted += (x) => _Delete("items", x.Id, nameof(Show)); - IRepository.OnCreated += (x) => _CreateOrUpdate("items", x, nameof(Collection)); - IRepository.OnEdited += (x) => _CreateOrUpdate("items", x, nameof(Collection)); + IRepository.OnCreated += (x) => CreateOrUpdate("items", x, nameof(Collection)); + IRepository.OnEdited += (x) => CreateOrUpdate("items", x, nameof(Collection)); IRepository.OnDeleted += (x) => _Delete("items", x.Id, nameof(Collection)); - IRepository.OnCreated += (x) => _CreateOrUpdate(nameof(Episode), x); - IRepository.OnEdited += (x) => _CreateOrUpdate(nameof(Episode), x); + IRepository.OnCreated += (x) => CreateOrUpdate(nameof(Episode), x); + IRepository.OnEdited += (x) => CreateOrUpdate(nameof(Episode), x); IRepository.OnDeleted += (x) => _Delete(nameof(Episode), x.Id); - IRepository.OnCreated += (x) => _CreateOrUpdate(nameof(Studio), x); - IRepository.OnEdited += (x) => _CreateOrUpdate(nameof(Studio), x); + IRepository.OnCreated += (x) => CreateOrUpdate(nameof(Studio), x); + IRepository.OnEdited += (x) => CreateOrUpdate(nameof(Studio), x); IRepository.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) {