-
Notifications
You must be signed in to change notification settings - Fork 0
/
indexing.js
86 lines (70 loc) · 2.42 KB
/
indexing.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import {Client} from "@elastic/elasticsearch"
import fs from 'fs'
const client = new Client({
node: "http://localhost:9200"
})
client.info()
.then(response => console.log(response))
.catch(error => console.error(error))
const readFiles1 = (dirname) => {
return new Promise((resolve, reject) => {
let dataset = []
fs.readdir(dirname, function(err, filenames) {
if (err) {
reject(err)
return;
}
filenames.forEach(function(filename) {
let data = {};
data['disease'] = filename.substring(0, filename.lastIndexOf('.')) || filename
data['symptoms'] = fs.readFileSync(dirname + filename, 'utf-8')
dataset.push(data);
})
resolve(dataset)
})
})
}
const getData = async() => {
const dataset = await readFiles1('./drugs-data-master/disease-symptoms/imp-symptoms/')
console.log(dataset);
run(dataset).catch(console.log)
}
async function run (dataset) {
await client.indices.create({
index: 'disease-symptoms',
operations: {
mappings: {
properties: {
disease: { type: 'keyword' },
symptoms: { type: 'text' },
}
}
}
}, { ignore: [400] })
const operations = dataset.flatMap(doc => [{ index: { _index: 'disease-symptoms' } }, doc])
const bulkResponse = await client.bulk({ refresh: true, operations })
if (bulkResponse.errors) {
const erroredDocuments = []
// The items array has the same order of the dataset we just indexed.
// The presence of the `error` key indicates that the operation
// that we did for the document has failed.
bulkResponse.items.forEach((action, i) => {
const operation = Object.keys(action)[0]
if (action[operation].error) {
erroredDocuments.push({
// If the status is 429 it means that you can retry the document,
// otherwise it's very likely a mapping error, and you should
// fix the document before to try it again.
status: action[operation].status,
error: action[operation].error,
operation: body[i * 2],
document: body[i * 2 + 1]
})
}
})
console.log(erroredDocuments)
}
const count = await client.count({ index: 'disease-symptoms' })
console.log(count)
}
// getData()