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.
- 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 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 on 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, you will see different options on the left-side panel. Click on API Keys.
- Clicking on API Keys will open up the API Keys and 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.
- Log on to Algolia and create your account.
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 AWS Lambda Service from the service list.
- Click on the Create Function button, and then the Author from Scratch option.
- Configure the lambda based on your requirements. Choose Node.js as your run-time language. Click on the Create Function button to create the Lambda Function.
- AWS Lambda offers an inline code editor. You can write your lambda code here. Another option is to write the lambda code on your machine and then load it in the code editor. For our example, we have created a sample code. Contact our support team at firstname.lastname@example.org to get the code. Once you get code, download it and unzip the project.
- 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.
- In the Lambda function handler, keep algolia/index.handler as Handler.
- In the in-line code editor, click on the src folder, and then click on the config.js file.
- Provide a name of your index, for example, my index.
- Then, provide the applicationID and adminAPIkey that you generated in step 1, when you created Algolia account.
- Review your setting and click on the Save option if everything you entered is correct.
The whole set up will look similar to the one shown below:
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.
- Login to AWS Management Console and click on your Lambda Function that you just created in the above step.
- Click on + Add trigger.
- In the Trigger configuration drop-down, select API Gateway.
- In the API drop-down, select Create a new API.
- Under the Security drop-down, select Open for now. Your settings after performing the above steps should look similar to the following:
- Click on Add to save your settings.
You will now get an API URL that you can use in the next step while configuring webhooks.
Click on the URL that is generated. You should get an "Unknown operation" message.
Set up Webhooks in Contentstack
Next, you need to create and set up a webhook in Contentstack. Proceed with the following steps to create a webhook in Contentstack:
- Log in to your Contentstack account and navigate to Settings.
- Then, 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 When section for creating a trigger for the webhook as shown below:
Set up webhooks for the following events:
All Entries: publish, unpublish
All Assets: publish, unpublish
Content type: delete
- Once done, click on the Save button to 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.