async function insertBookData (title, author, paragraphs) {
let bulkOps = [] // Array to store bulk operations

// Add an index operation for each section in the book
for (let i = 0; i < paragraphs.length; i++) {
// Describe action
bulkOps.push({ index: { _index: esConnection.index, _type: esConnection.type } })

// Add document
bulkOps.push({
  author,
  title,
  location: i,
  text: paragraphs[i]
})

if (i > 0 && i % 500 === 0) { // Do bulk insert in 500 paragraph batches
  await esConnection.client.bulk({ body: bulkOps })
  bulkOps = []
  console.log(`Indexed Paragraphs ${i - 499} - ${i}`)
}
}

// Insert remainder of bulk ops array
await esConnection.client.bulk({ body: bulkOps })
console.log(`Indexed Paragraphs ${paragraphs.length - (bulkOps.length / 2)} - ${paragraphs.length}\n\n\n`)
}
/** Bulk index the book data in Elasticsearch */
Bulk index the book data in Elasticsearch
Bulk index the book data in Elasticsearch
async function insertBookData (title, author, paragraphs) {
let bulkOps = [] 
for (let i = 0; i < paragraphs.length; i++) {
bulkOps.push({ index: { _index: esConnection.index, _type: esConnection.type } })
bulkOps.push({
  author,
  title,
  location: i,
  text: paragraphs[i]
})
if (i > 0 && i % 500 === 0) { 
  await esConnection.client.bulk({ body: bulkOps })
  bulkOps = []
  console.log(`Indexed Paragraphs ${i - 499} - ${i}`)
}
}
await esConnection.client.bulk({ body: bulkOps })
console.log(`Indexed Paragraphs ${paragraphs.length - (bulkOps.length / 2)} - ${paragraphs.length}\n\n\n`)
}
async function insertBookData ( title , author , paragraphs ) { let bulkOps = [ ] for ( let i = 0 ; i < paragraphs . length ; i ++ ) { bulkOps . push ( { index : { _index : esConnection . index , _type : esConnection . type } } ) bulkOps . push ( { author , title , location : i , text : paragraphs [ i ] } ) if ( i > 0 && i % 500 === 0 ) { await esConnection . client . bulk ( { body : bulkOps } ) bulkOps = [ ] console . log ( ` ${ i - 499 } ${ i } ` ) } } await esConnection . client . bulk ( { body : bulkOps } ) console . log ( ` ${ paragraphs . length - ( bulkOps . length / 2 ) } ${ paragraphs . length } \n \n \n ` ) }
Bulk index the book data in Elasticsearch
Bulk index the book data in Elasticsearch
// Array to store bulk operations // Add an index operation for each section in the book // Describe action // Add document // Do bulk insert in 500 paragraph batches // Insert remainder of bulk ops array