From 3a7fcc2d3d0cbb12593fcd21473d4bb45f095b69 Mon Sep 17 00:00:00 2001 From: Baris Soner Usakli Date: Thu, 5 Dec 2013 21:07:35 -0500 Subject: [PATCH] search in mongo --- src/database/mongo.js | 49 ++++++++++++++++++++++++++++++++----------- src/database/redis.js | 2 +- src/install.js | 1 - src/routes/debug.js | 5 +++-- 4 files changed, 41 insertions(+), 16 deletions(-) diff --git a/src/database/mongo.js b/src/database/mongo.js index 9005c86095..0c538cd103 100644 --- a/src/database/mongo.js +++ b/src/database/mongo.js @@ -25,6 +25,13 @@ }); + // TODO : what is the db user name?? + /*if(nconf.get('mongo:password')) { + db.authenticate(dbUser, nconf.get('mongo:password'), function (err) { + }); + }*/ + + db.createCollection('objects', function(err, collection) { if(err) { winston.error("Error creating collection " + err.message); @@ -56,11 +63,7 @@ callback(err); }); - // look up how its done in mongo - /*if (nconf.get('mongo:password')) { - redisClient.auth(nconf.get('mongo:password')); - } - */ + } @@ -69,22 +72,44 @@ // module.searchIndex = function(key, content, id) { - // REQUIRES MONGO 2.4 - db.collection('search').update({key:key, content:content, _id:id}, {upsert:true, w: 1}, function(err, result) { + 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, callback) { - // REQUIRES MONGO 2.4 - db.command({text:"search" , search: term }, function(err, result) { - callback(err, result) + + db.command({text:"search" , search: term, filter: {key:key} }, 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) { - // REQUIRES MONGO 2.4 - db.collection('search').remove({_id:id}, function(err, result) { + db.collection('search').remove({id:id, key:key}, function(err, result) { callback(err, result); }); } diff --git a/src/database/redis.js b/src/database/redis.js index 7361be3458..b3a27ef895 100644 --- a/src/database/redis.js +++ b/src/database/redis.js @@ -86,7 +86,7 @@ module.search = function(key, term, callback) { function search(searchObj, callback) { searchObj - .query(query = term).type('or') + .query(term).type('or') .end(callback); } diff --git a/src/install.js b/src/install.js index 22d00cfdd3..a695fe3b4d 100644 --- a/src/install.js +++ b/src/install.js @@ -219,7 +219,6 @@ var async = require('async'), async.each(defaults, function (configObj, next) { meta.configs.setOnEmpty(configObj.field, configObj.value, next); }, function (err) { - console.log('calling meta.configs.init'); meta.configs.init(next); }); }, diff --git a/src/routes/debug.js b/src/routes/debug.js index 59838d83b1..6b413ee007 100644 --- a/src/routes/debug.js +++ b/src/routes/debug.js @@ -407,10 +407,11 @@ var DebugRoute = function(app) { var miscTests = [ function(next) { - //db.searchIndex('post', 'here is content tomato testing purple orange', 1); + /*db.searchIndex('post', 'green tomato is healthy', 2); + next();*/ db.search('post', 'tomato', function(err, result) { next(err, result); - }) + }); } ];