Skip to content

Commit

Permalink
Add fuzzySearch parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanoverna committed Sep 9, 2022
1 parent 33a3328 commit 9c2590c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
3 changes: 3 additions & 0 deletions docs/site-search.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ const client = buildClient({ apiToken: 'YOUR_API_TOKEN' });
const { state, error, data } = useSiteSearch({
client,
buildTriggerId: '7497',
// optional: by default fuzzy-search is not active
fuzzySearch: true,
// optional: you can omit it you only have one locale, or you want to find results in every locale
initialState: { locale: 'en' },
// optional: to configure how to present the part of page title/content that matches the query
Expand All @@ -59,6 +61,7 @@ For a complete walk-through, please refer to the [DatoCMS Site Search documentat
| ------------------- | ------------------------------------------------------------------ | ------------------ | ------------------------------------------------------------------------------------------------------------------------------------------ | ---------------------------------------------------------- |
| client | CMA Client instance | :white_check_mark: | [DatoCMS CMA Client](https://www.datocms.com/docs/content-management-api/using-the-nodejs-clients) instance | |
| buildTriggerId | string | :white_check_mark: | The [ID of the build trigger](https://www.datocms.com/docs/site-search/base-integration#performing-searches) to use to find search results | |
| fuzzySearch | boolean | :x: | Whether fuzzy-search is active or not. When active, it will also find strings that approximately match the query provided. | false |
| resultsPerPage | number | :x: | The number of search results to show per page | 8 |
| highlightMatch | (match, key, context: 'title' \| 'bodyExcerpt') => React.ReactNode | :x: | A function specifying how to highlight the part of page title/content that matches the query | (text, key) => (<mark key={key}>{text}</mark>) |
| initialState.query | string | :x: | Initialize the form with a specific query | '' |
Expand Down
13 changes: 11 additions & 2 deletions src/useSiteSearch/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type SearchResultInstancesHrefSchema = {
[k: string]: unknown;
};
filter: {
fuzzy?: string;
query: string;
build_trigger_id?: string;
locale?: string;
Expand Down Expand Up @@ -58,6 +59,7 @@ type Highlighter = (
export type UseSiteSearchConfig<Client extends GenericClient> = {
client: Client;
buildTriggerId: string;
fuzzySearch?: boolean;
resultsPerPage?: number;
highlightMatch?: Highlighter;
initialState?: {
Expand Down Expand Up @@ -150,7 +152,7 @@ export function useSiteSearch<Client extends GenericClient>(

setResponse(undefined);

const response = await config.client.searchResults.rawList({
const request: SearchResultInstancesHrefSchema = {
filter: {
query: state.query,
locale: state.locale,
Expand All @@ -160,7 +162,13 @@ export function useSiteSearch<Client extends GenericClient>(
limit: resultsPerPage,
offset: resultsPerPage * state.page,
},
});
};

if (config.fuzzySearch) {
request.fuzzy = 'true';
}

const response = await config.client.searchResults.rawList(request);

if (!isCancelled) {
setResponse(response);
Expand All @@ -187,6 +195,7 @@ export function useSiteSearch<Client extends GenericClient>(
resultsPerPage,
state,
config.buildTriggerId,
config.fuzzySearch,
config.client.config.apiToken,
]);

Expand Down

0 comments on commit 9c2590c

Please sign in to comment.