Icon indexing white

Search on top of your Parse.com data with Algolia

Last updated 12 October 2017

Introduction

One key feature of Parse is for applications to use Parse Core as their data store. In a few simple steps, this tutorial will teach you how to import your existing data, index new data as it is added to Parse, and remove indexed data when it is removed from Parse.

The Algolia Parse Module simplifies the integration of your Parse based applications with Algolia’s real time search service. The module makes it easy for you to use Algolia’s search capabilities in a manner that will be familiar to those already using the Algolia Node.js client APIs.

Prerequisites

Familiar with Parse

This tutorial assumes you are familiar with Parse, how it works, and how to build Cloud Code applications. If you would like to learn more before continuing with this tutorial, we suggest reading the following documentation and tutorials:

Add Algolia Realtime Search to the Project

In order to integrate Aloglia within your Parse application, you will need to add the Algolia Node.js client. Copy algoliasearch.parse.js to cloud/algoliasearch.parse.js within your Parse Cloud Code directory.

Import Existing Data

In many cases, you may already have data within your Parse application. In order to integrate with Algolia, you will want to index that data. We will use contact information being stored within Parse as our example.

var algoliasearch = require('cloud/algoliasearch.parse.js');
var client = algoliasearch('YourApplicationID', 'YourAPIKey');
var index = client.initIndex('contacts');
function indexData() {
  var objectsToIndex = [];
  //Create a new query for Contacts
  var query = new Parse.Query('Contact');
  // Find all items
  query.find({
    success: function(contacts) {
      // prepare objects to index from contacts
      objectsToIndex = contacts.map(function(contact) {
        // convert to regular key/value JavaScript object
        contact = contact.toJSON();
        // Specify Algolia's objectID with the Parse.Object unique ID
        contact.objectID = contact.objectId;
        return contact;
      });
      // Add or update new objects
      index.saveObjects(objectsToIndex, function(err, content) {
        if (err) {
          throw err;
        }
        console.log('Parse<>Algolia import done');
      });
    },
    error: function(err) {
      throw err;
    }
  });
}

You can now use this function within your own Parse Cloud Code functions in order to index your existing data.

To ensure the reindexing performs well it is suggested you limit the number of items indexed per call between 1,000 and 10,000 depending on the object size.

Reindex Data

Sometimes, you might have the need to completely reindex your data. This means removing data from the index that may not longer exist, adding new data, and updating existing data. The following code can be used within your own Parse Cloud Code functions to perform a reindexing.

var tempIndexName = 'contacts_temp';
var mainIndexName = 'contacts';
var algoliasearch = require('cloud/algoliasearch.parse.js');
var client = algoliasearch('YourApplicationID', 'YourAPIKey');
var tempIndex = client.initIndex(tempIndexName);
var reindexData = function() {
  var objectsToIndex = [];
  // Create a temp index
  var tempIndex = client.initIndex(tempIndexName);
  // Create a new query for Contacts
  var query = new Parse.Query('Contact');
  // Find all items
  query.find({
    success: function(contacts) {
      // prepare objects to index from contacts
      objectsToIndex = contacts.map(function(contact) {
        // convert to regular key/value JavaScript object
        contact = contact.toJSON();
        // Specify Algolia's objectID with the Parse.Object unique ID
        contact.objectID = contact.objectId;
        return contact;
      });
      // Add new objects to temp index
      tempIndex.saveObjects(objectsToIndex, function(err, content) {
        if (err) {
          throw err;
        }
        // Overwrite main index with temp index
        client.moveIndex(tempIndexName, mainIndexName, function(err, content) {
          if (err) {
            throw err;
          }
          console.log('Parse<>Algolia reimport done');
        });
      });
    },
    error: function(err) {
      throw err;
    }
  });
};

To ensure the reindexing performs well it is suggested you limit the number of items indexed per call between 1,000 and 10,000 depending on the object size.

Add or Update Data

Now, we need to handle the case where data is being added or updated. We can easily setup our code to automatically add or update data to our search index by using the afterSave Parse function. This will allow us to define code that will be called after data is stored in Parse.

var algoliasearch = require('cloud/algoliasearch.parse.js');
var client = algoliasearch('YourApplicationID', 'YourAPIKey');
var index = client.initIndex('contacts');
Parse.Cloud.afterSave('Contact', function(request) {
  // Convert Parse.Object to JSON
  var objectToSave = request.object.toJSON();
  // Specify Algolia's objectID with the Parse.Object unique ID
  objectToSave.objectID = objectToSave.objectId;
  // Add or update object
  index.saveObject(objectToSave, function(err, content) {
    if (err) {
      throw err;
    }
    console.log('Parse<>Algolia object saved');
  });
});

Now, whenever contact data is saved in Parse, it will automatically be indexed with Algolia.

Delete Data

Next, we need to handle the case where data is deleted from your Parse application. In order to do this, we can use the afterDelete Parse function. This will allow us to define code that will be called after data is removed from Parse.

var algoliasearch = require('cloud/algoliasearch.parse.js');
var client = algoliasearch('YourApplicationID', 'YourAPIKey');
var index = client.initIndex('contacts');
Parse.Cloud.afterDelete('Contact', function(request) {
  // Get Algolia objectID
  var objectID = request.object.id;
  // Remove the object from Algolia
  index.deleteObject(objectID, function(err, content) {
    if (err) {
      throw err;
    }
    console.log('Parse<>Algolia object deleted');
  });
});

Now, whenever contact data is removed from Parse, it will automatically get removed from Algolia.

© Algolia - Privacy Policy