4

我正在尝试将我的课程(2500 条记录)从 Parse.com 导入到 Algolia 索引中。默认情况下限制为 100 条记录,这显然不适合我。即使我使用query.limit = 1000;

下面的代码如何用于导入我的整个班级?

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;
    }
});

});
4

1 回答 1

2

Parse.com 有一个查找限制,以及当您想要“获取所有对象”时的所有其他限制,您可以在此处找到更多信息:https ://parse.com/docs/js/guide#performance-limits-and-其他考虑

对于您当前的问题,您可以这样做:

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;
    }
});
于 2015-11-18T16:11:16.750 回答