`trieve-hooks` is a simple React hook package that simplifies the process of creating search functionality in React applications using the [Trieve API](https://docs.trieve.ai/introduction).
npm install trieve-demo-searchtrieve-hooks is a simple React hook package that simplifies the process of creating search functionality in React applications using the Trieve API.
Before using this package, you'll need to have created and populated a dataset in Trieve, and then obtain your:
1. Dataset ID
2. Trieve API Key
``bash`
npm install trieve-hooks
Import the useSearch hook from trieve-hooks and use it in your React component:
`jsx
import { useSearch } from 'trieve-hooks';
const App = () => {
const {
searchQuery,
setSearchQuery,
resultChunks,
fetching,
currentPage,
setCurrentPage,
goToNextPage,
// other returned properties
} = useSearch({
apiKey: 'YOUR_API_KEY',
datasetId: 'YOUR_DATASET_ID',
initialQuery: 'open source EHR software',
resultsPerPage: 10,
highlightResults: false,
slimChunks: true,
recencyBias: 0,
});
// Use the returned properties in your component
// ...
};
`
The useSearch hook accepts an options object with the following properties:
- apiKey (string, required): Your Trieve API key.datasetId
- (string, required): The ID of the dataset to search.apiUrl
- (string, optional): The URL of the Trieve API. Default is "https://api.trieve.ai/api".initialQuery
- (string, optional): The initial search query. Default is "open source EHR software".resultsPerPage
- (number, optional): The number of search results per page. Default is 10.highlightResults
- (boolean, optional): Enables highlighting of search results. Default is false.slimChunks
- (boolean, optional): Enables retrieval of smaller chunks of data. Default is true.recencyBias
- (number, optional): Applies a bias to favor more recent results. Default is 0.
The useSearch hook returns an object with the following properties:
- searchQuery (string): The current search query.setSearchQuery
- (function): Sets the search query.resultChunks
- (array): The search result chunks.fetching
- (boolean): Indicates if the search is in progress.currentPage
- (number): The current page of search results.setCurrentPage
- (function): Sets the current page.goToNextPage
- (function): Navigates to the next or previous page.contentStart
- (number): The index of the first visible search result.contentEnd
- (number): The index of the last visible search result.setSearchType
- (function): Sets the search type.setSortBy
- (function): Sets the sorting criteria.setPerPage
- (function): Sets the number of results per page.changeSearch
- (function): Handles input changes and resets pagination.setHighlightResults
- (function): Enables or disables result highlighting.setSlimChunks
- (function): Enables or disables slim chunk retrieval.setRecencyBias
- (function): Sets the recency bias value.setFilters
- (function): Sets the search filters.filters` (object): The current search filters.
-
This package is licensed under the MIT License.