mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-12-24 01:10:31 +01:00
search cleanup
This commit is contained in:
@@ -8,9 +8,13 @@
|
|||||||
XRegExp = require('xregexp').XRegExp;
|
XRegExp = require('xregexp').XRegExp;
|
||||||
|
|
||||||
process.profile = function(operation, start) {
|
process.profile = function(operation, start) {
|
||||||
|
console.log('%s took %d milliseconds', process.elapsedTimeSince(start));
|
||||||
|
};
|
||||||
|
|
||||||
|
process.elapsedTimeSince = function(start) {
|
||||||
var diff = process.hrtime(start);
|
var diff = process.hrtime(start);
|
||||||
console.log('%s took %d milliseconds', operation, diff[0] * 1e3 + diff[1] / 1e6);
|
return diff[0] * 1e3 + diff[1] / 1e6;
|
||||||
}
|
};
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
XRegExp = window.XRegExp;
|
XRegExp = window.XRegExp;
|
||||||
|
|||||||
@@ -87,11 +87,10 @@ Controllers.home = function(req, res, next) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Controllers.search = function(req, res, next) {
|
Controllers.search = function(req, res, next) {
|
||||||
|
var start = process.hrtime();
|
||||||
|
|
||||||
if (!req.params.term) {
|
if (!req.params.term) {
|
||||||
return res.render('search', {
|
return res.render('search', {
|
||||||
show_no_topics: 'hide',
|
|
||||||
show_no_posts: 'hide',
|
|
||||||
show_results: 'hide',
|
|
||||||
search_query: '',
|
search_query: '',
|
||||||
posts: [],
|
posts: [],
|
||||||
topics: []
|
topics: []
|
||||||
@@ -102,51 +101,49 @@ Controllers.search = function(req, res, next) {
|
|||||||
return res.redirect('/404');
|
return res.redirect('/404');
|
||||||
}
|
}
|
||||||
|
|
||||||
function searchPosts(callback) {
|
function search(index, callback) {
|
||||||
plugins.fireHook('filter:search.query', {
|
plugins.fireHook('filter:search.query', {
|
||||||
index: 'post',
|
index: index,
|
||||||
query: req.params.term
|
query: req.params.term
|
||||||
}, function(err, pids) {
|
}, callback);
|
||||||
if (err) {
|
|
||||||
return callback(err);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
posts.getPostSummaryByPids(pids, false, callback);
|
async.parallel({
|
||||||
});
|
pids: function(next) {
|
||||||
|
search('post', next);
|
||||||
|
},
|
||||||
|
tids: function(next) {
|
||||||
|
search('topic', next);
|
||||||
}
|
}
|
||||||
|
}, function (err, results) {
|
||||||
function searchTopics(callback) {
|
|
||||||
plugins.fireHook('filter:search.query', {
|
|
||||||
index: 'topic',
|
|
||||||
query: req.params.term
|
|
||||||
}, function(err, tids) {
|
|
||||||
if (err) {
|
|
||||||
return callback(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
topics.getTopicsByTids(tids, 0, callback);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
async.parallel([searchPosts, searchTopics], function (err, results) {
|
|
||||||
if (err) {
|
if (err) {
|
||||||
return next(err);
|
return next(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!results) {
|
if(!results) {
|
||||||
results = [];
|
results = {pids:[], tids: []};
|
||||||
results[0] = results[1] = [];
|
}
|
||||||
|
|
||||||
|
async.parallel({
|
||||||
|
posts: function(next) {
|
||||||
|
posts.getPostSummaryByPids(results.pids, false, next);
|
||||||
|
},
|
||||||
|
topics: function(next) {
|
||||||
|
topics.getTopicsByTids(results.tids, 0, next);
|
||||||
|
}
|
||||||
|
}, function(err, results) {
|
||||||
|
if (err) {
|
||||||
|
return next(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
return res.render('search', {
|
return res.render('search', {
|
||||||
show_no_topics: results[1].length ? 'hide' : '',
|
time: process.elapsedTimeSince(start),
|
||||||
show_no_posts: results[0].length ? 'hide' : '',
|
|
||||||
show_results: '',
|
|
||||||
search_query: req.params.term,
|
search_query: req.params.term,
|
||||||
posts: results[0],
|
posts: results.posts,
|
||||||
topics: results[1],
|
topics: results.topics,
|
||||||
post_matches : results[0].length,
|
post_matches : results.posts.length,
|
||||||
topic_matches : results[1].length
|
topic_matches : results.topics.length
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user