From cbaf9e6034aa09911fca967b7af6cad11f154b3e Mon Sep 17 00:00:00 2001 From: Mohamed Bassem Date: Tue, 31 Dec 2024 13:17:56 +0200 Subject: feat: Introduce advanced search capabilities (#753) * feat: Implement search filtering in the backend * feat: Implement search language parser * rename matcher name * Add ability to interleve text * More fixes * be more tolerable to parsing errors * Add a search query explainer widget * Handle date parsing gracefully * Fix the lockfile * Encode query search param * Fix table body error * Fix error when writing quotes --- packages/trpc/routers/bookmarks.ts | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'packages/trpc/routers') diff --git a/packages/trpc/routers/bookmarks.ts b/packages/trpc/routers/bookmarks.ts index 254ac6c2..3320b3b9 100644 --- a/packages/trpc/routers/bookmarks.ts +++ b/packages/trpc/routers/bookmarks.ts @@ -45,6 +45,7 @@ import { zNewBookmarkRequestSchema, zUpdateBookmarksRequestSchema, } from "@hoarder/shared/types/bookmarks"; +import { zMatcherSchema } from "@hoarder/shared/types/search"; import type { AuthedContext, Context } from "../index"; import { authedProcedure, router } from "../index"; @@ -54,6 +55,7 @@ import { mapDBAssetTypeToUserType, mapSchemaAssetTypeToDB, } from "../lib/attachments"; +import { getBookmarkIdsFromMatcher } from "../lib/search"; export const ensureBookmarkOwnership = experimental_trpcMiddleware<{ ctx: Context; @@ -521,6 +523,7 @@ export const bookmarksAppRouter = router({ .input( z.object({ text: z.string(), + matcher: zMatcherSchema.optional(), cursor: z .object({ offset: z.number(), @@ -548,8 +551,19 @@ export const bookmarksAppRouter = router({ message: "Search functionality is not configured", }); } + + let filter: string[]; + if (input.matcher) { + const bookmarkIds = await getBookmarkIdsFromMatcher(ctx, input.matcher); + filter = [ + `userId = '${ctx.user.id}' AND id IN [${bookmarkIds.join(",")}]`, + ]; + } else { + filter = [`userId = '${ctx.user.id}'`]; + } + const resp = await client.search(input.text, { - filter: [`userId = '${ctx.user.id}'`], + filter, showRankingScore: true, attributesToRetrieve: ["id"], sort: ["createdAt:desc"], -- cgit v1.2.3-70-g09d2