This quickstart walks through the main building blocks of an Algolia app:Documentation Index
Fetch the complete documentation index at: https://algolia.com/llms.txt
Use this file to discover all available pages before exploring further.
- Create an index, which is the searchable version of your data.
- Build an interface that helps users find what they need.
- Configure relevance and ranking, so the most useful results appear first.
Before you begin
Make sure you have:- An Algolia account. Create one for free if you don’t already have one.
- Node.js 20.19 or later.
Set up your app and index sample data
Set up your project and index a sample product dataset. In Algolia, the way you structure and configure your index affects both the search experience and how results are ranked.Install dependencies
Install the packages for indexing and search:
algoliasearchsends data to Algolia.react-instantsearchbuilds the search UI.instantsearch.cssprovides base styles.tsxruns the indexing script.orashows progress in the terminal while the script runs.
Add your Algolia credentials
In the Algolia dashboard, open the API Keys page and copy these values:
- Application ID. Identifies your Algolia application.
- Write API key. Used by the indexing script to update records and index settings.
- Search API key. Used by the React app to query the index from the browser.
.env.local file and add the values to it:.env.local
Keep your Write API key secret.
Don’t commit
.env.local to version control or expose the write key in your app.Create the indexing script
Create a This script loads your credentials,
fetches a sample product dataset,
uploads those products as records to Algolia with
scripts directory and a scripts/indexing.ts file.saveObjects,
and adds product_type as a facet with setSettings so your UI can filter results.Run this script once to create and configure the index before connecting it to your UI.
Since indexing operations are asynchronous,
waitForTasks and waitForTask make sure the records and settings are applied before the script exits.scripts/indexing.ts
Index the sample products
Add the indexing script to Run the script to create the
package.json:quickstart-products index and add the sample records.You should see the message
Successfully indexed products. in your terminal.
At this point, your data is ready for search and you can explore the quickstart-products index
in the Algolia dashboard.
In the next section, you’ll connect a React UI to this index.Build the search UI
Connect a React UI to yourquickstart-products index.
React InstantSearch automatically keeps the search box, filters, and pagination in sync with the current results.
Connect your app to Algolia
Replace This app connects to your
src/App.tsx with the following code to connect your app to Algolia and render a basic search interface.src/App.tsx
quickstart-products index and renders a search interface.SearchBoxlets users enter search terms.RefinementListadds filtering by product type.Hitsdisplays matching records from your index.Paginationlets users move through results.HighlightandSnippetemphasize matching parts oftitleanddescription.
Style the search interface
Replace
src/App.css with the following code.
These styles lay out the search interface and make the results and filters easier to scan.
They change the presentation, not the search behavior.src/App.css
Start the app
Start the development server.Open You now have a working end-to-end search:
http://localhost:5173 and start searching.You see a product grid with images, a product type filter,
and a search box.
As you type, the product grid updates.
Matching text is highlighted in the results.
- Records come from the
quickstart-productsindex you created earlier. - Queries from
SearchBoxupdate the results and pagination together. - The
product_typefilter works because that attribute is configured for faceting. HighlightandSnippetshow which parts of each record match the query.
Improve relevance and add features
Improve relevance
Improve relevance
Your app already returns matching products.
You can improve relevance by adjusting which matches appear first.Update the existing This makes search results more useful by prioritizing matches in product titles and types before descriptions,
then ranking similar matches by best-selling and higher-priced products.
indexSettings object in scripts/indexing.ts,
then rerun npm run index:products.scripts/indexing.ts
Add more search features
Add more search features
Add features that help users understand and refine results.
For example, you can add search stats,
active filters,
or more ways to filter results, such as filter lists,
menus,
and numerical filters.
If you add more filtering features,
update
attributesForFaceting in scripts/indexing.ts to match.Collect events from your search interface
Collect events from your search interface
Collect events from your UI to measure engagement and
support Algolia features that use behavioral data.Update the This turns on the default React InstantSearch events,
including
InstantSearch component in src/App.tsx:src/App.tsx
view events for hits and filter click events for RefinementList.To send click events for hits, see Send events with React InstantSearch.What’s next
- Get started with React InstantSearch to continue building your search interface.
- Prepare your records for indexing to replace the sample catalog with your own product data.
- Manage results to learn how to adjust relevance and ranking to your needs.
