-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
308 lines (285 loc) · 8.92 KB
/
index.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
var recursive = require('recursive-readdir')
var elasticsearch = require('elasticsearch')
var _ = require('lodash')
var path = require('path')
var fs = require('graceful-fs')
var AgentKeepAlive = require('agentkeepalive')
var Elasticdump = require('elasticdump')
var debug = require('debug')('canary-perch:index')
var factIndexMapping = require('./snippetfacts-mapping.json')
// =====================================================================================
// FUNCTIONS TO GET CONTENT INTO THE INDEX
// recursively looks through a given folder to upload eupmc_result.json contents
// to elastic search
var ESClient = function (hosts) {
if (!hosts) throw new Error('no elastichost host')
var client = new elasticsearch.Client({
apiVersion: '2.3',
hosts: hosts,
maxSockets: 20,
maxRetries: 50,
createNodeAgent: function (connection, config) {
return new AgentKeepAlive(connection.makeAgentConfig(config))
}
})
return client
}
var errorPrintingCB = function (error) {
if (error) {
console.log(error)
}
}
var uploadJSONFileToES = function (file, index, type, client, cprojectID, cb) {
fs.readFile(file, function (err, data) {
if (err) throw err
var document = JSON.parse(data)
document.cprojectID = cprojectID
client.create({
index: index,
type: type,
body: document
}, cb)
})
}
var uploadXMLFileToES = function (file, index, type, client, cprojectID, cb) {
fs.readFile(file, function (err, data) {
if (err) throw err
client.index({
index: index,
type: type,
body: {
'fulltext': data.toString('utf8'),
'cprojectID': cprojectID
}
}, cb)
})
}
var loadEuPMCFullTexts = function (folder, hosts, index, cb) {
var client = ESClient(hosts)
console.log('reading fulltexts from disk')
recursive(folder, function (err, files) {
if (err) throw err
var errorWrappingDone = function (err) {
if (err) throw err
done()
}
var done = _.after(files.length, function () {
cb()
console.log('done all loading of files')
})
debug('list of files to consider: %O', files)
files.forEach(function (file) {
if (path.basename(file) === 'fulltext.xml') {
var cprojectID = path.basename(path.dirname(file))
debug('uploading fulltext from CProject: ' + cprojectID)
uploadXMLFileToES(file, index, 'unstructured', client, cprojectID, errorWrappingDone)
} else {
done()
}
})
})
}
var loadCRHTMLFullTexts = function (folder, hosts, cb) {
loadCRFullTexts(folder, 'fulltext.html', hosts, cb)
}
var loadCRXHTMLFullTexts = function (folder, hosts, cb) {
loadCRFullTexts(folder, 'fulltext.xhtml', hosts, cb)
}
var loadCRPDFFullTexts = function (folder, hosts, cb) {
loadCRFullTexts(folder, 'fulltext.pdf.txt', hosts, cb)
}
var loadCRFullTexts = function (folder, filename, hosts, cb) {
filename = filename || 'fulltext.html'
var client = ESClient(hosts)
console.log('reading fulltexts from disk')
recursive(folder, function (err, files) {
if (err) throw err
var done = _.after(files.length, function () {
cb()
console.log('done all loading of files')
})
files.forEach(function (file) {
if (path.basename(file) === filename) {
var cprojectID = path.basename(path.dirname(file))
// console.log("uploading fulltext from CProject: " + cprojectID)
uploadXMLFileToES(file, 'fulltext', 'unstructured', client, cprojectID, done)
} else {
done()
}
})
})
}
var indexEuPMCMetadata = function (folder, hosts, index) {
var client = ESClient(hosts)
console.log(folder)
recursive(folder, function (err, files) {
if (err) throw err
files.forEach(function (file) {
if (path.basename(file) === 'eupmc_result.json') {
var cprojectID = path.basename(path.dirname(file))
// console.log("Uploading file with cprojectID: " + cprojectID)
uploadJSONFileToES(file, index, 'eupmc', client, cprojectID, errorPrintingCB)
}
})
})
}
var indexCRMetadata = function (folder, hosts) {
var client = ESClient(hosts)
console.log(folder)
recursive(folder, function (err, files) {
if (err) throw err
files.forEach(function (file) {
if (path.basename(file) === 'crossref_result.json') {
var cprojectID = path.basename(path.dirname(file))
// console.log("Uploading file with cprojectID: " + cprojectID)
uploadJSONFileToES(file, 'metadata', 'crossref', client, cprojectID, errorPrintingCB)
}
})
})
}
var deleteFactIndex = function (err, hosts, index, cb) {
if (err) throw err
var client = ESClient(hosts)
// Use dummy callback to wedge in hosts
var dummyCallback = function (err) {
if ((err) && !(err.status === 404)) throw err
cb(undefined, hosts, cb)
}
client.indices.delete({
index: index
}, dummyCallback)
}
var mapFactIndex = function (err, hosts, index, cb) {
if ((err) && !(err.status === 404)) {
console.log(err)
throw err
}
var client = ESClient(hosts)
client.indices.create({
body: factIndexMapping,
index: index
}, cb)
}
var deleteAndMapFactIndex = function (err, hosts, index, cb) {
if (err) throw err
var dummyCallback = function () {
mapFactIndex(undefined, hosts, index, cb)
}
deleteFactIndex(undefined, hosts, index, dummyCallback)
}
var deleteAndMapMetadataIndex = function (err, hosts, cb) {
if (err) throw err
deleteMetadataIndex(undefined, hosts, mapMetadataIndex)
}
var deleteMetadataIndex = function (err, hosts, cb) {
if (err) throw err
var client = ESClient(hosts)
// dummy callbackto wedge in hosts
var dummyCallback = function (err) {
if (err) throw err
if (Meteor) {
Meteor.bindEnvironment(function () { cb(err, hosts) })
}
cb(err, hosts)
}
client.indices.delete({
index: 'metadata'
}, dummyCallback)
}
var mapMetadataIndex = function (err, hosts, cb) {
if ((err) && !(err.status === 404)) {
console.log(err)
throw err
}
var client = ESClient(hosts)
// ToDo: sort out mapping
var metadataMapping = require('metadataMap.json')
client.indices.create({
index: 'facts',
body: {
mappings: metadataMapping
}
}
, cb)
}
var deleteAndMapUnstructuredPaperIndex = function (err, hosts, index, cb) {
debug('deleting papers from hosts:' + hosts + ' and index:' + index)
if (err) throw err
var boundMapUnstructuredPaperIndex = function (err) {
mapUnstructuredPaperIndex(err, hosts, index, cb)
}
deleteUnstructuredPaperIndex(undefined, hosts, index, boundMapUnstructuredPaperIndex)
}
var deleteUnstructuredPaperIndex = function (err, hosts, index, cb) {
if (err) throw err
var client = ESClient(hosts)
function callback (err) {
if ((err) && !(err.status === 404)) throw err
cb()
}
client.indices.delete({index: index}, callback)
}
var mapUnstructuredPaperIndex = function (err, hosts, index, cb) {
if (err) throw err
var client = ESClient(hosts)
client.indices.create({
index: index,
body: {
'mappings': {
'unstructured': {
'properties': {
'cprojectID': {'type': 'string'},
'fulltext': {
'type': 'string', 'term_vector': 'with_positions_offsets_payloads'
}
}
}
}
}
}, cb)
}
var dump = function (hosts, directory) {
var defaultEDOptions = {
limit: 100,
offset: 0,
debug: false,
type: 'data',
delete: false,
maxSockets: null,
input: 'http://' + hosts[0],
'input-index': '_all',
output: directory + '/' + 'dump-' + new Date().toISOString() + '.json',
'output-index': null,
inputTransport: null,
outputTransport: null,
searchBody: null,
sourceOnly: false,
jsonLines: false,
format: '',
'ignore-errors': false,
scrollTime: '10m',
timeout: null,
toLog: null,
awsAccessKeyId: null,
awsSecretAccessKey: null
}
var date = new Date()
var outfile = directory + '/' + 'dump-' + date.toISOString() + '.json'
var ed = new Elasticdump.Elasticdump('http://' + hosts[0], outfile, defaultEDOptions)
ed.on('log', function (message) { console.log('log' + message) })
ed.on('debug', function (message) { console.log('debug' + message) })
ed.on('error', function (error) { console.log('error' + 'Error Emitted => ' + (error.message || JSON.stringify(error))) })
ed.dump()
}
module.exports.loadEuPMCFullTexts = loadEuPMCFullTexts
module.exports.ESClient = ESClient
module.exports.dump = dump
module.exports.deleteAndMapFactIndex = deleteAndMapFactIndex
module.exports.deleteAndMapMetadataIndex = deleteAndMapMetadataIndex
module.exports.deleteAndMapUnstructuredPaperIndex = deleteAndMapUnstructuredPaperIndex
module.exports.indexCRMetadata = indexCRMetadata
module.exports.indexEuPMCMetadata = indexEuPMCMetadata
module.exports.loadCRHTMLFullTexts = loadCRHTMLFullTexts
module.exports.loadCRXHTMLFullTexts = loadCRXHTMLFullTexts
module.exports.loadCRPDFFullTexts = loadCRPDFFullTexts
module.exports.loadCRFullTexts = loadCRFullTexts