moved search to dbsearch

v1.18.x
barisusakli 9 years ago
parent 538c6ab7e7
commit dcbf53bae3

@ -56,7 +56,6 @@
"passport": "^0.3.0", "passport": "^0.3.0",
"passport-local": "1.0.0", "passport-local": "1.0.0",
"prompt": "^0.2.14", "prompt": "^0.2.14",
"redisearch": "^0.0.6",
"request": "^2.44.0", "request": "^2.44.0",
"rimraf": "~2.4.2", "rimraf": "~2.4.2",
"rss": "^1.0.0", "rss": "^1.0.0",

@ -124,7 +124,7 @@
require('./mongo/sorted')(db, module); require('./mongo/sorted')(db, module);
require('./mongo/list')(db, module); require('./mongo/list')(db, module);
if(nconf.get('mongo:password') && nconf.get('mongo:username')) { if (nconf.get('mongo:password') && nconf.get('mongo:username')) {
db.authenticate(nconf.get('mongo:username'), nconf.get('mongo:password'), function (err) { db.authenticate(nconf.get('mongo:username'), nconf.get('mongo:password'), function (err) {
if (err) { if (err) {
winston.error(err.stack); winston.error(err.stack);
@ -138,31 +138,22 @@
} }
function createIndices() { function createIndices() {
winston.info('[database] Checking database indices.') winston.info('[database] Checking database indices.');
async.parallel([ async.parallel([
async.apply(createIndex, 'objects', {_key: 1, score: -1}, {background: true}), async.apply(createIndex, 'objects', {_key: 1, score: -1}, {background: true}),
async.apply(createIndex, 'objects', {_key: 1, value: -1}, {background: true, unique: true, sparse: true}), async.apply(createIndex, 'objects', {_key: 1, value: -1}, {background: true, unique: true, sparse: true}),
async.apply(createIndex, 'objects', {expireAt: 1}, {expireAfterSeconds: 0, background: true})
async.apply(createIndex, 'objects', {expireAt: 1}, {expireAfterSeconds: 0, background: true}),
async.apply(createIndex, 'searchtopic', {content: 'text', uid: 1, cid: 1}, {background: true}),
async.apply(createIndex, 'searchtopic', {id: 1}, {background: true}),
async.apply(createIndex, 'searchpost', {content: 'text', uid: 1, cid: 1}, {background: true}),
async.apply(createIndex, 'searchpost', {id: 1}, {background: true})
], function(err) { ], function(err) {
callback(err);
});
}
function createIndex(collection, index, options, callback) {
db.collection(collection).ensureIndex(index, options, function(err) {
if (err) { if (err) {
winston.error('Error creating index ' + err.message); winston.error('Error creating index ' + err.message);
} }
callback(err); callback(err);
}); });
} }
function createIndex(collection, index, options, callback) {
db.collection(collection).ensureIndex(index, options, callback);
}
}); });
}; };

