-
-
Notifications
You must be signed in to change notification settings - Fork 244
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: Add full text search support
- Loading branch information
1 parent
75d315d
commit a543473
Showing
17 changed files
with
440 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { MeiliSearch, Index } from "meilisearch"; | ||
import serverConfig from "./config"; | ||
import { z } from "zod"; | ||
|
||
export const zBookmarkIdxSchema = z.object({ | ||
id: z.string(), | ||
userId: z.string(), | ||
url: z.string().nullish(), | ||
title: z.string().nullish(), | ||
description: z.string().nullish(), | ||
content: z.string().nullish(), | ||
tags: z.array(z.string()).default([]), | ||
}); | ||
|
||
export type ZBookmarkIdx = z.infer<typeof zBookmarkIdxSchema>; | ||
|
||
let searchClient: MeiliSearch | undefined; | ||
|
||
if (serverConfig.meilisearch) { | ||
searchClient = new MeiliSearch({ | ||
host: serverConfig.meilisearch.address, | ||
apiKey: serverConfig.meilisearch.key, | ||
}); | ||
} | ||
|
||
const BOOKMARKS_IDX_NAME = "bookmarks"; | ||
|
||
let idxClient: Index<ZBookmarkIdx> | undefined; | ||
|
||
export async function getSearchIdxClient(): Promise<Index<ZBookmarkIdx> | 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<ZBookmarkIdx>(BOOKMARKS_IDX_NAME); | ||
const taskId = await idxFound.updateFilterableAttributes(["id", "userId"]); | ||
await searchClient.waitForTask(taskId.taskUid); | ||
} | ||
return idxFound; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
"use client"; | ||
|
||
import { api } from "@/lib/trpc"; | ||
import { usePathname, useRouter, useSearchParams } from "next/navigation"; | ||
import BookmarksGrid from "../bookmarks/components/BookmarksGrid"; | ||
import { Input } from "@/components/ui/input"; | ||
import Loading from "../bookmarks/loading"; | ||
import { keepPreviousData } from "@tanstack/react-query"; | ||
import { Search } from "lucide-react"; | ||
import { ActionButton } from "@/components/ui/action-button"; | ||
import { Suspense, useRef } from "react"; | ||
|
||
function SearchComp() { | ||
const router = useRouter(); | ||
const pathname = usePathname(); | ||
const searchParams = useSearchParams(); | ||
const searchQuery = searchParams.get("q") || ""; | ||
|
||
const { data, isPending, isPlaceholderData, error } = | ||
api.bookmarks.searchBookmarks.useQuery( | ||
{ | ||
text: searchQuery, | ||
}, | ||
{ | ||
placeholderData: keepPreviousData, | ||
}, | ||
); | ||
|
||
if (error) { | ||
throw error; | ||
} | ||
|
||
const inputRef: React.MutableRefObject<HTMLInputElement | null> = | ||
useRef<HTMLInputElement | null>(null); | ||
|
||
let timeoutId: NodeJS.Timeout | undefined; | ||
|
||
// Debounce user input | ||
const doSearch = () => { | ||
if (!inputRef.current) { | ||
return; | ||
} | ||
router.replace(`${pathname}?q=${inputRef.current.value}`); | ||
}; | ||
|
||
const onInputChange = () => { | ||
if (timeoutId) { | ||
clearTimeout(timeoutId); | ||
} | ||
timeoutId = setTimeout(() => { | ||
doSearch(); | ||
}, 200); | ||
}; | ||
|
||
return ( | ||
<div className="container flex flex-col gap-3 p-4"> | ||
<div className="flex gap-2"> | ||
<Input | ||
ref={inputRef} | ||
placeholder="Search" | ||
defaultValue={searchQuery} | ||
onChange={onInputChange} | ||
/> | ||
<ActionButton | ||
loading={isPending || isPlaceholderData} | ||
onClick={doSearch} | ||
> | ||
<span className="flex gap-2"> | ||
<Search /> | ||
<span className="my-auto">Search</span> | ||
</span> | ||
</ActionButton> | ||
</div> | ||
<hr /> | ||
{data ? ( | ||
<BookmarksGrid | ||
query={{ ids: data.bookmarks.map((b) => b.id) }} | ||
bookmarks={data.bookmarks} | ||
/> | ||
) : ( | ||
<Loading /> | ||
)} | ||
</div> | ||
); | ||
} | ||
|
||
export default function SearchPage() { | ||
return ( | ||
<Suspense> | ||
<SearchComp /> | ||
</Suspense> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.