Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const bulkPostRecipe = data => {
- console.log('trying to execute bulk');
- client.bulk({ body: data }, (data1, data2) => console.log(data1, data2));
- };
- exports.onPostBuild = async ({ getNodes }) => {
- console.log('on post build');
- const nodes = getNodes();
- const recipes = nodes.filter(node => node.internal.type === 'Recipe');
- const batchSize = 10;
- const noOfBatches = Math.ceil(recipes.length / batchSize);
- let startItem = 0;
- for (let i = 0; i < noOfBatches; i++) {
- const endItem = startItem + batchSize;
- const bulkRows = [];
- recipes.slice(startItem, endItem).forEach(recipe => {
- if (recipe) {
- // each datarow requires a header row like the belwo
- const headerRow = {
- index: {
- _index: keys.elasticSearch.index,
- _type: '_doc',
- _id: recipe.recipeId,
- },
- };
- bulkRows.push(headerRow);
- const dataRow = {
- id: recipe.recipeId,
- title: recipe.title,
- };
- bulkRows.push(dataRow);
- }
- });
- // format required by ES needs newline at end of each row
- bulkRows.length && bulkPostRecipe(bulkRows);
- if (endItem > recipes.length) break;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement