Level up your content search with the sanity-algolia toolkit
The new sanity-algolia toolkit lets you efficiently index your content using serverless functions.
Algolia is a service that lets you index and add complex search features to your content while also gathering useful metrics about what people are looking for and if they're able to find it. In conversations with customers and users, it frequently comes up as a technology they want to integrate with.
We want to help people get going with Sanity and Algolia quicker. So today we're announcing a toolkit that lets you index your content from Sanity into Algolia using serverless functions on Vercel. It comes with some helper functions that take care of the indexing mechanics and flatten Portable Text to an indexable form.
You can go to Algolia's blog post to read how to set it up or go directly to the GitHub repository.
// Minimal example of a search indexing function
import indexer from 'sanity-algolia'
import algoliaIndex from '../lib/algoloa'
import sanity from '../lib/client'
export default (req, res) => {
const sanityAlgolia = indexer(
{
post: {
index: algoliaIndex,
projection: `{
title,
"path": slug.current,
"body": pt::text(body)
}`
}
},
(document: SanityDocumentStub) => {
return {
title: document.heading,
body: document.body,
authorNames: document.authorNames
}
}
)
return sanityAlgolia
.webhookSync(sanity, req.body)
.then(() => res.status(200).send('ok'))
}