Add Algolia Search to Contentstack-powered Websites using AWS Lambda

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.

Pre-requisites

  • 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:

  1. Create an account in Algolia
  2. Create an AWS Lambda function
  3. Set up API gateway
  4. Set up webhooks in Contentstack
  5. Create and publish an entry
  1. 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:

    1. 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

    2. After you log in to Algolia, you will see a successful message with options to choose from. Click on Go to dashboard.Algolia Dashboard.PNG
    3. On your Algolia dashboard screen, you will see different options on the left-side panel. Click on API Keys.Algolia Dashboard.PNG
    4. 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.

    API.PNG

  2. Create an AWS Lambda Function

    Create an AWS Lambda function that sends the data (received from Contentstack via webhooks) to Algolia.

    1. Login to your AWS Management Console, select AWS Lambda Service from the service list.
    2. Click on the Create Function button, and then the Author from Scratch option.
    3. 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.
    4. 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 support@contentstack.com to get the code. Once you get code, download it and unzip the project.
    5. Then, fire up your terminal and move inside the project directory that you just downloaded.
    6. Install npm modules using the following command:
      npm install
    7. 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.
    8. In the Lambda function handler, keep algolia/index.handler as Handler.
    9. In the in-line code editor, click on the src folder, and then click on the config.js file.
    10. Provide a name of your index, for example, my index.
    11. Then, provide the applicationID and adminAPIkey that you generated in step 1, when you created Algolia account.
    12. 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:

    lambda.PNG

    Now that you have configured your lambda, let's move on and configure the AWS API Gateways.

  3. 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.

    1. Login to AWS Management Console and click on your Lambda Function that you just created in the above step.
    2. Click on + Add trigger.

      add trigger.PNG
    3. In the Trigger configuration drop-down, select API Gateway.
    4. In the API drop-down, select Create a new API.
    5. Under the Security drop-down, select Open for now. Your settings after performing the above steps should look similar to the following:

      trigger setup.PNG
    6. 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.

    API gateway.PNG

    Click on the URL that is generated. You should get an "Unknown operation" message.

  4. 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:

    1. Log in to your Contentstack account and navigate to Settings.
    2. Then, click on Webhooks. On the Webhook page, click on + New Webhook.
    3. 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.
    4. Scroll down to the When section for creating a trigger for the webhook as shown below:
      Algolia Dashboard.PNG
      Set up webhooks for the following events:

      All Entries: publish, unpublish
      All Assets: publish, unpublish
      Content type: delete

    5. 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.

  5. 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.

    Indices.PNG

    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.

On This Page

top-arrow