Skip to content

Node.js based forum software built for the modern web

Revert "removing exported search methods from redis and mongo"

Julian Lama2942e22cc1d
diff --git a/src/database/mongo.js b/src/database/mongo.js
index 73d7833fb9..e3a5d3529c 100644
--- a/src/database/mongo.js
+++ b/src/database/mongo.js
@@ -112,6 +112,53 @@
// Exported functions
//
+ module.searchIndex = function(key, content, id) {
+
+ var data = {
+ id:id,
+ key:key,
+ content:content
+ };
+
+ db.collection('search').update({id:id, key:key}, {$set:data}, {upsert:true, w: 1}, function(err, result) {
+ if(err) {
+ winston.error('Error indexing ' + err.message);
+ }
+ });
+ }
+
+ module.search = function(key, term, limit, callback) {
+ db.command({text:"search" , search: term, filter: {key:key}, limit: limit }, function(err, result) {
+ if(err) {
+ return callback(err);
+ }
+
+ if(!result) {
+ return callback(null, []);
+ }
+
+ if(result.results && result.results.length) {
+ var data = result.results.map(function(item) {
+ return item.obj.id;
+ });
+ callback(null, data);
+ } else {
+ callback(null, []);
+ }
+ });
+ }
+
+ module.searchRemove = function(key, id, callback) {
+ db.collection('search').remove({id:id, key:key}, function(err, result) {
+ if(err) {
+ winston.error('Error removing search ' + err.message);
+ }
+ if (typeof callback === 'function') {
+ callback();
+ }
+ });
+ }
+
module.flushdb = function(callback) {
db.dropDatabase(function(err, result) {
if(err){
diff --git a/src/database/redis.js b/src/database/redis.js
index 525610263f..0c90128939 100644
--- a/src/database/redis.js
+++ b/src/database/redis.js
@@ -9,11 +9,13 @@
utils = require('./../../public/src/utils.js'),
redis,
connectRedis,
+ reds,
redisClient;
try {
redis = require('redis');
connectRedis = require('connect-redis')(express);
+ reds = require('reds');
} catch (err) {
winston.error('Unable to initialize Redis! Is Redis installed? Error :' + err.message);
process.exit();
@@ -47,6 +49,14 @@
ttl: 60 * 60 * 24 * 14
});
+ reds.createClient = function () {
+ return reds.client || (reds.client = redisClient);
+ };
+
+ var userSearch = reds.createSearch('nodebbusersearch'),
+ postSearch = reds.createSearch('nodebbpostsearch'),
+ topicSearch = reds.createSearch('nodebbtopicsearch');
+
var db = parseInt(nconf.get('redis:database'), 10);
if (db){
@@ -65,6 +75,47 @@
//
// Exported functions
//
+ module.searchIndex = function(key, content, id) {
+ if(key === 'post') {
+ postSearch.index(content, id);
+ } else if(key === 'topic') {
+ topicSearch.index(content, id);
+ } else if(key === 'user') {
+ userSearch.index(content, id);
+ }
+ }
+
+ module.search = function(key, term, limit, callback) {
+ function search(searchObj, callback) {
+ searchObj
+ .query(term)
+ .between(0, limit - 1)
+ .type('or')
+ .end(callback);
+ }
+
+ if(key === 'post') {
+ search(postSearch, callback);
+ } else if(key === 'topic') {
+ search(topicSearch, callback);
+ } else if(key === 'user') {
+ search(userSearch, callback);
+ }
+ }
+
+ module.searchRemove = function(key, id, callback) {
+ if(key === 'post') {
+ postSearch.remove(id);
+ } else if(key === 'topic') {
+ topicSearch.remove(id);
+ } else if(key === 'user') {
+ userSearch.remove(id);
+ }
+
+ if (typeof callback === 'function') {
+ callback()
+ }
+ }
module.flushdb = function(callback) {
redisClient.send_command('flushdb', [], function(err) {