A self-hostable bookmark-everything app (links, notes and images) with AI-based automatic tagging and full text search
fix: Sort search results by relevance
MohamedBassem853ed13450b3
diff --git a/apps/workers/searchWorker.ts b/apps/workers/searchWorker.tsindex f983245d..ae916441 100644--- a/apps/workers/searchWorker.ts+++ b/apps/workers/searchWorker.ts@@ -74,6 +74,7 @@ async function runIndex(: undefined),...(bookmark.text ? { content: bookmark.text.text } : undefined),note: bookmark.note,+ createdAt: bookmark.createdAt.toISOString(),tags: bookmark.tagsOnBookmarks.map((t) => t.tag.name),},]);diff --git a/packages/shared/search.ts b/packages/shared/search.tsindex 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<Index<ZBookmarkIdx> | null>});await searchClient.waitForTask(idx.taskUid);idxFound = await searchClient.getIndex<ZBookmarkIdx>(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;}diff --git a/packages/trpc/routers/bookmarks.ts b/packages/trpc/routers/bookmarks.tsindex bcf200a3..4fb29c4c 100644--- a/packages/trpc/routers/bookmarks.ts+++ b/packages/trpc/routers/bookmarks.ts@@ -361,11 +361,18 @@ export const bookmarksAppRouter = router({}const resp = await client.search(input.text, {filter: [`userId = '${ctx.user.id}'`],+ showRankingScore: true,+ attributesToRetrieve: ["id"],+ sort: ["createdAt:desc"],});if (resp.hits.length == 0) {return { bookmarks: [], nextCursor: null };}+ const idToRank = resp.hits.reduce<Record<string, number>>((acc, r) => {+ acc[r.id] = r._rankingScore!;+ return acc;+ }, {});const results = await ctx.db.query.bookmarks.findMany({where: and(eq(bookmarks.userId, ctx.user.id),@@ -385,6 +392,7 @@ export const bookmarksAppRouter = router({asset: true,},});+ results.sort((a, b) => idToRank[b.id] - idToRank[a.id]);return { bookmarks: results.map(toZodSchema), nextCursor: null };}),