From b94896a0f8fa43b957a9bdd6ab57ada0ab8101af Mon Sep 17 00:00:00 2001 From: MohamedBassem Date: Sun, 27 Jul 2025 19:37:11 +0100 Subject: refactor: Extract meilisearch as a plugin --- packages/shared/search.ts | 90 +++++++++++++++-------------------------------- 1 file changed, 28 insertions(+), 62 deletions(-) (limited to 'packages/shared/search.ts') diff --git a/packages/shared/search.ts b/packages/shared/search.ts index 2c6904b2..2afc9763 100644 --- a/packages/shared/search.ts +++ b/packages/shared/search.ts @@ -1,10 +1,8 @@ -import type { Index } from "meilisearch"; -import { MeiliSearch } from "meilisearch"; import { z } from "zod"; -import serverConfig from "./config"; +import { PluginManager, PluginType } from "./plugins"; -export const zBookmarkIdxSchema = z.object({ +export const zBookmarkSearchDocument = z.object({ id: z.string(), userId: z.string(), url: z.string().nullish(), @@ -24,68 +22,36 @@ export const zBookmarkIdxSchema = z.object({ dateModified: z.date().nullish(), }); -export type ZBookmarkIdx = z.infer; +export type BookmarkSearchDocument = z.infer; -let searchClient: MeiliSearch | undefined; - -if (serverConfig.search.meilisearch) { - searchClient = new MeiliSearch({ - host: serverConfig.search.meilisearch.address, - apiKey: serverConfig.search.meilisearch.key, - }); +export interface SearchResult { + id: string; + score?: number; } -const BOOKMARKS_IDX_NAME = "bookmarks"; - -let idxClient: Index | undefined; - -export async function getSearchIdxClient(): Promise | null> { - if (idxClient) { - return idxClient; - } - if (!searchClient) { - return null; - } - - const indicies = await searchClient.getIndexes(); - let idxFound = indicies.results.find((i) => i.uid == BOOKMARKS_IDX_NAME); - if (!idxFound) { - const idx = await searchClient.createIndex(BOOKMARKS_IDX_NAME, { - primaryKey: "id", - }); - await searchClient.waitForTask(idx.taskUid); - idxFound = await searchClient.getIndex(BOOKMARKS_IDX_NAME); - } +export interface SearchOptions { + query: string; + filter?: string[]; + limit?: number; + offset?: number; + sort?: string[]; +} - const desiredFilterableAttributes = ["id", "userId"].sort(); - const desiredSortableAttributes = ["createdAt"].sort(); +export interface SearchResponse { + hits: SearchResult[]; + totalHits: number; + processingTimeMs: number; +} - 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); - } +export interface SearchIndexClient { + addDocuments(documents: BookmarkSearchDocument[]): Promise; + updateDocuments(documents: BookmarkSearchDocument[]): Promise; + deleteDocument(id: string): Promise; + deleteDocuments(ids: string[]): Promise; + search(options: SearchOptions): Promise; + clearIndex(): Promise; +} - 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; +export async function getSearchClient(): Promise { + return PluginManager.getClient(PluginType.Search); } -- cgit v1.2.3-70-g09d2