Algolia is a search-as-a-service platform that lets you add powerful search capabilities to your Contentstack-powered websites. It allows businesses to display highly-relevant and accurate search results, thereby reducing dependencies on support and cut down costs.
Apart from providing powerful search capabilities, Algolia also helps in capturing important user behavior analytics such as links clicked, queries performed, and so on.
In this guide, we will discuss how to add Algolia Search to your Contentstack-powered website using AWS Lambda.
Additional Resource: We also have other documents that cover the integration of Swiftype, Elasticsearch, and Lucidworks with your Contentstack-powered website.
- Basic understanding of Node.js and Algolia
- Contentstack and Algolia account
- AWS account and basic understanding of AWS Lambda
Setting up the Search
To use Algolia with Contentstack, you need to follow the steps given below:
- Create an account in Algolia
- Create an AWS Lambda function
- Set up API gateway
- Set up webhooks in Contentstack
- Create and publish an entry
Create an Account in Algolia
Algolia provides an interactive dashboard for managing searches and analytics. You can also manage indexes in your account. So whenever any content (entries or assets) is published or unpublished or deleted in Contentstack, the data object gets created or deleted, respectively, in your Algolia index.
So let's create an account in Algolia by using the following steps:
- Log in to Algolia and create your account.
Note: You get a 14-day trial account. You can upgrade your account by subscribing to any of the plans it offers
- After you log in to Algolia, you will see a successful message with options to choose from. Click on Go to dashboard.
- On your Algolia dashboard screen, click on Indices from the left navigation panel and click on Create Index.
- In the Create index model that opens, provide a name to your index inside the Index name field and click on Create. Make note of the index name that you just created, we will need it while creating the environment variables for our lambda function.
- Scroll a little down and click on API Keys from the left navigation panel as shown below:
- On the API Keys page, you will get an Application ID. You'll need to provide the Admin API Key and Application ID in your website configuration code, so make a note of these.
Create an AWS Lambda Function
Create an AWS Lambda function that sends the data (received from Contentstack via webhooks) to Algolia.
- Login to your AWS Management Console, select Lambda from the Services list.
- Click on the Create Function button, and then the Author from Scratch option.
Provide a name to your lambda function inside the Function name field, select Node.js 14.x as your Runtime language, and click on the Create function button.
You will get a success message on creation of the lambda function. For this exercise, we have created the sample code for the lambda functions. Contact our support team to get the sample code.
- Then, fire up your terminal and move inside the project directory that you just downloaded.
- Install npm modules using the following command:
- Node modules are now installed the project directory. Compress it using any tool and upload this code in the code editor of your Lambda function.
- Now go back to the AWS console and in the Code source section, click on the Upload from dropdown and select .zip file.
In the Upload a .zip file modal, click on the Upload button, move to the folder where you have saved the compressed file, and select it. Then, click on Save.
In the Runtime settings option, keep Handler as index.handler.
Scroll up and select the Configuration tab.
- Click on the Environment Variable option on the left and add the following variables inside it by clicking on Edit and then Add environment variable.
ADMIN_API_KEY: <<Your Algolia Admin API Key you generated in the above step>>
APPLICATION ID: <<Your Algolia Application ID generated in the above step>>
INDEX_NAME: <<The name of the Algolia index you created in the above step>>
- After adding these details, click on the Save button.
Now that you have configured your lambda, let's move on and configure the AWS API Gateways.
Set up API Gateway
For your Lambda to be accessible publicly through an HTTP POST, you need to configure the AWS API Gateway using your function as the backend for the API. Follow the steps given below set up the API gateway.
- scroll up to the Function overview section and click on + Add trigger.
- On the Add trigger screen, from the Select a trigger dropdown, select API Gateway.
- From the API dropdown, select Create an API. Then, select REST API inside the API type block, select Open from the Security dropdown. Your settings after performing the above steps should look similar to the following:
- Click on Add to save your settings.
- An API for your lambda function is now created. Inside the Triggers section, you will see the Details link. Click on it and you will find your API endpoint. Make a note of it as we will need it while setting up our webhook in Contentstack.
Set up Webhooks in Contentstack
To create and set up a webhook in log in to your Contentstack account and perform the following steps:
- Click the “Settings” icon on the left navigation panel and click on Webhooks
- On the Webhook page, click on + New Webhook.
- On the Create Webhook page, fill up the Name field (for example: Algolia). In the URL to notify field, enter the URL that you generated when you deployed your APIs, in the previous step.
- Scroll down to the Conditional View section for creating a trigger for the webhook as shown below:
- Select the Enable Webhook checkbox option and Save your settings.
With this, we have set up the webhooks for triggering notifications. Let's move on to setting up Algolia.
Create and Publish Entry
Now that the set up is ready, let's publish an entry in Contentstack. Once the entry is published, go to the Algolia dashboard and click on Indices.
You will see that the index you specified in your configuration has your entry that you just published. In the search bar of your Algolia dashboard, you can search for any word of your entry.
Additionally, you make search API requests from your website to your app server to perform search operations using the API gateway that you created in step 3.