nodebb-plugin-solr
Advanced tools
Comparing version 0.1.0 to 0.1.1
137
library.js
@@ -10,5 +10,15 @@ "use strict"; | ||
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'), | ||
// This method is necessary until solr-client 0.3.x is released | ||
escapeSpecialChars = function(s) { | ||
return s.replace(/([\+\-&\|!\(\)\{\}\[\]\^"~\*\?:\\\ ])/g, function(match) { | ||
return '\\' + match; | ||
}); | ||
}, | ||
Solr = { | ||
@@ -27,2 +37,3 @@ config: {}, // default is localhost:8983, '' core, '/solr' path | ||
ping: res.locals.ping, | ||
enabled: res.locals.enabled, | ||
stats: res.locals.stats, | ||
@@ -33,7 +44,8 @@ csrf: token | ||
app.get('/admin/plugins/solr', middleware.admin.buildHeader, pluginMiddleware.ping, pluginMiddleware.getStats, render); | ||
app.get('/api/admin/plugins/solr', pluginMiddleware.ping, pluginMiddleware.getStats, render); | ||
app.get('/admin/plugins/solr', middleware.admin.buildHeader, pluginMiddleware.ping, pluginMiddleware.getEnabled, pluginMiddleware.getStats, render); | ||
app.get('/api/admin/plugins/solr', pluginMiddleware.ping, pluginMiddleware.getEnabled, pluginMiddleware.getStats, render); | ||
// Utility | ||
app.post('/admin/plugins/solr/rebuild', middleware.admin.isAdmin, Solr.rebuildIndex); | ||
app.post('/admin/plugins/solr/toggle', Solr.toggle); | ||
app.delete('/admin/plugins/solr/flush', middleware.admin.isAdmin, Solr.flush); | ||
@@ -46,2 +58,10 @@ | ||
Solr.ping = function(callback) { | ||
if (Solr.client) { | ||
Solr.client.ping(callback); | ||
} else { | ||
callback(new Error('not-connected')); | ||
} | ||
}; | ||
Solr.checkConflict = function() { | ||
@@ -55,2 +75,13 @@ if (module.parent.exports.libraries['nodebb-plugin-dbsearch']) { | ||
Solr.getNotices = function(notices, callback) { | ||
Solr.ping(function(err, obj) { | ||
var solrNotices = [ | ||
{ done: !err ? true : false, doneText: 'Solr connection OK', notDoneText: 'Could not connect to Solr server' }, | ||
{ done: parseInt(Solr.config.enabled, 10) ? true : false, doneText: 'Solr Indexing Enabled', notDoneText: 'Solr Indexing Disabled' } | ||
]; | ||
callback(null, notices.concat(solrNotices)); | ||
}) | ||
}; | ||
Solr.getSettings = function(callback) { | ||
@@ -110,2 +141,6 @@ db.getObject('settings:solr', function(err, config) { | ||
Solr.client.autoCommit = true; | ||
if (Solr.config.username && Solr.config.password) { | ||
Solr.client.basicAuth(Solr.config.username, Solr.config.password); | ||
} | ||
}; | ||
@@ -135,18 +170,60 @@ | ||
var query = Solr.client.createQuery().q(data.query).dismax().qf({ | ||
title_t: 1.5, | ||
description_t: 1 | ||
}).start(0).rows(20); | ||
if (cache.has(data.query)) { | ||
callback(null, cache.get(data.query)); | ||
} else { | ||
var query = Solr.client.createQuery().q(data.query).dismax().qf({ | ||
title_t: 1.5, | ||
description_t: 1 | ||
}).start(0).rows(20); | ||
Solr.client.search(query, function(err, obj) { | ||
if (obj.response.docs.length > 0) { | ||
callback(null, obj.response.docs.map(function(result) { | ||
return result.id; | ||
})); | ||
} else { | ||
callback(null, []); | ||
} | ||
Solr.client.search(query, function(err, obj) { | ||
if (obj.response.docs.length > 0) { | ||
var payload = obj.response.docs.map(function(result) { | ||
return result.id; | ||
}); | ||
callback(null, payload); | ||
cache.set(data.query, payload); | ||
} else { | ||
callback(null, []); | ||
cache.set(data.query, []); | ||
} | ||
}); | ||
} | ||
}; | ||
Solr.searchTopic = function(tid, term, callback) { | ||
async.parallel({ | ||
mainPid: async.apply(topics.getTopicField, tid, 'mainPid'), | ||
pids: async.apply(topics.getPids, tid) | ||
}, function(err, data) { | ||
data.pids.unshift(data.mainPid); | ||
var query = Solr.client.createQuery().q({ | ||
description_t: escapeSpecialChars(term), | ||
id: '(' + data.pids.join(' OR ') + ')' | ||
}); | ||
Solr.client.search(query, function(err, obj) { | ||
if (obj.response.docs.length > 0) { | ||
callback(null, obj.response.docs.map(function(result) { | ||
return result.id; | ||
})); | ||
} else { | ||
callback(null, []); | ||
} | ||
}); | ||
}); | ||
}; | ||
Solr.toggle = function(req, res) { | ||
if (req.body.state) { | ||
db.setObjectField('settings:solr', 'enabled', parseInt(req.body.state, 10) ? '1' : '0', function(err) { | ||
Solr.config.enabled = req.body.state; | ||
res.send(!err ? 200 : 500); | ||
}); | ||
} else { | ||
res.send(400, "'state' required"); | ||
} | ||
}; | ||
Solr.add = function(payload, callback) { | ||
@@ -183,2 +260,6 @@ Solr.client.add(payload, function(err, obj) { | ||
Solr.post.save = function(postData) { | ||
if (!parseInt(Solr.config.enabled, 10)) { | ||
return; | ||
} | ||
Solr.add({ | ||
@@ -191,5 +272,13 @@ id: postData.pid, | ||
Solr.post.delete = function(pid, callback) { | ||
if (!parseInt(Solr.config.enabled, 10)) { | ||
return; | ||
} | ||
Solr.remove(pid); | ||
if (typeof callback === 'function') { | ||
if (!parseInt(Solr.config.enabled, 10)) { | ||
return; | ||
} | ||
callback(); | ||
@@ -200,2 +289,6 @@ } | ||
Solr.post.restore = function(postData) { | ||
if (!parseInt(Solr.config.enabled, 10)) { | ||
return; | ||
} | ||
Solr.add({ | ||
@@ -211,2 +304,6 @@ id: postData.pid, | ||
Solr.topic.post = function(topicObj) { | ||
if (!parseInt(Solr.config.enabled, 10)) { | ||
return; | ||
} | ||
Solr.indexTopic(topicObj.tid); | ||
@@ -216,2 +313,6 @@ }; | ||
Solr.topic.delete = function(tid) { | ||
if (!parseInt(Solr.config.enabled, 10)) { | ||
return; | ||
} | ||
Solr.deindexTopic(tid); | ||
@@ -221,2 +322,6 @@ }; | ||
Solr.topic.restore = function(tid) { | ||
if (!parseInt(Solr.config.enabled, 10)) { | ||
return; | ||
} | ||
Solr.indexTopic(tid); | ||
@@ -226,2 +331,6 @@ }; | ||
Solr.topic.edit = function(tid) { | ||
if (!parseInt(Solr.config.enabled, 10)) { | ||
return; | ||
} | ||
topics.getTopicFields(tid, ['mainPid', 'title'], function(err, topicData) { | ||
@@ -228,0 +337,0 @@ Solr.indexPost(topicData.mainPid, function(err, payload) { |
@@ -7,3 +7,3 @@ var Solr = module.parent.exports, | ||
Middleware.ping = function(req, res, next) { | ||
Solr.client.ping(function(err, response) { | ||
Solr.ping(function(err, response) { | ||
res.locals.ping = !err ? response : undefined; | ||
@@ -14,2 +14,7 @@ next(); | ||
Middleware.getEnabled = function(req, res, next) { | ||
res.locals.enabled = parseInt(Solr.config.enabled, 10) ? true : false; | ||
next(); | ||
}; | ||
Middleware.getStats = function(req, res, next) { | ||
@@ -16,0 +21,0 @@ async.parallel({ |
{ | ||
"name": "nodebb-plugin-solr", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "Full-text searching for NodeBB using Apache Solr", | ||
@@ -26,4 +26,5 @@ "main": "library.js", | ||
"dependencies": { | ||
"solr-client": "^0.2.9" | ||
"solr-client": "^0.2.9", | ||
"lru-cache": "^2.5.0" | ||
} | ||
} |
@@ -11,2 +11,3 @@ { | ||
{ "hook": "action:settings.set", "method": "onConfigChange" }, | ||
{ "hook": "filter:admin.notices", "method": "getNotices"}, | ||
@@ -13,0 +14,0 @@ { "hook": "action:post.save", "method": "post.save"}, |
Sorry, the diff of this file is not supported yet
21751
404
2
+ Addedlru-cache@^2.5.0
+ Addedlru-cache@2.7.3(transitive)