-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestore.js
68 lines (53 loc) · 1.95 KB
/
restore.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
const fs = require('fs');
const path = require ('path');
const inputDir = './db';
const outputDir = './db_output';
// Ensure output directory exists
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir);
}
// Read all files from the input directory
fs.readdirSync(inputDir).forEach((file) => {
// Only process .json files
if (path.extname(file) === '.json') {
// Read the JSON file
const searchOutput = JSON.parse(fs.readFileSync(path.join(inputDir, file), 'utf-8'));
let bulkData = "";
searchOutput.hits.hits.forEach(hit => {
// Indexing operation
const indexOperation = {
index: {
_index: hit._index,
_id: hit._id,
},
};
// Corresponding document
const document = hit._source;
bulkData += `${JSON.stringify(indexOperation)}\n${JSON.stringify(document)}\n`;
})
// Write the transformed data to a new file in the output directory
const outputFilename = 'output' + path.basename(file, '.json') + '.json';
fs.writeFileSync(path.join(outputDir, outputFilename), bulkData);
//exec in future
//curl -s -H "Content-Type: application/x-ndjson" -XPOST localhost:9200/_bulk --data-binary output.json
}
});
// let bulkData = "";
// searchOutput.hits.hits.forEach(hit => {
// // Indexing operation
// const indexOperation = {
// index: {
// _index: hit._index,
// _id: hit._id,
// },
// };
// // Corresponding document
// const document = hit._source;
// bulkData += `${JSON.stringify(indexOperation)}\n${JSON.stringify(document)}\n`;
// });
// // Add trailing newline required by Bulk API
// bulkData += '\n';
// //bulkOperations.push('');
// // Write to output file
// //fs.writeFileSync('bulkOperations.json', bulkOperations.join('\n'));
// fs.writeFileSync(path.join(__dirname, 'output.json'), bulkData);