From dcf47f558e203a52db8a8b7c4e8979a2b793a0a2 Mon Sep 17 00:00:00 2001 From: Baris Soner Usakli Date: Tue, 14 Jan 2014 18:04:54 -0500 Subject: [PATCH] limit search in db --- src/database/mongo.js | 5 ++--- src/database/redis.js | 6 ++++-- src/routes/api.js | 15 ++++----------- src/socket.io/index.js | 1 + src/user.js | 3 ++- 5 files changed, 13 insertions(+), 17 deletions(-) diff --git a/src/database/mongo.js b/src/database/mongo.js index 83e6eb8dec..9890604cf2 100644 --- a/src/database/mongo.js +++ b/src/database/mongo.js @@ -124,9 +124,8 @@ }); } - module.search = function(key, term, callback) { - - db.command({text:"search" , search: term, filter: {key:key} }, function(err, result) { + 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); } diff --git a/src/database/redis.js b/src/database/redis.js index 7490f6c87c..8a5578e439 100644 --- a/src/database/redis.js +++ b/src/database/redis.js @@ -97,10 +97,12 @@ } } - module.search = function(key, term, callback) { + module.search = function(key, term, limit, callback) { function search(searchObj, callback) { searchObj - .query(term).type('or') + .query(term) + .between(0, limit - 1) + .type('or') .end(callback); } diff --git a/src/routes/api.js b/src/routes/api.js index 2bfd66a8de..d0da395d54 100644 --- a/src/routes/api.js +++ b/src/routes/api.js @@ -251,17 +251,14 @@ var path = require('path'), }); app.get('/search/:term', function (req, res, next) { + var limit = 50; function searchPosts(callback) { - db.search('post', req.params.term, function(err, pids) { + db.search('post', req.params.term, limit, function(err, pids) { if (err) { return callback(err, null); } - if(pids.length > 50) { - pids = pids.splice(0, 50); - } - posts.getPostSummaryByPids(pids, false, function (err, posts) { if (err){ return callback(err, null); @@ -272,15 +269,11 @@ var path = require('path'), } function searchTopics(callback) { - db.search('topic', req.params.term, function(err, tids) { + db.search('topic', req.params.term, limit, function(err, tids) { if (err) { return callback(err, null); } - if(tids.length > 50) { - tids = tids.splice(0, 50); - } - topics.getTopicsByTids(tids, 0, function (topics) { callback(null, topics); }, 0); @@ -290,7 +283,7 @@ var path = require('path'), if ((req.user && req.user.uid) || meta.config.allowGuestSearching === '1') { async.parallel([searchPosts, searchTopics], function (err, results) { if (err) { - return next(); + return next(err); } return res.json({ diff --git a/src/socket.io/index.js b/src/socket.io/index.js index e17c3acd34..c4e8085232 100644 --- a/src/socket.io/index.js +++ b/src/socket.io/index.js @@ -28,6 +28,7 @@ var users = {}, io; Sockets.init = function() { + io = socketioWildcard(SocketIO).listen(global.server, { log: false, transports: ['websocket', 'xhr-polling', 'jsonp-polling', 'flashsocket'], diff --git a/src/user.js b/src/user.js index 96e8244eb0..91c901c5a6 100644 --- a/src/user.js +++ b/src/user.js @@ -493,11 +493,12 @@ var bcrypt = require('bcrypt'), return callback([]); } - db.search('user', username, function(err, uids) { + db.search('user', username, 50, function(err, uids) { if (err) { console.log(err); return; } + if (uids && uids.length) { User.getDataForUsers(uids, function(userdata) { callback(userdata);