How to Implement Algolia Search in Node.js

Algolia is a search engine API that allows developers to add search functionality to their applications.

Algolia search can be implemented in Node.js by installing the algoliasearch package, a client library for communicating with the Algolia API.

Let’s implement the algolia search step by step in Node.js

Step 1: Install the algolia package.

mkdir node-algolia

Go into the project folder.

cd node-algolia

Initialize the package.json file.

npm init

Install the algoliasearch using the following command.

npm install algoliasearch --save

Step 2: Configure the algolia.

If you have not signed up for the algolia, you can create your account using your email or sign up with GitHub. After that, you need to get APP_ID and API_KEY, and ADMIN_KEY.

Create a the.env file inside our node.js project root and add the following key with the values.

// .env

APP_KEY=your app key
ADMIN_KEY=your admin key
SEARCH_ONLY_KEY=your search only key

Now install the dotenv package using the following command.

npm install dotenv

Step 3: Create a server.js file.

Inside the project root, create one file called server.js and add the following command.

// server.js

const algoliasearch = require('algoliasearch');
require('dotenv').config()
const client = algoliasearch(process.env.APP_KEY, process.env.ADMIN_KEY);

We have included both packages.

  1. algoliasearch
  2. dotenv

Now, configure the algoliaclient by passing the APP_KEY and ADMIN_KEY.

We have already defined it in the .env file; now, we need to use those variables.

We have used the admin key because we need to push the data to the algolia. SEARCH_ONLY_KEY won’t help us to push the data. For indexing, we push the data to the algolia and then query that data.

Step 4: Push the data.

First, we need to create the index in the algolia dashboard. Go to the indices section, and you can see your existing indexes or no index if you have logged in for the first time. Now, all you need to do is create an index.

Node js Algolia Search Tutorial

We will push dummy data to Algolia. So, create an object that describes the data.

In my case, I have created the index called getstarted_result, and my data is an array of two items. But you need to create the index, and that is it.

For creating the new index, you can see in the image that there is an index button. Click that button, and there is a dialog box with the textbox. First, you need to enter your index name. In my case, it is getstarted_results.

Now, we need to initialize that index inside our node.js application. So write the following code inside the server.js file.

// server.js

const index = client.initIndex('getstarted_results');
var objects = [{
    firstname: 'Jimmie',
    lastname: 'Barninger'
  }, {
    firstname: 'Warren',
    lastname: 'Speach'
  }];

index.addObjects(objects, function(err, content) {
    if (err) {
      console.error(err);
    }
});

So, I have written the code to add the objects to the algolia.

Generally, this array of data is real-time data from the database. So, we have to index all of our data by the algolia. I have written this data hardcoded for the demo purpose.

Now, go to the terminal inside the project’s root and type the following command.

node server

You can see that we have pushed data successfully to the algolia. Now, we are ready to fetch that data based on our query. Let us fetch that data.

Step 5: Fetch the data.

Write the following code inside the server.js file.

// server.js 


index.search('war', function(err, actor) {
  console.log(actor.hits);
});

Here “war” is our query. So we need to fetch the data that contains war. If there is a spelling typo, it will also fetch the related results, which is the power of algolia.

In the real-time scenario, you will get the query from an ajax request or simple form request, extract the query parameter from the request, and pass that query to the algolia search function, and the rest of the work will be done by algolia.

Go to the terminal and again type the following command.

node server

You will get the data below.

Node algolia search example

If you are using Angular, React, or Vue.js, then you need to send a JSON response to one of these frameworks, and then they will loop through our data and display that data to the front end.

So, we have successfully integrated Algolia into our Node.js application. In the next tutorials, we will use the express and mongodb databases to simulate the real-time experience.

That’s it.

1 thought on “How to Implement Algolia Search in Node.js”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.