@ -5,83 +5,6 @@ var winston = require('winston');
module.exports = function(db, module) { module.exports = function(db, module) {
var helpers = module.helpers.mongo; var helpers = module.helpers.mongo;
module.searchIndex = function(key, data, id, callback) {
callback = callback || function() {};
id = parseInt(id, 10);
if (!id) {
return callback();
}
var setData = {
id: id
};
for(var field in data) {
if (data.hasOwnProperty(field) && data[field]) {
setData[field] = data[field].toString();
}
}
db.collection('search' + key).update({id: id}, {$set: setData}, {upsert:true, w: 1}, function(err) {
if (err) {
winston.error('Error indexing ' + err.message);
}
callback(err);
});
};
module.search = function(key, data, limit, callback) {
var searchQuery = {};
if (data.content) {
searchQuery.$text = {$search: data.content};
}
if (Array.isArray(data.cid) && data.cid.length) {
data.cid = data.cid.filter(Boolean);
if (data.cid.length > 1) {
searchQuery.cid = {$in: data.cid.map(String)};
} else if (data.cid[0]) {
searchQuery.cid = data.cid[0].toString();
}
}
if (Array.isArray(data.uid) && data.uid.length) {
data.uid = data.uid.filter(Boolean);
if (data.uid.length > 1) {
searchQuery.uid = {$in: data.uid.map(String)};
} else if (data.uid[0]) {
searchQuery.uid = data.uid[0].toString();
}
}
db.collection('search' + key).find(searchQuery, {limit: limit, fields:{_id: 0, id: 1}}).toArray(function(err, results) {
if (err) {
return callback(err);
}
if (!results || !results.length) {
return callback(null, []);
}
var data = results.map(function(item) {
return item.id;
});
callback(null, data);
});
};
module.searchRemove = function(key, id, callback) {
callback = callback || helpers.noop;
id = parseInt(id, 10);
if (!id) {
return callback();
}
db.collection('search' + key).remove({id: id}, function(err, res) {
callback(err);
});
};
module.flushdb = function(callback) { module.flushdb = function(callback) {
callback = callback || helpers.noop; callback = callback || helpers.noop;
db.dropDatabase(callback); db.dropDatabase(callback);

@ -4,16 +4,11 @@
var winston = require('winston'), var winston = require('winston'),
nconf = require('nconf'), nconf = require('nconf'),
path = require('path'),
semver = require('semver'), semver = require('semver'),
session = require('express-session'), session = require('express-session'),
utils = require('./../../public/src/utils.js'),
redis, redis,
connectRedis, connectRedis,
redisSearch, redisClient;
redisClient,
postSearch,
topicSearch;
module.questions = [ module.questions = [
{ {
@ -43,7 +38,6 @@
try { try {
redis = require('redis'); redis = require('redis');
connectRedis = require('connect-redis')(session); connectRedis = require('connect-redis')(session);
redisSearch = require('redisearch');
} catch (err) { } catch (err) {
winston.error('Unable to initialize Redis! Is Redis installed? Error :' + err.message); winston.error('Unable to initialize Redis! Is Redis installed? Error :' + err.message);
process.exit(); process.exit();
@ -58,9 +52,6 @@
ttl: 60 * 60 * 24 * 14 ttl: 60 * 60 * 24 * 14
}); });
module.postSearch = redisSearch.createSearch('nodebbpostsearch', redisClient);
module.topicSearch = redisSearch.createSearch('nodebbtopicsearch', redisClient);
require('./redis/main')(redisClient, module); require('./redis/main')(redisClient, module);
require('./redis/hash')(redisClient, module); require('./redis/hash')(redisClient, module);
require('./redis/sets')(redisClient, module); require('./redis/sets')(redisClient, module);

@ -1,31 +1,6 @@
"use strict"; "use strict";
module.exports = function(redisClient, module) { module.exports = function(redisClient, module) {
module.searchIndex = function(key, data, id, callback) {
var method = key === 'post' ? module.postSearch : module.topicSearch;
method.index(data, id, function(err, res) {
callback(err);
});
};
module.search = function(key, data, limit, callback) {
var method = key === 'post' ? module.postSearch : module.topicSearch;
method.query(data, 0, limit - 1, callback);
};
module.searchRemove = function(key, id, callback) {
callback = callback || function() {};
if (!id) {
return callback();
}
var method = key === 'post' ? module.postSearch : module.topicSearch;
method.remove(id, function(err, res) {
callback(err);
});
};
module.flushdb = function(callback) { module.flushdb = function(callback) {
redisClient.send_command('flushdb', [], function(err) { redisClient.send_command('flushdb', [], function(err) {
@ -35,8 +10,6 @@ module.exports = function(redisClient, module) {
}); });
}; };
module.exists = function(key, callback) { module.exists = function(key, callback) {
redisClient.exists(key, function(err, exists) { redisClient.exists(key, function(err, exists) {
callback(err, exists === 1); callback(err, exists === 1);

Loading…
Cancel
Save