Developer guides

How to implement front-end search with Sanity

By integrating Sanity's structured content with Algolia, you can provide your users with fast, relevant search results

This guide provides a comprehensive walkthrough on integrating Sanity's structured content platform with Algolia's powerful search capabilities using Sanity Functions, and an example front-end implementation using React and Next.js.

It will walk through how to set up indexing of your Sanity content in Algolia v5, including initial indexing of existing content and incremental updates as your content changes.

a diagram showing the process of building a website search engine .
Sanity Function runs on create, update, and delete events for the specified content types. It sends relevant data to Algolia index.

By following these steps, you can provide your users with fast, relevant search results while leveraging the benefits of Sanity's content platform.

Steps to implement:

  • Create schema in Sanity and create Next.js app
  • Set up environment variables
  • Run a first time indexing script
  • Incremental indexing
  • Set up and deploy an Algolia sync Sanity Function
  • Test the sync function locally
  • Customization
  • Indexing long records
  • Create a front-end search component
  • Add Search component to your page

Create schema in Sanity and create Next.js app

We will be using:

Follow the instructions for Clean Next.js + Sanity app template to install and deploy your project.

The template includes a Next.js app with a Sanity Studio – an open-source React application that connects to your Sanity project’s hosted dataset.

Either start with a sample content included with the template, or create your own.

We will make it possible to search the post type documents using Algolia.
The post type has these fields:

{
  _id,
  title,
  slug,
  content,
  coverImage,
  date,
  _createdAt,
  _updatedAt
}

Environment variables

You must add NEXT_PUBLIC_ALGOLIA_APP_ID, NEXT_PUBLIC_ALGOLIA_API_KEY, and ALGOLIA_WRITE_KEY as environment variables in Vercel. You can find these in your Algolia account dashboard.

Algolia comes with a set of predefined API keys. Search API Key works on all your Algolia application indices and is safe to use in your production frontend code. Write API Key is used to create, update and DELETE your indices.

First time indexing

If you are indexing for the first time with Algolia, you can add a script within your studio folder and run it locally to do the initial indexing of all the existing content.

Create /scripts directory inside of /studio

Make sure you have these packages installed inside of your /studio directory:

npm install algoliasearch dotenv

Add script

Customization

Execute script

npx sanity exec scripts/algolia-initial-sync.ts --with-user-token

Check the Algolia index

If it synced successfully, your Algolia Application index should now have a number of records based on your query inside the Initial Sync Script.

Incremental indexing

When a content editor publishes, updates or deletes a blog post, the Sanity function that we are setting up below will automatically:

  • Trigger on the create, update, delete event for post documents
  • Extract the document data ( title , ID, coverImage, coverImageAlt, date, slug, _createdAt, _updatedAt, _type. )
  • Send the data to Algolia using the Algolia client
  • Update the search index with the latest content
Sanity+Algolia index
Screenshots of the Sanity Studio with matching indexed data in Algolia

Set up and deploy an Algolia Sync Sanity Function

This Sanity Function automatically syncs documents to Algolia's search index, ensuring your search functionality always reflects your latest content. When a post is published, the function sends the document data to Algolia, either creating a new search record or updating an existing one. We also track when documents are updated and deleted, using the delta operation. Our function can remove an item from Algolia under the delete operation.

Getting Started with Functions

Initialize Blueprints

To create your first function, you need to initialize a blueprint. Blueprints are templates that describe Sanity resources.

Learn More about Blueprints

Prerequisites:

  • sanity CLI v4.9.0 or higher is recommended to interact with Blueprints and Functions as shown in this guide. You can always run the latest CLI commands with npx sanity@latest.
  • Node.js v22.x. We highly suggest working on this version as it is the same version that your functions will run when deployed to Sanity.
  • An existing project and a role with Deploy Studio permissions (the deployStudio grant).

It's recommended keeping functions and blueprints a level above your Studio directory.

If you're using Clean Next.js + Sanity App template, your project structure may look like this:

main-project-folder/
├─ studio/
├─ frontend/

If you initialize the blueprint in the main-project-folder directory, functions and future resources will live alongside the studio and frontend directory.

npx sanity blueprints init

You'll be prompted to select your organization and Sanity studio.

Add the Algolia Function example

npx sanity blueprints add function --example algolia-document-sync

If you followed the directory structure mentioned earlier, you project structure may look like this:

main-project-folder/
├─ studio/
├─ frontend/
├─ sanity.blueprint.ts
├─ package.json
├─ node_modules/
├─ functions/
│  ├─ algolia-document-sync/
│  │  ├─ index.ts

Add configuration to your blueprint

sanity.blueprint.ts already exists in your root directory because you added the algolia-document-sync function example in the previous step.

We will modify it below and add SANITY_PROJECT_ID,
SANITY_DATASET environment variables, as well as more document fields inside the projection

Install dependencies

Inside your functions/algolia-document-sync directory install @sanity/asset-utils which we'll be using to build the image url from the document image

npm install @sanity/asset-utils

In the project root directory, we need to install dotenv package:

npm install dotenv

Update your Function

We will modify the Initial Algolia Sync Function script and add additional fields to the query projection, such as coverImage, coverImageAlt, date, slug, _createdAt, _updatedAt, _type. We will also truncate the body and title length. Algolia has size limits depending on your Algolia plan.

Deploy a Function

Once you're satisfied that the function works as expected, you can deploy it by deploying the blueprint.

npx sanity blueprints deploy

You can begin using your function when the deployment is finished. If you set a filter earlier, edit a document that matches it and publish the changes to trigger the function.

If you need to change the function, update your code and re-run the deploy command to push the new changes live.

Functions rate limits

Customization

Modify Indexed Fields

Update the fields sent to Algolia by modifying the object in addOrUpdateObject:

Change Target Index

Modify the index name (currently set to 'posts') to sync to a different Algolia index, alternatively pass _type into the projection so you can sync to indexes based on the post type, allowing one function to update many indexes:

Add Document Filtering

Update the filter to sync specific document types or conditions:

filter: "_type == 'post' && defined(publishedAt)"

Testing the Function Locally

Functions testing tips

There are several ways you can test your function

Using Sanity CLI

You can test the algolia-document-sync function locally using the Sanity CLI before deploying. To see a full list, see the functions CLI reference documentation or run npx sanity functions test --help.

This function writes directly to Algolia, so we can test locally with our document.json without relying on any Sanity schema.

  • Test with the included sample document:
npx sanity functions test algolia-document-sync --file functions/algolia-document-sync/document.json --dataset production --with-user-token
  • Supply data from a document in your dataset

Use the --dataset, --project-id, and --document-id flags to fetch real documents to use as source data for your function.

npm sanity@latest functions test algolia-document-sync --document-id 52df8926-1afe-413b-bd23-e9efbc32cea3 --project-id 123456 --dataset production

Running local server

The command below opens a local dev server on http://localhost:8080. You can also specify a port with the --port flag.

You need to select project, dataset, Document ID, choose Event, and click Run button to see the results.

  • Toggle "With Token" to supply a token to the function handler's context.clientOptions object. A token is omitted from local testing unless this is toggled.
npx sanity functions dev
a screenshot of the algolia document sync function
Testing function locally with a local dev server

Check the Logs

When you tested the function locally, you saw the logs directly in your console. Once deployed, the function and its logs are in the cloud.

npx sanity functions logs algolia-document-sync

INFO Successfully deleted document 77a50e6d-518a-422f-8e04-941c9e464d60 ("This is my test post") from Algolia

INFO Synced 633f34b8-a68c-480a-a2ff-59b8dc168871 (“Really awesome post”) – coverImage: https://cdn.sanity.io/images/e29s7c8p/production/b79d80d029395f5205ce09857644a75cc97fa356-5132x3157.jpg

The above command outputs the function's logs. Try updating your document, publishing the change, and running the command again to see new logs.

Indexing long records

Your Algolia plan has limits on the number of records and the size of records you can import. If you exceed these limits, you might get an error: Algolia error: Record too big.

To work around this Algolia suggests to break the page into sections or even paragraphs, and store each as a separate record. When you split a page, the same content might appear in multiple records. To avoid duplicates, you can turn on distinct and set attributeForDistinct.

Create a Front-end Search Component

In your frontend directory, you will need to install react-instantsearch and react-instantsearch-nextjs packages.

npm install react-instantsearch react-instantsearch-nextjs

In the /app/components directory add a new file Search.tsx

Add the Search Component to the Page

Add your Search to the front-end component in your site.

In our example we'll add it to the main page in /app/page.tsx

Run your frontend project and test to make sure you're getting search results:

npm run dev
a web page that says search with algolia on it
Our search results showing on the page

Front-end Implementation Demo

The example code in this guide can be found in https://github.com/sanity-io/sanity-algolia-sync

Live Demo: https://sanity-algolia-sync-one.sanity.dev

Conclusion

  • By integrating Sanity and Algolia, you can provide powerful search capabilities for your content. This guide walked through the steps to set up indexing of your Sanity content in Algolia, including:
  • Initial indexing in Algolia with a CLI script
  • Deploying a Sanity function to handle create, update, delete document events
  • Setting up Algolia search in your front-end application and using the Algolia JavaScript API client to send search queries and display the results.

Was this page helpful?