Skip to content

Commit

Permalink
Load every items from postgres on meilisearch creation
Browse files Browse the repository at this point in the history
  • Loading branch information
zoriya committed Nov 1, 2023
1 parent 1698de3 commit 167e285
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 13 deletions.
7 changes: 5 additions & 2 deletions back/src/Kyoo.Host/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,11 @@ public async Task Start(string[] args, Action<ContainerBuilder> 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);
}
Expand Down
21 changes: 21 additions & 0 deletions back/src/Kyoo.Meilisearch/MeilisearchModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<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)
Expand Down
22 changes: 11 additions & 11 deletions back/src/Kyoo.Meilisearch/SearchManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,26 +51,26 @@ public SearchManager(MeilisearchClient client, ILibraryManager libraryManager)
_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)
{
Expand Down

0 comments on commit 167e285

Please sign in to comment.