diff --git a/src/posts.js b/src/posts.js index 769d263aee..8219a39363 100644 --- a/src/posts.js +++ b/src/posts.js @@ -404,7 +404,7 @@ var RDB = require('./redis.js'), topics: data[0]?data[0]:0, posts: data[1]?data[1]:0 }; - + socket.emit('post.stats', stats); } else @@ -412,4 +412,28 @@ var RDB = require('./redis.js'), }); } + Posts.reIndexPids = function(pids, callback) { + + function reIndex(pid, callback) { + + Posts.getPostField(pid, 'content', function(content) { + search.remove(pid, function() { + + if(content && content.length) { + search.index(content, pid); + } + callback(null); + }); + }); + } + + async.each(pids, reIndex, function(err) { + if(err) { + callback(err, null); + } else { + callback(null, 'Posts reindexed'); + } + }); + } + }(exports)); \ No newline at end of file diff --git a/src/topics.js b/src/topics.js index d1105180e5..bbbf812751 100644 --- a/src/topics.js +++ b/src/topics.js @@ -677,4 +677,37 @@ marked.setOptions({ RDB.lrange('tid:' + tid + ':posts', 0, -1, callback); } + Topics.reIndexTopic = function(tid, callback) { + Topics.getPids(tid, function(err, pids) { + if(err) { + callback(err); + } else { + posts.reIndexPids(pids, function(err) { + if(err) { + callback(err); + } else { + callback(null); + } + }); + } + }); + } + + Topics.reIndexAll = function(callback) { + RDB.smembers('topics:tid', function(err, tids) { + if(err) { + callback(err, null); + } else { + + async.each(tids, Topics.reIndexTopic, function(err) { + if(err) { + callback(err, null); + } else { + callback(null, 'All topics reindexed.'); + } + }); + } + }); + } + }(exports)); \ No newline at end of file diff --git a/src/webserver.js b/src/webserver.js index b27ce2868b..70e4bee425 100644 --- a/src/webserver.js +++ b/src/webserver.js @@ -394,6 +394,16 @@ var express = require('express'), res.send(header + app.create_route("search/"+req.params.term, null, "search") + templates['footer']); }); }); + + app.get('/reindex', function(req, res) { + topics.reIndexAll(function(err) { + if(err) { + res.json(err); + } else { + res.send('All topics reindexed'); + } + }); + }); }); }(WebServer));