-
Notifications
You must be signed in to change notification settings - Fork 7
/
library.js
777 lines (666 loc) · 18.5 KB
/
library.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
"use strict";
/* globals module, require */
var db = module.parent.require('./database'),
winston = module.parent.require('winston'),
elasticsearch = require('elasticsearch'),
async = module.parent.require('async'),
_ = module.parent.require('underscore'),
//LRU = require('lru-cache'),
//cache = LRU({ max: 20, maxAge: 1000 * 60 * 60 }), // Remember the last 20 searches in the past hour
topics = module.parent.require('./topics'),
posts = module.parent.require('./posts'),
batch = module.parent.require('./batch'),
escapeSpecialChars = function(s) {
return s.replace(/([\+\-&\|!\(\)\{\}\[\]\^"~\*\?:\\\ ])/g, function(match) {
return '\\' + match;
});
},
client = new elasticsearch.Client({
host: 'localhost:9200'
// log: 'trace'
}),
// this config dosen't work for newer version of elasticsearch api
Elasticsearch = {
/*
Defaults configs:
host: localhost:9200
enabled: undefined (false)
*/
config: {
sniffOnStart: true, // Should the client attempt to detect the rest of the cluster when it is first instantiated?
sniffInterval: 60000, // Every n milliseconds, perform a sniff operation and make sure our list of nodes is complete.
sniffOnConnectionFault: true, // Should the client immediately sniff for a more current list of nodes when a connection dies?
host: 'localhost:9200',
index_name: 'nodebb',
post_type: 'posts',
batch_size: 1000
}, // default is localhost:9200
client: undefined
};
_.str = require('underscore.string'); // Import Underscore.string to separate object, because there are conflict functions (include, reverse, contains)
_.mixin(_.str.exports()); // Mix in non-conflict functions to Underscore namespace if you want
Elasticsearch.init = function(data, callback) {
var pluginMiddleware = require('./middleware'),
render = function(req, res, next) {
// Regenerate csrf token
var token = req.csrfToken();
res.render('admin/plugins/elasticsearch', {
ping: res.locals.ping,
enabled: res.locals.enabled,
stats: res.locals.stats,
csrf: token
});
};
data.router.get('/admin/plugins/elasticsearch', data.middleware.applyCSRF, data.middleware.admin.buildHeader, pluginMiddleware.ping, pluginMiddleware.getEnabled, pluginMiddleware.getStats, render);
data.router.get('/api/admin/plugins/elasticsearch', data.middleware.applyCSRF, pluginMiddleware.ping, pluginMiddleware.getEnabled, pluginMiddleware.getStats, render);
// Utility
data.router.post('/admin/plugins/elasticsearch/rebuild', data.middleware.admin.isAdmin, Elasticsearch.rebuildIndex);
data.router.post('/admin/plugins/elasticsearch/toggle', Elasticsearch.toggle);
data.router.delete('/admin/plugins/elasticsearch/flush', data.middleware.admin.isAdmin, Elasticsearch.flush);
Elasticsearch.getSettings(Elasticsearch.connect);
callback();
};
Elasticsearch.ping = function(callback) {
if (client) {
client.ping(callback);
} else {
callback(new Error('not-connected'));
}
};
Elasticsearch.checkConflict = function() {
if (module.parent.exports.libraries['nodebb-plugin-dbsearch'] || module.parent.exports.libraries['nodebb-plugin-solr']) {
return true;
} else {
return false;
}
};
Elasticsearch.getNotices = function(notices, callback) {
Elasticsearch.ping(function(err, obj) {
var elasticsearchNotices = [
{ done: !err ? true : false, doneText: 'Elasticsearch connection OK', notDoneText: 'Could not connect to Elasticsearch server' },
{ done: parseInt(Elasticsearch.config.enabled, 10) || false, doneText: 'Elasticsearch Indexing Enabled', notDoneText: 'Elasticsearch Indexing Disabled' }
];
callback(null, notices.concat(elasticsearchNotices));
})
};
Elasticsearch.getSettings = function(callback) {
db.getObject('settings:elasticsearch', function(err, config) {
Elasticsearch.config = {};
if (!err) {
for(var k in config) {
if (config.hasOwnProperty(k) && config[k].length && !Elasticsearch.config.hasOwnProperty(k)) {
Elasticsearch.config[k] = config[k];
}
}
} else {
winston.error('[plugin:elasticsearch] Could not fetch settings, assuming defaults.');
}
callback();
});
};
Elasticsearch.getRecordCount = function(callback) {
if (!Elasticsearch.client) {
return callback(new Error('not-connected'));
}
Elasticsearch.client.count({
index: Elasticsearch.config.index_name
}, function (error, response) {
if (!error && response) {
callback(null, response.count);
}
else {
callback(error, 0);
}
});
};
Elasticsearch.getTopicCount = function(callback) {
if (!Elasticsearch.client) {
return callback(new Error('not-connected'));
}
Elasticsearch.client.count({
index: Elasticsearch.config.index_name,
type: Elasticsearch.config.post_type,
body: {
query: {
constant_score: {
filter: {
exists: {
field: "title"
}
}
}
}
}
}, function (error, response) {
if (!error && response) {
callback(null, response.count);
}
else {
callback(error, 0);
}
});
};
Elasticsearch.connect = function() {
if (!Elasticsearch.config.host) {
return;
}
if (Elasticsearch.client) {
delete Elasticsearch.client;
}
// Convert host to array
var hosts = Elasticsearch.config.host.split(',');
hosts = _.map(hosts, function(host){ return _.trim(host); });
// Compact array to remove empty elements just in case.
hosts = _.compact(hosts);
if (hosts.length === 0) {
return;
}
Elasticsearch.config.hosts = hosts;
// Now remove the host since we're going to use hosts.
delete Elasticsearch.config.host;
Elasticsearch.client = new elasticsearch.Client(Elasticsearch.config);
};
Elasticsearch.adminMenu = function(custom_header, callback) {
custom_header.plugins.push({
"route": '/plugins/elasticsearch',
"icon": 'fa-search',
"name": 'Elasticsearch'
});
callback(null, custom_header);
};
Elasticsearch.search = function(data, callback) {
if (Elasticsearch.checkConflict()) {
// The dbsearch plugin was detected, abort search!
winston.warn('[plugin/elasticsearch] Another search plugin (dbsearch or solr) is enabled, so search via Elasticsearch was aborted.');
return callback(null, data);
}
var queryMatch = {
content: escapeSpecialChars(data.content)
};
if (data.index === 'topic') {
queryMatch = {
title: escapeSpecialChars(data.content)
};
}
/*
if (cache.has(data.query)) {
return callback(null, cache.get(data.query));
}
*/
if (!Elasticsearch.client) {
return callback(new Error('not-connected'));
}
var query = {
index: Elasticsearch.config.index_name,
body: {
query: {
match: queryMatch
},
from: 0,
size: 20
}
};
// changing the client obj
console.log('search query', query);
client.search(query, function(err, obj) {
if (err) {
callback(err);
} else if (obj && obj.hits && obj.hits.hits && obj.hits.hits.length > 0) {
console.log('search hits', obj.hits.hits);
var payload = obj.hits.hits.map(function(result) {
// return the correct post id
if (data.index === 'topic') {
return parseInt(result._source.tid, 10);
}
return parseInt(result._source.id, 10);
});
callback(null, payload);
//cache.set(data.query, payload);
} else {
callback(null, []);
//cache.set(data.query, []);
}
});
};
Elasticsearch.searchTopic = function(data, callback) {
var tid = data.tid,
term = data.term;
if (!term || !term.length) {
return callback(null, []);
}
async.parallel({
mainPid: async.apply(topics.getTopicField, tid, 'mainPid'),
pids: async.apply(topics.getPids, tid)
}, function(err, data) {
if (!Elasticsearch.client) {
return callback(new Error('not-connected'));
}
if (data.mainPid && data.pids.indexOf(data.mainPid) === -1) {
data.pids.unshift(data.mainPid);
}
// Make sure ids are integers
data.pids = _.map(data.pids, function(p) {
if (_.isString(p)) {
return parseInt(p, 10);
}
return p;
});
var query = {
body: {
query: {
filtered: {
query: {
match: {
content: escapeSpecialChars(term)
}
},
filter: {
ids: {
type: Elasticsearch.config.post_type,
values: data.pids
}
}
}
},
from: 0,
size: 20
}
};
Elasticsearch.client.search(query, function(err, obj) {
if (err) {
callback(err);
} else if (obj && obj.hits && obj.hits.hits && obj.hits.hits.length > 0) {
callback(null, obj.hits.hits.map(function(result) {
return result._id;
}));
} else {
callback(null, []);
}
});
});
};
Elasticsearch.toggle = function(req, res) {
if (req.body.state) {
db.setObjectField('settings:elasticsearch', 'enabled', parseInt(req.body.state, 10) ? '1' : '0', function(err) {
Elasticsearch.config.enabled = req.body.state;
res.send(!err ? 200 : 500);
});
} else {
res.send(400, "'state' required");
}
};
Elasticsearch.add = function(payload, callback) {
if (!Elasticsearch.client) {
if (callback) {
return callback(new Error('not-connected'));
}
return;
}
if (!payload) {
if (callback) {
return callback(null);
}
return;
}
if (_.isArray(payload)) {
if (0 === payload.length) {
if (callback) {
return callback(null);
}
return;
}
}
else {
// If not array, then make it a single-element array because bulk method requires array.
payload = [ payload ];
}
// Create bulk document, which looks like this:
/*
[
// action description
{ index: { _index: 'myindex', _type: 'mytype', _id: 1 } },
// the document to index
{ title: 'foo' },
// action description
{ update: { _index: 'myindex', _type: 'mytype', _id: 2 } },
// the document to update
{ doc: { title: 'foo' } },
// action description
{ delete: { _index: 'myindex', _type: 'mytype', _id: 3 } },
// no document needed for this delete
]
*/
var body = [];
_.each(payload, function(item) {
if (item && item.id) {
// Make sure id is an integer
var itemId = parseInt(item.id, 10);
item.id = itemId;
// Action
body.push({
index: {
/*_index: Elasticsearch.config.index_name, */ // We'll set it in bulk()
/*_type: Elasticsearch.config.post_type, */ // We'll set it in bulk()
_id: itemId
}
});
// Document
body.push(item);
}
});
if (0 === body.length) {
if (callback) {
return callback(null);
}
return;
}
Elasticsearch.client.bulk({
body: body,
type: Elasticsearch.config.post_type,
index: Elasticsearch.config.index_name
}, function(err, obj) {
if (err) {
if (payload.length === 1) {
winston.error('[plugin/elasticsearch] Could not index post ' + payload[0].id + ', error: ' + err.message);
}
else {
winston.error('[plugin/elasticsearch] Could not index posts, error: ' + err.message);
}
} else if (typeof callback === 'function') {
callback.apply(arguments);
}
});
};
Elasticsearch.remove = function(pid, callback) {
if (!Elasticsearch.client) {
return;
}
// Make sure id is an integer
if (_.isString(pid)) {
pid = parseInt(pid, 10);
}
Elasticsearch.client.delete({
index: Elasticsearch.config.index_name,
type: Elasticsearch.config.post_type,
id: pid
}, function(err, obj) {
if (err) {
winston.error('[plugin/elasticsearch] Could not remove post ' + pid + ' from index');
}
if (callback) {
callback(null, obj);
}
});
};
Elasticsearch.flush = function(req, res) {
if (!Elasticsearch.client) {
return;
}
Elasticsearch.client.deleteByQuery({
index: Elasticsearch.config.index_name,
type: Elasticsearch.config.post_type,
q: '*'
}, function(err, obj){
if (err) {
winston.error('[plugin/elasticsearch] Could not empty the search index');
res.send(500, err.message);
} else {
res.send(200);
}
});
};
Elasticsearch.post = {};
Elasticsearch.post.save = function(postData) {
if (!parseInt(Elasticsearch.config.enabled, 10)) {
return;
}
Elasticsearch.indexPost(postData);
};
Elasticsearch.post.delete = function(pid, callback) {
if (!parseInt(Elasticsearch.config.enabled, 10)) {
return;
}
Elasticsearch.remove(pid);
if (typeof callback === 'function') {
if (!parseInt(Elasticsearch.config.enabled, 10)) {
return;
}
callback();
}
};
Elasticsearch.post.restore = function(postData) {
if (!parseInt(Elasticsearch.config.enabled, 10)) {
return;
}
Elasticsearch.indexPost(postData);
};
Elasticsearch.post.edit = Elasticsearch.post.restore;
Elasticsearch.topic = {};
Elasticsearch.topic.post = function(topicObj) {
if (!parseInt(Elasticsearch.config.enabled, 10)) {
return;
}
Elasticsearch.indexTopic(topicObj);
};
Elasticsearch.topic.delete = function(topicData) {
var tid = (void 0 === topicData.tid) ? topicData : topicData.tid;
if (!parseInt(Elasticsearch.config.enabled, 10)) {
return;
}
Elasticsearch.deindexTopic(tid);
};
Elasticsearch.topic.restore = function(topicObj) {
if (!parseInt(Elasticsearch.config.enabled, 10)) {
return;
}
Elasticsearch.indexTopic(topicObj);
};
Elasticsearch.topic.edit = function(topicObj) {
if (!parseInt(Elasticsearch.config.enabled, 10)) {
return;
}
async.waterfall([
async.apply(posts.getPostFields, topicObj.mainPid, ['pid', 'content']),
Elasticsearch.indexPost,
], function(err, payload) {
if (err) {
return winston.error(err.message);
}
if (!payload) {
return winston.warn('[plugins/elasticsearch] no payload for pid ' + topicObj.mainPid);
}
payload.title = topicObj.title;
Elasticsearch.add(payload);
});
};
/* Topic and Post indexing methods */
Elasticsearch.indexTopic = function(topicObj, callback) {
async.waterfall([
async.apply(topics.getPids, topicObj.tid),
function(pids, next) {
// Add OP to the list of pids to index
if (topicObj.mainPid && pids.indexOf(topicObj.mainPid) === -1) {
pids.unshift(topicObj.mainPid);
}
posts.getPostsFields(pids, ['pid', 'content'], next);
},
function(posts, next) {
async.map(posts, Elasticsearch.indexPost, next);
}
], function(err, payload) {
if (err) {
winston.error('[plugins/elasticsearch] Encountered an error while compiling post data for tid ' + topicObj.tid);
if (typeof callback === 'function') {
return callback(err);
}
}
// Also index the title into the main post of this topic
for(var x=0,numPids=payload.length;x<numPids;x++) {
if (payload[x]) {
if (payload[x].id === topicObj.mainPid) {
payload[x].title = topicObj.title;
// add tid to main post, so search topic could get tid.
payload[x].tid = topicObj.tid;
}
}
}
if (typeof callback === 'function') {
// If callback is defined, then we don't index, but rather return the payload?!
callback(undefined, payload);
} else {
Elasticsearch.add(payload, callback);
}
});
};
Elasticsearch.deindexTopic = function(tid) {
async.parallel({
mainPid: async.apply(topics.getTopicField, tid, 'mainPid'),
pids: async.apply(topics.getPids, tid)
}, function(err, data) {
if (!Elasticsearch.client) {
return;
}
if (data.mainPid && data.pids.indexOf(data.mainPid) === -1) {
data.pids.unshift(data.mainPid);
}
// Make sure ids are integers
data.pids = _.map(data.pids, function(p) {
if (_.isString(p)) {
return parseInt(p, 10);
}
return p;
});
var query = {
index: Elasticsearch.config.index_name,
type: Elasticsearch.config.post_type,
body: {
query: {
ids: {
values: data.pids
}
}
}
};
Elasticsearch.client.deleteByQuery(query, function(err, obj) {
if (err) {
winston.error('[plugin/elasticsearch] Encountered an error while deindexing tid ' + tid + '. Error: ' + err.message);
}
});
});
};
Elasticsearch.indexPost = function(postData, callback) {
if (!postData || !postData.pid) {
if (typeof callback === 'function') {
return callback(new Error('Post data is null or missing pid.'));
} else {
return;
}
}
var payload = {
id: postData.pid
};
// We are allowing posts with null content to be indexed.
if (postData.content) {
payload.content = postData.content;
}
if (typeof callback === 'function') {
callback(undefined, payload);
} else {
Elasticsearch.add(payload);
}
};
Elasticsearch.deindexPost = Elasticsearch.post.delete;
Elasticsearch.rebuildIndex = function(req, res) {
async.waterfall([
function(next) {
Elasticsearch.client.deleteByQuery({
index: Elasticsearch.config.index_name,
type: Elasticsearch.config.post_type,
q: '*'
}, next);
}
],
function(err, results){
// if (err) {
// winston.error('[plugin/elasticsearch] Could not delete and re-create index. Error: ' + err.message);
// res.sendStatus(500);
// return
// }
batch.processSortedSet('topics:tid', function(tids, next) {
topics.getTopicsFields(tids, ['tid', 'mainPid', 'title'], function(err, topics) {
if (err) {
return next(err);
}
async.map(topics, Elasticsearch.indexTopic, function(err, topicPayloads) {
var payload = topicPayloads.reduce(function(currentPayload, topics) {
if (Array.isArray(topics)) {
return currentPayload.concat(topics);
} else {
currentPayload.push(topics);
}
}, []).filter(function(entry) {
if (entry) {
return entry.hasOwnProperty('id');
}
return false;
});
Elasticsearch.add(payload, function(err, obj) {
if (err) {
return next(err);
}
next();
});
});
});
}, {batch: parseInt(Elasticsearch.config.batch_size, 10)}, function(err) {
if (!err) {
res.sendStatus(200);
}
});
});
};
Elasticsearch.createIndex = function(callback) {
if (!Elasticsearch.client) {
return callback(new Error('not-connected'));
}
var indexName = Elasticsearch.config.index_name;
if (indexName && 0 < indexName.length) {
Elasticsearch.client.indices.create({
index : Elasticsearch.config.index_name
}, function(err, results){
if (!err) {
callback(null, results);
}
else if ( /IndexAlreadyExistsException/im.test(err.message) ) { // we can ignore if index is already there
winston.info("[plugin/elasticsearch] Ignoring error creating mapping " + err);
callback(null);
}
else {
callback(err);
}
});
}
};
Elasticsearch.deleteIndex = function(callback) {
if (!Elasticsearch.client) {
return callback(new Error('not-connected'));
}
var indexName = Elasticsearch.config.index_name;
if (indexName && 0 < indexName.length) {
Elasticsearch.client.indices.delete({
index : Elasticsearch.config.index_name
}, function(err, results) {
if (!err) {
callback(null, results);
}
else if ( /IndexMissingException|index_not_found_exception/im.test(err.message) ) { // we can ignore if index is not there
winston.info("[plugin/elasticsearch] Ignoring error deleting mapping " + err);
callback(null);
}
else {
callback(err);
}
});
}
};
module.exports = Elasticsearch;