Algolia is a search engine that delivers real-time results and allows you to quickly implement search for your application. This article will walk through creating a simple search page (similar to ours) using Algolia’s React InstantSearch library.
Setting Up Your Index
Creating Your Index
Go to your Algolia dashboard and click “Create Application”. Choose a location for your server and click “Create”.
Choose a name for your new index.
Importing Your Data
There are multiple options for getting your data into your index. This guide will go over using the Algolia API.
Install Algolia instantsearch with npm install algoliasearch react-instantsearch
Create a new route.ts file under a path of your choice in the app directory (for example, app/lookup/route.ts).
Initialize your Algolia client and write a GET request handler that posts your data to Algolia
You can find your API keys under “API keys” tab of your project. You can use the default keys, but you might want to create your own - one with search permissions to use with your search page and one with posting permissions to use for your API endpoint
You can manually send a request to your API endpoint when you want to update your index or set up a Vercel CRON job to periodically query it. You might also want to add an authorization key to your request and check for it within the handler or limit the amount of API calls per hour of your postKey.
Creating a Search Page
Start by creating a new page.tsx at the route you want your search page to be located (for example, app/search/page.tsx)
Create and export your new search component - here we called it Search
Import and use InstantSearch, SearchBox, and Hits from react-instantsearch.
Customizing Your Hit Components
If you don’t like the look of Algolia’s default Hit components, you can add your own styling by overriding it.
Limiting Results and Using Pagination
If you have a lot of hits and don’t want to display them all on one page, you can use pagination instead.
Displaying Search Statistics
Algolia also has a built-in component that can display statistics about a query. It displays the number of hits and time to retrieve results by default.
If you want to change the default statistics, you can also override what’s displayed by creating your own Stats component. Algolia’s usePagination hook provides multiple statistics such as the number of pages, number of hits, etc. You can find a full list of provided variables here. This example will show you how to show the current number of displayed results on a page (i.e. Showing 1 to 7 of 25 results).
Wrapping Up
This guide went over some examples of using and customizing Algolia InstantSearch components. You may want to add more components or refactor some logic into their own files. Additional features can be found in the Algolia InstantSearch API.
Found this article interesting? Chat with our account specialists to get started on your next digital journey.