Skip to content

Commit

Permalink
feature: add pagefind to search component
Browse files Browse the repository at this point in the history
  • Loading branch information
cdxker committed Dec 13, 2024
1 parent 26e8ed6 commit a097bbd
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 2 deletions.
4 changes: 3 additions & 1 deletion clients/search-component/example/.env.dist
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ VITE_API_KEY="tr-zpPVGUq18FxOCmXgLfqGbmDOY4UMW00r"
VITE_BRAND_LOGO_SRC_URL="https://cdn.trieve.ai/trieve-logo.png"
VITE_BRAND_NAME="Trieve"
VITE_BRAND_COLOR="#CB53EB"
VITE_PROBLEM_LINK="mailto:[email protected]?subject="
VITE_PROBLEM_LINK="mailto:[email protected]?subject="
VITE_USE_PAGEFIND="false"
VITE_PAGEFIND_URL="https://pagefind-testing-index.trieve.ai/pagfind-index-west/pagefind"
6 changes: 6 additions & 0 deletions clients/search-component/example/src/routes/index.lazy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export default function Home() {
const brandColor = import.meta.env.VITE_ACCENT_COLOR;
const problemLink = import.meta.env.VITE_PROBLEM_LINK;
const useGroupSearch = import.meta.env.VITE_USE_GROUP_SEARCH == "true";
const usePagefind = import.meta.env.VITE_USE_PAGEFIND == "true";
const pagefindUrl = import.meta.env.VITE_PAGEFIND_URL;
const defaultSearchQueries: string[] = (
import.meta.env.VITE_DEFAULT_SEARCH_QUERIES ?? ""
).split(",");
Expand Down Expand Up @@ -120,6 +122,10 @@ export default function Home() {
},
]}
useGroupSearch={useGroupSearch}
pagefindOptions={{
usePagefind: usePagefind,
cdnBaseUrl: pagefindUrl,
}}
defaultAiQuestions={[
"What is Trieve?",
"How to perform autocomplete search?",
Expand Down
10 changes: 10 additions & 0 deletions clients/search-component/src/utils/hooks/modal-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
import {
countChunks,
groupSearchWithTrieve,
searchWithPagefind,
searchWithTrieve,
} from "../trieve";

Expand All @@ -38,6 +39,11 @@ export type ModalTypes = "ecommerce" | "docs";
export type SearchModes = "chat" | "search";
export type searchOptions = simpleSearchReqPayload & customAutoCompleteAddOn;

export interface PagefindOptions {
usePagefind: boolean;
cdnBaseUrl?: string;
}

export type ModalProps = {
datasetId: string;
apiKey: string;
Expand Down Expand Up @@ -65,6 +71,7 @@ export type ModalProps = {
icon?: () => JSX.Element;
}[];
defaultSearchMode?: SearchModes;
pagefindOptions?: PagefindOptions;
type?: ModalTypes;
useGroupSearch?: boolean;
allowSwitchingModes?: boolean;
Expand Down Expand Up @@ -220,6 +227,9 @@ const ModalProvider = ({

setResults(Array.from(groupMap.values()));
setRequestID(results.requestID);
} else if (props.pagefindOptions?.usePagefind) {
const results = await searchWithPagefind(query, props.pagefindOptions, props.datasetId);
setResults(results);
} else {
const results = await searchWithTrieve({
query: query,
Expand Down
43 changes: 42 additions & 1 deletion clients/search-component/src/utils/trieve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
} from "trieve-ts-sdk";
import { Chunk, GroupSearchResults, Props, SearchResults } from "./types";
import { defaultHighlightOptions, highlightText } from "./highlight";
import { ModalTypes } from "./hooks/modal-context";
import { ModalTypes, PagefindOptions } from "./hooks/modal-context";

export const omit = (obj: object | null | undefined, keys: string[]) => {
if (!obj) return obj;
Expand Down Expand Up @@ -300,3 +300,44 @@ export const getAllChunksForGroup = async (
}
return chunks;
};

export const searchWithPagefind = async (
query: string,
pagefindOptions: PagefindOptions,
datasetId: string,
) => {
const pagefind_base_url = `${pagefindOptions.cdnBaseUrl}/${datasetId}`;

const pagefind = await import(`${pagefind_base_url}/pagefind.js`);
const response = await pagefind.search(query);

const results = await Promise.all(response.results.map(async (result: any) => {
return await result.data();
}));

// Set pagesize to 20
const pagefindResultsMappedToTrieve = results.slice(0, 20).map((result, i) => {
return {
chunk: {
chunk_html: result.content,
link: result.url,
metadata: result.meta,
created_at: "",
dataset_id: datasetId,
id: i.toString(),
image_urls: null,
location: null,
num_value: null,
tag_set: null,
time_stamp: null,
tracking_id: null,
updated_at: "",
weight: 0

},
highlights: []
}
})

return pagefindResultsMappedToTrieve;
};

0 comments on commit a097bbd

Please sign in to comment.