将解析类导入到阿尔及利亚

泰勒苏克

我正在尝试将我的课程(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;
    }
});

});

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章