mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-02 12:05:57 +01:00
moved search to dbsearch
This commit is contained in:
@@ -56,7 +56,6 @@
|
||||
"passport": "^0.3.0",
|
||||
"passport-local": "1.0.0",
|
||||
"prompt": "^0.2.14",
|
||||
"redisearch": "^0.0.6",
|
||||
"request": "^2.44.0",
|
||||
"rimraf": "~2.4.2",
|
||||
"rss": "^1.0.0",
|
||||
|
||||
@@ -124,7 +124,7 @@
|
||||
require('./mongo/sorted')(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) {
|
||||
if (err) {
|
||||
winston.error(err.stack);
|
||||
@@ -138,31 +138,22 @@
|
||||
}
|
||||
|
||||
function createIndices() {
|
||||
winston.info('[database] Checking database indices.')
|
||||
winston.info('[database] Checking database indices.');
|
||||
async.parallel([
|
||||
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', {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})
|
||||
async.apply(createIndex, 'objects', {expireAt: 1}, {expireAfterSeconds: 0, background: true})
|
||||
], function(err) {
|
||||
callback(err);
|
||||
});
|
||||
}
|
||||
|
||||
function createIndex(collection, index, options, callback) {
|
||||
db.collection(collection).ensureIndex(index, options, function(err) {
|
||||
if (err) {
|
||||
winston.error('Error creating index ' + err.message);
|
||||
}
|
||||
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) {
|
||||
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) {
|
||||
callback = callback || helpers.noop;
|
||||
db.dropDatabase(callback);
|
||||
|
||||
@@ -4,16 +4,11 @@
|
||||
|
||||
var winston = require('winston'),
|
||||
nconf = require('nconf'),
|
||||
path = require('path'),
|
||||
semver = require('semver'),
|
||||
session = require('express-session'),
|
||||
utils = require('./../../public/src/utils.js'),
|
||||
redis,
|
||||
connectRedis,
|
||||
redisSearch,
|
||||
redisClient,
|
||||
postSearch,
|
||||
topicSearch;
|
||||
redisClient;
|
||||
|
||||
module.questions = [
|
||||
{
|
||||
@@ -43,7 +38,6 @@
|
||||
try {
|
||||
redis = require('redis');
|
||||
connectRedis = require('connect-redis')(session);
|
||||
redisSearch = require('redisearch');
|
||||
} catch (err) {
|
||||
winston.error('Unable to initialize Redis! Is Redis installed? Error :' + err.message);
|
||||
process.exit();
|
||||
@@ -58,9 +52,6 @@
|
||||
ttl: 60 * 60 * 24 * 14
|
||||
});
|
||||
|
||||
module.postSearch = redisSearch.createSearch('nodebbpostsearch', redisClient);
|
||||
module.topicSearch = redisSearch.createSearch('nodebbtopicsearch', redisClient);
|
||||
|
||||
require('./redis/main')(redisClient, module);
|
||||
require('./redis/hash')(redisClient, module);
|
||||
require('./redis/sets')(redisClient, module);
|
||||
|
||||
@@ -1,31 +1,6 @@
|
||||
"use strict";
|
||||
|
||||
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) {
|
||||
redisClient.send_command('flushdb', [], function(err) {
|
||||
@@ -35,8 +10,6 @@ module.exports = function(redisClient, module) {
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
module.exists = function(key, callback) {
|
||||
redisClient.exists(key, function(err, exists) {
|
||||
callback(err, exists === 1);
|
||||
|
||||
Reference in New Issue
Block a user