AppDividend
Latest Code Tutorials

Node js Algolia Search Tutorial

367

Get real time updates directly on you device, subscribe now.

Node js Algolia Search Tutorial is today’s leading topic. For this example, we do not use express.js. Just see an overview of how we can integrate algolia search and index the data. Also, we fetch the data and display inside the node.js console based on the query. We start this example by installing npm package and then create an account in algolia and then grab the api keys. After that, we programmatically index the data into the database and then query that data. Display that data in the node.js console. In a real-time application, we send an ajax request containing our query and then in response, we get an array of data and loop through in frontend and display the data. Let us start Node js Algolia Search Tutorial with an example.

Node js Algolia Search Tutorial

Create a node.js project folder using the following command.

#Step 1: Install 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 to the algolia, then you can either create your account using your email, or you can sign up with Github. After that, you need to get APP_ID and API_KEY and ADMIN_KEY. You can find both at this URL: https://www.algolia.com/api-keys

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 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 the packages.

  1. algoliasearch
  2. dotenv

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

We have already defined 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 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 the Algolia. So, create an object that describes the data.

In my case, I have created the index called getstarted_result and my data 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. 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. In general scenario, 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 hard coded for the demo purpose.

Related Posts
1 of 16

Now, go to the terminal inside the root of the project 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 spelling typo then also it will fetch the related results, and that is the power of algolia.

In the real-time scenario, you will get the query from an ajax request or simple form request and 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 like 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 the Algolia to our Node.js application. In the next tutorials, we will use express and mongodb database to simulate the real-time experience.

Finally, Node js Algolia Search Tutorial is over. Thanks for taking.

Leave A Reply

Your email address will not be published.

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