Import Parse class to algolia

Taylorsuk

I am trying to import my class (2500 records) from Parse.com into an Algolia index. There is a limit of 100 records by default which obviously is not working for me. Even if I use query.limit = 1000;

How can the below code be used to import my whole class?

Parse.Cloud.define("createIndex", function(request, response) {

var algoliasearch = require('cloud/algoliasearch.parse.js');
var client = algoliasearch('9PsdfsdWVU7', '3b24e897bfb4esdfsdfsdf209e25c28');
var index = client.initIndex('exercises');

console.log("running");

var objectsToIndex = [];

//Create a new query for Contacts
var query = new Parse.Query('Exercises');

query.limit = 1000;
// Find all items
query.find({
    success: function(exercises) {
        // prepare objects to index from contacts
        objectsToIndex = exercises.map(function(exercise) {
            // convert to regular key/value JavaScript object
            exercise = exercise.toJSON();

            // Specify Algolia's objectID with the Parse.Object unique ID
            exercise.objectID = exercise.objectId;

            return exercise;
        });

        // Add or update new objects
        index.saveObjects(objectsToIndex, function(err, content) {
            if (err) {
                throw err;
            }

            console.log('Parse<>Algolia import done');
        });
        response.success("worked");
    },
    error: function(err) {
        response.error("failed");
        throw err;
    }
});

});
vvo

Parse.com has a find limit, and all other limitations when you want to "get all the objects", you can find more information here: https://parse.com/docs/js/guide#performance-limits-and-other-considerations

For your current issue you could do this:

Parse.Cloud.define("createIndex", function(request, response) {
    var algoliasearch = require('cloud/algoliasearch.parse.js');
    var client = algoliasearch('9PsdfsdWVU7', '3b24e897bfb4esdfsdfsdf209e25c28');
    var index = client.initIndex('exercises');

    console.log("running");

    //Create a new query for Contacts
    var Exercises = Parse.Object.extend('Exercises');
    var query = new Parse.Query(Exercises);
    var skip = -1000;
    var limit = 1000;

    saveItems();

    function saveItems() {
        skip += limit;
        query.skip(skip + limit);
        query.limit(limit);
        query.find({success: sucess, error: error});
    }

    function sucess(exercices) {
        if (exercices.length === 0) {
            response.success("finished");
            return;
        }


        exercises = exercises.map(function(exercise) {
            // convert to regular key/value JavaScript object
            exercise = exercise.toJSON();

            // Specify Algolia's objectID with the Parse.Object unique ID
            exercise.objectID = exercise.objectId;

            return exercise;
        });

        index.saveObjects(exercises, function(err) {
            if (err) {
                throw err;
            }

            console.log('Parse<>Algolia import done');
            response.success("worked");
            saveItems();
        });

    }

    function error() {
        response.error("failed");
        throw err;
    }
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related