Export a CSV file from Solr with Express.js / Node

There are several libraries which claim to help you export CSV or XLSX in Node, but I found it was simple to implement this manually.

In this example, I’m going to export from a Solr resultset. To do this, you need to build a URL on your own, then write a get. Once this returns, you can parse the JSON, and pass it on to your CSV exporter.

For completeness, here is the call to Solr:

get({
  host: "localhost", 
  port: 3000, 
  path: url
}, 
function(res) {
  res.setEncoding("utf8");
  let data = "";
    
  res.on("data", (chunk) => {
    data += chunk;
  });
    
  res.on("end", () => {
    try {
      let searchResults = JSON.parse(data);       
      onComplete(searchResults);
    } catch (e) {
      console.log(e);
    }
  });
)

To make a CSV file come out correctly, you need to set a couple headers correctly:

response.header("Content-Type", "text/csv");
response.header("Content-Disposition", "attachment; filename=search.csv");

You’ll also need to know the columns you’re exporting:

let columns = [
  "speakerName_s",
  "download_s",
  "title_s"
];

Once you do this, you’ll need to loop through the Solr results and print it out.

response.write(columns.join(","));
response.write("\n");

for (let j = 0; j < searchResults.response.docs.length; j++) {
  let doc = searchResults.response.docs[j];
  let row = '';
  
  for (let i = 0; i < columns.length; i++) {
    let column = columns[i];
    let value: any = doc[column];
          
    if (value === undefined) {
      value = '';
    } else if (value instanceof Array) {
      value = value.join(",");
    } else {
      value = value + '';
    }
          
    row += '"' + value.replace(/"/g, '""') + '",'; 
  }
        
  response.write(row);
  response.write("\n");
}
            
response.end();