diff options
| author | MohamedBassem <me@mbassem.com> | 2024-03-30 15:44:17 +0000 |
|---|---|---|
| committer | MohamedBassem <me@mbassem.com> | 2024-03-30 15:44:17 +0000 |
| commit | 853ed13450b3a0d92cba144cc0dfd0696e7c810c (patch) | |
| tree | 7e654b5d32e7c4b8e69091c48dcab89eb7b50bad | |
| parent | e99dee0b4f7220568c8ffa2755147bc45d35b32b (diff) | |
| download | karakeep-853ed13450b3a0d92cba144cc0dfd0696e7c810c.tar.zst | |
fix: Sort search results by relevance
| -rw-r--r-- | apps/workers/searchWorker.ts | 1 | ||||
| -rw-r--r-- | packages/shared/search.ts | 18 | ||||
| -rw-r--r-- | packages/trpc/routers/bookmarks.ts | 8 |
3 files changed, 26 insertions, 1 deletions
diff --git a/apps/workers/searchWorker.ts b/apps/workers/searchWorker.ts index 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.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<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.ts index 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 }; }), |
