Created
April 24, 2015 08:59
-
-
Save japel/be3dbc0bad6b86c6b8b0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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