trieve-demo-search
v1.0.15
Published
`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).
Downloads
33
Readme
trieve-hooks
trieve-hooks
is a simple React hook package that simplifies the process of creating search functionality in React applications using the Trieve API.
Prerequisites
Before using this package, you'll need to have created and populated a dataset in Trieve, and then obtain your:
- Dataset ID
- Trieve API Key
Installation
npm install trieve-hooks
Usage
Import the useSearch
hook from trieve-hooks
and use it in your React component:
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
// ...
};
Configuration
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 is10
.highlightResults
(boolean, optional): Enables highlighting of search results. Default isfalse
.slimChunks
(boolean, optional): Enables retrieval of smaller chunks of data. Default istrue
.recencyBias
(number, optional): Applies a bias to favor more recent results. Default is0
.
Returned Properties
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.
License
This package is licensed under the MIT License.