-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathesInsertGeneral.js
More file actions
90 lines (82 loc) · 2.55 KB
/
esInsertGeneral.js
File metadata and controls
90 lines (82 loc) · 2.55 KB
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
87
88
89
90
const request = require('axios');
const _ = require('lodash');
const fs = require('fs-extra');
const path = require('path');
const es = require('elasticsearch');
const hash = require('string-hash');
const INDEX_NAME = 'askit';
const TYPE_NAME = 'kb';
const univService = '';
const apiPath = 'https://nyudev.service-now.com/api/neyu/knowledge_for_bot/articles?univ_service=';
const ES_USERNAME = 'es4askit'
const ES_PASSWORD = 'codyGo4'
const ES_HOST = [{
host: '18.216.253.76',
port: 80,
auth: ES_USERNAME + ':' + ES_PASSWORD
}]
const client = new es.Client({
host: ES_HOST,
log: 'warning',
});
const ES_BULK_LIMIT = 100;
function createBulk(results, servicesMapping) {
const bulksList = [];
results.forEach((item, index) => {
if (index % ES_BULK_LIMIT === 0) {
bulksList[bulksList.length] = [];
}
bulksList[bulksList.length - 1].push({
index: {
_index: INDEX_NAME,
_type: TYPE_NAME,
_id: item.kb_id,
},
});
bulksList[bulksList.length - 1].push({
serviceId: parseInt(servicesMapping[item.UniversityService], 10),
serviceName: item.UniversityService,
description: unescape(item.short_description) || '',
serviceTower: item.service_tower || '',
keywords: unescape(item.keywords) || '',
solution: unescape(item.solution) || '',
solutionUrl: unescape(item.url) || '',
contexts: [0],
});
// console.log(typeof (bulksList[bulksList.length - 1].service));
});
return bulksList;
}
function makeESRequests(bulksList) {
const esInsertRequests = [];
bulksList.forEach((body) => {
esInsertRequests.push(client.bulk({ body }));
})
return Promise.all(esInsertRequests);
}
client.ping({
requestTimeout: 1000,
})
.then(() => request.get(apiPath + univService, {
auth: {
username: 'tst710',
password: 'tst710',
},
}), (error) => {
console.trace(error.message);
})
.then((resp) => {
// console.log(_.uniq(_.map(resp.data.results, 'service_tower')));
const uniqueServices = _.uniq(_.map(resp.data.results, 'UniversityService'));
const uniqueServiceIds = _.map(uniqueServices, service => hash(service.trim()));
const uniqueServicesObj = _.zipObject(uniqueServices, uniqueServiceIds);
fs.writeFile(path.join(__dirname, 'serviceToIdMapping.json'), JSON.stringify(uniqueServicesObj));
const body = createBulk(resp.data.results, uniqueServicesObj);
return makeESRequests(body);
})
.then((respOfBulk) => {
console.log('Bulk Insert');
})
.catch((err) => {
console.log('ERROR OCCURRED!', err);
});