Skip to content

Instantly share code, notes, and snippets.

@antronic
Created March 30, 2023 05:01
Show Gist options
  • Save antronic/6fd811dc58a44772660551ab870c32ff to your computer and use it in GitHub Desktop.
Save antronic/6fd811dc58a44772660551ab870c32ff to your computer and use it in GitHub Desktop.
const fs = require("fs");
const path = require("path");
const { SearchIndexClient, AzureKeyCredential } = require("@azure/search-documents");
// Replace with your search service name and admin api key
const searchServiceName = "<your-search-service-name>";
const adminApiKey = "<your-search-service-admin-api-key>";
// Replace with your index name and field name
const indexName = "<your-index-name>";
const fieldName = "<your-field-name>";
async function run() {
// Create a search index client
const credential = new AzureKeyCredential(adminApiKey);
const client = new SearchIndexClient(searchServiceName, indexName, credential);
// Loop through files in the 'data' folder
const dataFolderPath = path.join(__dirname, "data");
const dataFiles = fs.readdirSync(dataFolderPath);
for (const dataFile of dataFiles) {
const dataFilePath = path.join(dataFolderPath, dataFile);
// Check if the file is json, excel, or csv
if (path.extname(dataFile) === ".json") {
// Upload json file to the search index
const dataFileBuffer = fs.readFileSync(dataFilePath);
const dataDocuments = JSON.parse(dataFileBuffer.toString());
await client.uploadDocuments(dataDocuments.map((doc) => ({ [fieldName]: doc })));
} else if (path.extname(dataFile) === ".xlsx" || path.extname(dataFile) === ".xls") {
// Convert excel file to an array of documents
const dataFileBuffer = fs.readFileSync(dataFilePath);
const dataDocuments = await convertExcelToDocuments(dataFileBuffer);
// Upload excel documents to the search index
await client.uploadDocuments(dataDocuments.map((doc) => ({ [fieldName]: doc })));
} else if (path.extname(dataFile) === ".csv") {
// Convert csv file to an array of documents
const dataFileBuffer = fs.readFileSync(dataFilePath);
const dataDocuments = await convertCsvToDocuments(dataFileBuffer);
// Upload csv documents to the search index
await client.uploadDocuments(dataDocuments.map((doc) => ({ [fieldName]: doc })));
}
}
}
async function convertExcelToDocuments(excelFileBuffer) {
// Implement your own logic to convert the excel file to an array of documents
// Each document should be an object with field names as keys and field values as values
// For example:
return [
{ name: "John", age: 30, city: "New York" },
{ name: "Jane", age: 25, city: "San Francisco" },
{ name: "Bob", age: 40, city: "Chicago" },
];
}
async function convertCsvToDocuments(csvFileBuffer) {
// Implement your own logic to convert the csv file to an array of documents
// Each document should be an object with field names as keys and field values as values
// For example:
return [
{ name: "John", age: 30, city: "New York" },
{ name: "Jane", age: 25, city: "San Francisco" },
{ name: "Bob", age: 40, city: "Chicago" },
];
}
run().catch((err) => console.error(err));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment