From 54243b8cc5ccd76fe23821f6e159b954a2166578 Mon Sep 17 00:00:00 2001 From: Mohamed Bassem Date: Sun, 1 Feb 2026 22:42:17 +0000 Subject: feat: batch meilisearch requests (#2441) * feat: batch meilisearch requests * more fixes --- apps/workers/workers/searchWorker.ts | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) (limited to 'apps') diff --git a/apps/workers/workers/searchWorker.ts b/apps/workers/workers/searchWorker.ts index 7a9c3a8d..b0608dce 100644 --- a/apps/workers/workers/searchWorker.ts +++ b/apps/workers/workers/searchWorker.ts @@ -56,7 +56,11 @@ export class SearchIndexingWorker { } } -async function runIndex(searchClient: SearchIndexClient, bookmarkId: string) { +async function runIndex( + searchClient: SearchIndexClient, + bookmarkId: string, + batch: boolean, +) { const bookmark = await db.query.bookmarks.findFirst({ where: eq(bookmarks.id, bookmarkId), with: { @@ -107,11 +111,15 @@ async function runIndex(searchClient: SearchIndexClient, bookmarkId: string) { tags: bookmark.tagsOnBookmarks.map((t) => t.tag.name), }; - await searchClient.addDocuments([document]); + await searchClient.addDocuments([document], { batch }); } -async function runDelete(searchClient: SearchIndexClient, bookmarkId: string) { - await searchClient.deleteDocuments([bookmarkId]); +async function runDelete( + searchClient: SearchIndexClient, + bookmarkId: string, + batch: boolean, +) { + await searchClient.deleteDocuments([bookmarkId], { batch }); } async function runSearchIndexing(job: DequeuedJob) { @@ -133,17 +141,20 @@ async function runSearchIndexing(job: DequeuedJob) { } const bookmarkId = request.data.bookmarkId; + // Disable batching on retries (runNumber > 0) for improved reliability + const batch = job.runNumber === 0; + logger.info( - `[search][${jobId}] Attempting to index bookmark with id ${bookmarkId} ...`, + `[search][${jobId}] Attempting to index bookmark with id ${bookmarkId} (run ${job.runNumber}, batch=${batch}) ...`, ); switch (request.data.type) { case "index": { - await runIndex(searchClient, bookmarkId); + await runIndex(searchClient, bookmarkId, batch); break; } case "delete": { - await runDelete(searchClient, bookmarkId); + await runDelete(searchClient, bookmarkId, batch); break; } } -- cgit v1.2.3-70-g09d2