SearchClient
The SearchClient class is a vital part of search system. It takes care of search data, caching, events and enables providing different search adapters.
import { SearchClient } from '@archibald/search';
In order to use the search client, you need to create a new instance which takes the following options. Afterward you need to provide the instance to the SearchDataProvider.
const searchClient = new SearchClient(OPTIONS);
Options
| Name | Type | Description | Default |
|---|---|---|---|
| api | CreateApi | api definition. | commerce |
| adapter | SearchAdapter | mock | commerce | coveo | undefined | Search adapter. | commerce |
| cacheTime | number | undefined | How long search data should be kept in the cache. | 5 minutes |
| config | DefaultApiConfig | undefined | Api Config for the adapter. | |
| paramNames | SearchClientParamNames | undefined | URL params names for the search. |
SearchClientParamNames
| Name | Type | Description | Default |
|---|---|---|---|
| query | string | undefined | Search term | q |
| sort | string | undefined | Selected sort | sort |
| page | string | undefined | Current page if pagination is enabled | page |
search
This method executes search by calling onSearch method from the provided adapter.
await searchClient.search(PARAMETERS);
prefetchSearch
This method is used to prefetch a search on the server and seed the DataCache.
await searchClient.prefetchSearch(PARAMETERS);
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| key | string | Cache key pointing to the cache where the search results will be stored. | |
| searchOptions | SearchRequestOptions | Options that define what kind of search request will be sent. |
SearchRequestOptions
| Name | Type | Description | Default |
|---|---|---|---|
| categoryId | string | undefined | Category ID which is set on product search by category | |
| search | string | undefined | Search string container query, sort, current page etc. needed for both free search and search by category | |
| group | boolean | undefined | Fallback that will be used when new data is being fetched for the changed key avoiding loading indicators | |
| pageSizes | number[] | undefined | Number of page results per page | |
| entity | keyof SearchResponseMap | undefined | Entity type that is being searched for. |