From 853ed13450b3a0d92cba144cc0dfd0696e7c810c Mon Sep 17 00:00:00 2001 From: MohamedBassem Date: Sat, 30 Mar 2024 15:44:17 +0000 Subject: fix: Sort search results by relevance --- packages/shared/search.ts | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) (limited to 'packages/shared/search.ts') diff --git a/packages/shared/search.ts b/packages/shared/search.ts index 89a41329..8422d79e 100644 --- a/packages/shared/search.ts +++ b/packages/shared/search.ts @@ -9,6 +9,7 @@ export const zBookmarkIdxSchema = z.object({ title: z.string().nullish(), description: z.string().nullish(), content: z.string().nullish(), + createdAt: z.string().nullish(), note: z.string().nullish(), tags: z.array(z.string()).default([]), }); @@ -44,8 +45,23 @@ export async function getSearchIdxClient(): Promise | null> }); await searchClient.waitForTask(idx.taskUid); idxFound = await searchClient.getIndex(BOOKMARKS_IDX_NAME); - const taskId = await idxFound.updateFilterableAttributes(["id", "userId"]); + } + + const desiredFilterableAttributes = ["id", "userId"].sort(); + const desiredSortableAttributes = ["createdAt"].sort(); + + const settings = await idxFound.getSettings(); + if (JSON.stringify(settings.filterableAttributes?.sort()) != JSON.stringify(desiredFilterableAttributes)) { + console.log(`[meilisearch] Updating desired filterable attributes to ${desiredFilterableAttributes} from ${settings.filterableAttributes}`); + const taskId = await idxFound.updateFilterableAttributes(desiredFilterableAttributes); + await searchClient.waitForTask(taskId.taskUid); + } + + if (JSON.stringify(settings.sortableAttributes?.sort()) != JSON.stringify(desiredSortableAttributes)) { + console.log(`[meilisearch] Updating desired sortable attributes to ${desiredSortableAttributes} from ${settings.sortableAttributes}`); + const taskId = await idxFound.updateSortableAttributes(desiredSortableAttributes); await searchClient.waitForTask(taskId.taskUid); } + idxClient = idxFound; return idxFound; } -- cgit v1.2.3-70-g09d2