mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-17 03:01:08 +01:00
Merge remote-tracking branch 'origin/master' into mongodb-3.0-driver-2.0
This commit is contained in:
@@ -112,8 +112,14 @@
|
||||
createIndex('objects', {_key: 1, value: -1}, {background:true});
|
||||
createIndex('objects', {expireAt: 1}, {expireAfterSeconds:0, background:true});
|
||||
|
||||
createIndex('search', {content:'text'}, {background:true});
|
||||
createIndex('search', {key: 1, id: 1}, {background:true});
|
||||
|
||||
createIndex('searchtopic', {content: 'text', uid: 1, cid: 1}, {background:true});
|
||||
createIndex('searchtopic', {id: 1}, {background:true});
|
||||
|
||||
|
||||
createIndex('searchpost', {content: 'text', uid: 1, cid: 1}, {background:true});
|
||||
createIndex('searchpost', {id: 1}, {background:true});
|
||||
|
||||
|
||||
if (typeof callback === 'function') {
|
||||
callback();
|
||||
|
||||
@@ -5,15 +5,18 @@ var winston = require('winston');
|
||||
module.exports = function(db, module) {
|
||||
var helpers = module.helpers.mongo;
|
||||
|
||||
module.searchIndex = function(key, content, id, callback) {
|
||||
module.searchIndex = function(key, data, id, callback) {
|
||||
callback = callback || function() {};
|
||||
var data = {
|
||||
id: id,
|
||||
key: key,
|
||||
content: content
|
||||
var setData = {
|
||||
id: id
|
||||
};
|
||||
for(var field in data) {
|
||||
if (data.hasOwnProperty(field) && data[field]) {
|
||||
setData[field] = data[field].toString();
|
||||
}
|
||||
}
|
||||
|
||||
db.collection('search').update({key:key, id:id}, {$set:data}, {upsert:true, w: 1}, function(err) {
|
||||
db.collection('search' + key).update({id: id}, {$set: setData}, {upsert:true, w: 1}, function(err) {
|
||||
if(err) {
|
||||
winston.error('Error indexing ' + err.message);
|
||||
}
|
||||
@@ -21,13 +24,35 @@ module.exports = function(db, module) {
|
||||
});
|
||||
};
|
||||
|
||||
module.search = function(key, term, limit, callback) {
|
||||
db.collection('search').find({ $text: { $search: term }, key: key}, {limit: limit}).toArray(function(err, results) {
|
||||
if(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) {
|
||||
if (data.cid.length > 1) {
|
||||
searchQuery.cid = {$in: data.cid.map(String)};
|
||||
} else {
|
||||
searchQuery.cid = data.cid[0].toString();
|
||||
}
|
||||
}
|
||||
|
||||
if (Array.isArray(data.uid) && data.uid.length) {
|
||||
if (data.uid.length > 1) {
|
||||
searchQuery.uid = {$in: data.uid.map(String)};
|
||||
} else {
|
||||
searchQuery.uid = data.uid[0].toString();
|
||||
}
|
||||
}
|
||||
|
||||
db.collection('search' + key).find(searchQuery, {limit: limit}).toArray(function(err, results) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
if(!results || !results.length) {
|
||||
if (!results || !results.length) {
|
||||
return callback(null, []);
|
||||
}
|
||||
|
||||
@@ -41,7 +66,12 @@ module.exports = function(db, module) {
|
||||
|
||||
module.searchRemove = function(key, id, callback) {
|
||||
callback = callback || helpers.noop;
|
||||
db.collection('search').remove({key:key, id:id}, callback);
|
||||
if (!id) {
|
||||
return callback();
|
||||
}
|
||||
db.collection('search' + key).remove({id: id}, function(err, res) {
|
||||
callback(err);
|
||||
});
|
||||
};
|
||||
|
||||
module.flushdb = function(callback) {
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
utils = require('./../../public/src/utils.js'),
|
||||
redis,
|
||||
connectRedis,
|
||||
reds,
|
||||
redisSearch,
|
||||
redisClient,
|
||||
postSearch,
|
||||
topicSearch;
|
||||
@@ -41,7 +41,7 @@
|
||||
try {
|
||||
redis = require('redis');
|
||||
connectRedis = require('connect-redis')(session);
|
||||
reds = require('reds');
|
||||
redisSearch = require('redisearch');
|
||||
} catch (err) {
|
||||
winston.error('Unable to initialize Redis! Is Redis installed? Error :' + err.message);
|
||||
process.exit();
|
||||
@@ -56,12 +56,8 @@
|
||||
ttl: 60 * 60 * 24 * 14
|
||||
});
|
||||
|
||||
reds.createClient = function () {
|
||||
return reds.client || (reds.client = redisClient);
|
||||
};
|
||||
|
||||
module.postSearch = reds.createSearch('nodebbpostsearch');
|
||||
module.topicSearch = reds.createSearch('nodebbtopicsearch');
|
||||
module.postSearch = redisSearch.createSearch('nodebbpostsearch', redisClient);
|
||||
module.topicSearch = redisSearch.createSearch('nodebbtopicsearch', redisClient);
|
||||
|
||||
require('./redis/main')(redisClient, module);
|
||||
require('./redis/hash')(redisClient, module);
|
||||
|
||||
@@ -1,36 +1,30 @@
|
||||
"use strict";
|
||||
|
||||
module.exports = function(redisClient, module) {
|
||||
module.searchIndex = function(key, content, id, callback) {
|
||||
if (key === 'post') {
|
||||
module.postSearch.index(content, id, callback);
|
||||
} else if(key === 'topic') {
|
||||
module.topicSearch.index(content, id, callback);
|
||||
}
|
||||
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, term, limit, callback) {
|
||||
function search(searchObj, callback) {
|
||||
searchObj
|
||||
.query(term)
|
||||
.between(0, limit - 1)
|
||||
.type('or')
|
||||
.end(callback);
|
||||
}
|
||||
module.search = function(key, data, limit, callback) {
|
||||
var method = key === 'post' ? module.postSearch : module.topicSearch;
|
||||
|
||||
if(key === 'post') {
|
||||
search(module.postSearch, callback);
|
||||
} else if(key === 'topic') {
|
||||
search(module.topicSearch, callback);
|
||||
}
|
||||
method.query(data, 0, limit - 1, callback);
|
||||
};
|
||||
|
||||
module.searchRemove = function(key, id, callback) {
|
||||
if(key === 'post') {
|
||||
module.postSearch.remove(id, callback);
|
||||
} else if(key === 'topic') {
|
||||
module.topicSearch.remove(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) {
|
||||
|
||||
Reference in New Issue
Block a user