我正在尝试将我的课程(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-other-considerations
对于您当前的问题,您可以这样做:
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;
}
});