Skip to content

Instantly share code, notes, and snippets.

@japel
Created April 24, 2015 08:59
Show Gist options
  • Save japel/be3dbc0bad6b86c6b8b0 to your computer and use it in GitHub Desktop.
Save japel/be3dbc0bad6b86c6b8b0 to your computer and use it in GitHub Desktop.
function bulkIndexData(data, index, type) {
var temp = Q.when({});
var newArr = [];
for (var k = 0; k < data.length; k++) {
var oneElement = data[k];
newArr.push({"index": {"_index": index, "_type": type, "_id": oneElement.data.id}});
newArr.push(oneElement.data);
}
(function (newArr) {
temp = temp.then(function () {
return persist(newArr);
});
})(newArr);
return temp;
function persist(dataChunk) {
var d = Q.defer();
var client = SearchConnectorService.createClient();
client.bulk({body: dataChunk}).then(function (resp) {
if (resp.errors) {
console.log(JSON.stringify(resp.errors));
}
console.log("ready writing chunk");
d.resolve();
}, function (error) {
console.log("error writing chunk", error);
d.reject();
}).lastly(function () {
client.close();
dataChunk = null;
});
return d.promise;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment