Advertisement
turist_ua

some elastic pain

Jul 21st, 2019
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const bulkPostRecipe = data => {
  2.   console.log('trying to execute bulk');
  3.   client.bulk({ body: data }, (data1, data2) => console.log(data1, data2));
  4. };
  5.  
  6. exports.onPostBuild = async ({ getNodes }) => {
  7.   console.log('on post build');
  8.  
  9.   const nodes = getNodes();
  10.   const recipes = nodes.filter(node => node.internal.type === 'Recipe');
  11.   const batchSize = 10;
  12.   const noOfBatches = Math.ceil(recipes.length / batchSize);
  13.   let startItem = 0;
  14.   for (let i = 0; i < noOfBatches; i++) {
  15.     const endItem = startItem + batchSize;
  16.     const bulkRows = [];
  17.     recipes.slice(startItem, endItem).forEach(recipe => {
  18.       if (recipe) {
  19.         // each datarow requires a header row like the belwo
  20.         const headerRow = {
  21.           index: {
  22.             _index: keys.elasticSearch.index,
  23.             _type: '_doc',
  24.             _id: recipe.recipeId,
  25.           },
  26.         };
  27.         bulkRows.push(headerRow);
  28.         const dataRow = {
  29.           id: recipe.recipeId,
  30.           title: recipe.title,
  31.         };
  32.         bulkRows.push(dataRow);
  33.       }
  34.     });
  35.     // format required by ES needs newline at end of each row
  36.     bulkRows.length && bulkPostRecipe(bulkRows);
  37.     if (endItem > recipes.length) break;
  38.   }
  39. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement