function pushDataToCluster()

in Code.gs [209:297]


function pushDataToCluster(index,index_type,template,data_range_a1,doc_id_range_a1) {
  var host = getHostData();
  isValidHost(host);

  if(!index) { throw "Index name cannot be empty." }
  if(index.indexOf(' ')>=0) { throw "Index should not have spaces." }

  if(!index_type) { throw "Index type cannot be empty." }
  if(index_type.indexOf(' ')>=0) { throw "Index type should not have spaces." }

  if(template && template.indexOf(' ')>=0) { throw "Template name should not have spaces." }

  if(!data_range_a1) { throw "Document data range cannot be empty." }


  var data_range = null;
  try {
    data_range = SpreadsheetApp.getActiveSheet().getRange(data_range_a1);
  } catch(e) {
    throw "The document data range entered was invalid. Please verify the range entered.";
  }
  var data = data_range.getValues();
  if(data.length <= 0) {
    throw "No data to push."
  }

  var headers = data.shift();
  for(var i in headers) {
    if(!headers[i]) {
      throw 'Document key name cannot be empty. Please make sure each cell in the document key names range has a value.';
    }
    headers[i] = headers[i].replace(/[^0-9a-zA-Z]/g,'_'); // clean up the column names for index keys
    headers[i] = headers[i].toLowerCase();
    if(!headers[i]) {
      throw 'Document key name cannot be empty. Please make sure each cell in the document key names range has a value.';
    }
  }

  var doc_id_data = null;
  if(doc_id_range_a1) {
    var doc_id_range = null;
    try {
      doc_id_range = SpreadsheetApp.getActiveSheet().getRange(doc_id_range_a1);
    } catch(e) {
      throw "The document id column entered was invalid. Please verify the id column entered."
    }
    doc_id_range = doc_id_range.offset(data_range.getRow(), 0,data_range.getHeight()-1);
    doc_id_data = doc_id_range.getValues();
  }

  var bulkList = [];
  if(template) { createTemplate(host,index,template); }
  var did_send_some_data = false;
  for(var r=0;r<data.length;r++) {
    var row = data[r];
    var toInsert = {};
    for(var c=0;c<row.length;c++) {
      if(row[c]) {
        toInsert[headers[c]] = row[c];
      }
    }
    if(Object.keys(toInsert).length > 0) {
      if(doc_id_data) {
        if(!doc_id_data[r][0]) {
          throw "Missing document id for data row: "+(r+1);
        }
        bulkList.push(JSON.stringify({ "update" : { "_index" : index, "_type" : index_type, "_id" : doc_id_data[r][0], "_retry_on_conflict" : 3 } }));
        bulkList.push(JSON.stringify({ doc: toInsert, detect_noop: true, doc_as_upsert: true }));
      } else {
        bulkList.push(JSON.stringify({ "index" : { "_index" : index, "_type" : index_type } }));
        bulkList.push(JSON.stringify(toInsert));
      }
      did_send_some_data = true;
      // Don't hit the UrlFetchApp limits of 10MB for POST calls.
      if(bulkList.length >= 2000) {
        postDataToES(host,bulkList.join("\n")+"\n");
        bulkList = [];
      }
    }
  }
  if(bulkList.length > 0) {
    postDataToES(host,bulkList.join("\n")+"\n");
    did_send_some_data = true;
  }
  if(!did_send_some_data) {
    throw "No data was sent to the cluster. Make sure your document key name and value ranges are valid.";
  }
  return [(host.use_ssl) ? 'https://' : 'http://', host.host,':',host.port,'/',index,'/',index_type,'/_search'].join('');
}