Search Parse Data
On this page
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 JavaScript API client simplifies the integration of your Parse based applications with Algolia’s real time search service.
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 Algolia within your Parse application, install our JavaScript API client:
1
npm install algoliasearch
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
const algoliasearch = require('algoliasearch');
const client = algoliasearch('YourApplicationID', 'YourAdminAPIKey');
const index = client.initIndex('contacts');
const indexData = () => {
// Create a new query for Contacts
const query = new Parse.Query('Contact');
// Find all items
query.find({
success(contacts) {
// Prepare objects to index from contacts
const objectsToIndex = contacts.map(contact => {
// Convert to JavaScript object and specify Algolia's objectID with the Parse.Object unique ID
return { ...contact.toJSON(), ...{ objectID: contact.objectId } };
});
// Add or update new objects
index.saveObjects(objectsToIndex, (err, content) => {
if (err) {
throw err;
}
console.log('Parse<>Algolia import done');
});
},
error(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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
const tempIndexName = 'contacts_temp';
const mainIndexName = 'contacts';
const algoliasearch = require('algoliasearch');
const client = algoliasearch('YourApplicationID', 'YourAdminAPIKey');
const tempIndex = client.initIndex(tempIndexName);
const reindexData = () => {
let objectsToIndex = [];
// Create a temp index
const tempIndex = client.initIndex(tempIndexName);
// Create a new query for Contacts
const query = new Parse.Query('Contact');
// Find all items
query.find({
success(contacts) {
// prepare objects to index from contacts
objectsToIndex = contacts.map(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, (err, content) => {
if (err) {
throw err;
}
// Overwrite main index with temp index
client.moveIndex(tempIndexName, mainIndexName, (err, content) => {
if (err) {
throw err;
}
console.log('Parse<>Algolia reimport done');
});
});
},
error(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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
const algoliasearch = require('algoliasearch');
const client = algoliasearch('YourApplicationID', 'YourAdminAPIKey');
const index = client.initIndex('contacts');
Parse.Cloud.afterSave('Contact', ({object}) => {
// Convert Parse.Object to JSON
const objectToSave = object.toJSON();
// Specify Algolia's objectID with the Parse.Object unique ID
objectToSave.objectID = objectToSave.objectId;
// Add or update object
index.saveObject(objectToSave, (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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
const algoliasearch = require('algoliasearch');
const client = algoliasearch('YourApplicationID', 'YourAdminAPIKey');
const index = client.initIndex('contacts');
Parse.Cloud.afterDelete('Contact', ({object}) => {
// Get Algolia objectID
const objectID = object.id;
// Remove the object from Algolia
index.deleteObject(objectID, (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.