Files
NodeBB/src/search.js

406 lines
10 KiB
JavaScript
Raw Normal View History

2014-07-07 17:36:10 -04:00
'use strict';
2016-02-13 09:35:48 +02:00
var async = require('async');
2017-06-25 20:00:05 -04:00
var _ = require('lodash');
2016-02-13 09:35:48 +02:00
var db = require('./database');
var posts = require('./posts');
var topics = require('./topics');
var categories = require('./categories');
var user = require('./user');
var plugins = require('./plugins');
var privileges = require('./privileges');
var utils = require('./utils');
2014-07-07 17:36:10 -04:00
2017-05-26 22:28:12 -04:00
var search = module.exports;
2014-07-07 17:36:10 -04:00
search.search = function (data, callback) {
2014-07-07 17:36:10 -04:00
var start = process.hrtime();
2018-10-18 12:50:24 -04:00
data.searchIn = data.searchIn || 'titlesposts';
2014-07-07 17:36:10 -04:00
2016-02-13 09:35:48 +02:00
async.waterfall([
function (next) {
2018-10-18 12:50:24 -04:00
if (data.searchIn === 'posts' || data.searchIn === 'titles' || data.searchIn === 'titlesposts') {
2016-02-13 09:35:48 +02:00
searchInContent(data, next);
2018-10-18 12:50:24 -04:00
} else if (data.searchIn === 'users') {
2016-02-13 09:35:48 +02:00
user.search(data, next);
2018-10-18 12:50:24 -04:00
} else if (data.searchIn === 'tags') {
2016-02-13 09:35:48 +02:00
topics.searchAndLoadTags(data, next);
} else {
next(new Error('[[error:unknown-search-filter]]'));
}
},
function (result, next) {
result.time = (process.elapsedTimeSince(start) / 1000).toFixed(2);
next(null, result);
2017-02-17 19:31:21 -07:00
},
2016-02-13 09:35:48 +02:00
], callback);
2015-01-07 16:10:11 -05:00
};
2015-03-14 23:00:24 -04:00
function searchInContent(data, callback) {
2015-02-05 14:55:36 -05:00
data.uid = data.uid || 0;
2017-05-26 22:28:12 -04:00
var matchCount = 0;
var pids;
var metadata;
2018-02-02 11:55:55 -05:00
var itemsPerPage = data.itemsPerPage || 10;
2017-05-26 22:28:12 -04:00
async.waterfall([
function (next) {
async.parallel({
searchCids: function (next) {
getSearchCids(data, next);
},
searchUids: function (next) {
getSearchUids(data, next);
},
}, next);
2017-02-17 19:31:21 -07:00
},
2017-05-26 22:28:12 -04:00
function (results, next) {
function doSearch(type, searchIn, next) {
2018-03-27 20:32:57 -04:00
if (searchIn.includes(data.searchIn)) {
plugins.fireHook('filter:search.query', {
index: type,
content: data.query,
matchWords: data.matchWords || 'all',
cid: results.searchCids,
uid: results.searchUids,
2018-06-05 13:43:34 -04:00
searchData: data,
2018-03-27 20:32:57 -04:00
}, next);
2015-03-14 23:00:24 -04:00
} else {
next(null, []);
}
}
2017-05-26 22:28:12 -04:00
async.parallel({
pids: function (next) {
doSearch('post', ['posts', 'titlesposts'], next);
},
tids: function (next) {
doSearch('topic', ['titles', 'titlesposts'], next);
},
}, next);
},
function (results, next) {
pids = results.pids;
2018-10-18 12:50:24 -04:00
if (!results.pids.length && !results.tids.length) {
2017-02-18 12:30:49 -07:00
return callback(null, { posts: [], matchCount: matchCount, pageCount: 1 });
2015-01-07 16:10:11 -05:00
}
2015-03-14 23:00:24 -04:00
2017-05-26 22:28:12 -04:00
topics.getMainPids(results.tids, next);
},
function (mainPids, next) {
2018-10-21 19:33:46 -04:00
pids = mainPids.concat(pids).filter(Boolean);
2015-03-14 23:00:24 -04:00
2017-05-26 22:28:12 -04:00
privileges.posts.filter('read', pids, data.uid, next);
},
function (pids, next) {
filterAndSort(pids, data, next);
},
function (pids, next) {
plugins.fireHook('filter:search.inContent', {
pids: pids,
}, next);
},
function (_metadata, next) {
metadata = _metadata;
matchCount = metadata.pids.length;
2017-05-26 22:28:12 -04:00
if (data.page) {
2018-02-02 11:55:55 -05:00
var start = Math.max(0, (data.page - 1)) * itemsPerPage;
metadata.pids = metadata.pids.slice(start, start + itemsPerPage);
2017-05-26 22:28:12 -04:00
}
posts.getPostSummaryByPids(metadata.pids, data.uid, {}, next);
2017-05-26 22:28:12 -04:00
},
function (posts, next) {
// Append metadata to returned payload (without pids)
delete metadata.pids;
2018-02-02 11:55:55 -05:00
next(null, Object.assign({
posts: posts,
matchCount: matchCount,
pageCount: Math.max(1, Math.ceil(parseInt(matchCount, 10) / 10)),
}, metadata));
2017-05-26 22:28:12 -04:00
},
], callback);
2015-01-07 16:10:11 -05:00
}
2014-07-07 17:36:10 -04:00
2015-03-14 23:00:24 -04:00
function filterAndSort(pids, data, callback) {
2017-05-26 22:28:12 -04:00
async.waterfall([
function (next) {
getMatchedPosts(pids, data, next);
},
function (posts, next) {
2017-05-27 00:30:07 -04:00
if (!posts.length) {
2017-05-26 22:28:12 -04:00
return callback(null, pids);
}
posts = posts.filter(Boolean);
2017-05-26 22:28:12 -04:00
posts = filterByPostcount(posts, data.replies, data.repliesFilter);
posts = filterByTimerange(posts, data.timeRange, data.timeFilter);
posts = filterByTags(posts, data.hasTags);
2017-05-26 22:28:12 -04:00
sortPosts(posts, data);
2017-06-07 14:21:03 -04:00
plugins.fireHook('filter:search.filterAndSort', { pids: pids, posts: posts, data: data }, next);
},
function (result, next) {
2018-10-21 19:33:46 -04:00
pids = result.posts.map(post => post && post.pid);
2017-05-26 22:28:12 -04:00
next(null, pids);
},
], callback);
}
2015-03-14 23:00:24 -04:00
function getMatchedPosts(pids, data, callback) {
2017-06-08 14:12:07 -04:00
var postFields = ['pid', 'uid', 'tid', 'timestamp', 'deleted'];
var categoryFields = [];
2017-02-08 18:23:34 +03:00
if (data.sortBy && data.sortBy !== 'relevance') {
2017-06-08 14:12:07 -04:00
if (data.sortBy.startsWith('category.')) {
categoryFields.push(data.sortBy.split('.')[1]);
}
}
2018-10-21 19:33:46 -04:00
var postsData;
async.waterfall([
function (next) {
2018-10-21 19:33:46 -04:00
posts.getPostsFields(pids, postFields, next);
},
2018-10-21 19:33:46 -04:00
function (_postsData, next) {
postsData = _postsData.filter(post => post && !post.deleted);
async.parallel({
users: function (next) {
if (data.sortBy && data.sortBy.startsWith('user')) {
2018-10-21 19:33:46 -04:00
var uids = postsData.map(post => post.uid);
2015-09-25 17:38:58 -04:00
user.getUsersFields(uids, ['username'], next);
} else {
next();
}
},
topics: function (next) {
2016-12-19 21:40:09 +03:00
var topicsData;
2018-10-21 19:33:46 -04:00
const tids = postsData.map(post => post.tid);
async.waterfall([
function (next) {
2018-10-21 19:33:46 -04:00
topics.getTopicsData(tids, next);
},
function (_topics, next) {
2016-12-19 21:40:09 +03:00
topicsData = _topics;
async.parallel({
teasers: function (next) {
2017-06-08 14:29:14 -04:00
if (data.sortBy && data.sortBy.startsWith('teaser')) {
2018-10-21 19:33:46 -04:00
var teaserKeys = topicsData.map(topic => 'post:' + topic.teaserPid);
db.getObjectsFields(teaserKeys, ['timestamp'], next);
} else {
next();
}
},
categories: function (next) {
if (!categoryFields.length) {
return next();
}
2018-10-21 19:33:46 -04:00
var cids = topicsData.map(topic => 'category:' + topic.cid);
db.getObjectsFields(cids, categoryFields, next);
2016-12-19 21:40:09 +03:00
},
tags: function (next) {
2017-12-11 11:21:22 -05:00
if (Array.isArray(data.hasTags) && data.hasTags.length) {
2016-12-19 21:40:09 +03:00
topics.getTopicsTags(tids, next);
} else {
setImmediate(next);
}
2017-02-17 19:31:21 -07:00
},
}, next);
2017-02-17 19:31:21 -07:00
},
2017-05-26 22:28:12 -04:00
function (results, next) {
topicsData.forEach(function (topic, index) {
if (topic && results.categories && results.categories[index]) {
topic.category = results.categories[index];
}
if (topic && results.teasers && results.teasers[index]) {
topic.teaser = results.teasers[index];
}
if (topic && results.tags && results.tags[index]) {
topic.tags = results.tags[index];
}
});
2017-05-26 22:28:12 -04:00
next(null, topicsData);
},
], next);
2017-02-17 19:31:21 -07:00
},
}, next);
},
function (results, next) {
2018-10-21 19:33:46 -04:00
postsData.forEach(function (post, index) {
if (results.topics && results.topics[index]) {
post.topic = results.topics[index];
if (results.topics[index].category) {
post.category = results.topics[index].category;
}
if (results.topics[index].teaser) {
post.teaser = results.topics[index].teaser;
}
}
if (results.users && results.users[index]) {
post.user = results.users[index];
}
});
2018-10-21 19:33:46 -04:00
postsData = postsData.filter(post => post && post.topic && !post.topic.deleted);
next(null, postsData);
2017-02-17 19:31:21 -07:00
},
], callback);
}
function filterByPostcount(posts, postCount, repliesFilter) {
postCount = parseInt(postCount, 10);
if (postCount) {
if (repliesFilter === 'atleast') {
posts = posts.filter(function (post) {
return post.topic && post.topic.postcount >= postCount;
});
} else {
posts = posts.filter(function (post) {
return post.topic && post.topic.postcount <= postCount;
});
}
}
return posts;
}
function filterByTimerange(posts, timeRange, timeFilter) {
2017-02-18 14:29:52 -07:00
timeRange = parseInt(timeRange, 10) * 1000;
if (timeRange) {
var time = Date.now() - timeRange;
if (timeFilter === 'newer') {
posts = posts.filter(function (post) {
return post.timestamp >= time;
});
} else {
posts = posts.filter(function (post) {
return post.timestamp <= time;
});
}
}
return posts;
}
2016-12-19 21:40:09 +03:00
function filterByTags(posts, hasTags) {
2017-12-11 11:21:22 -05:00
if (Array.isArray(hasTags) && hasTags.length) {
2016-12-19 21:40:09 +03:00
posts = posts.filter(function (post) {
var hasAllTags = false;
2017-12-11 11:21:22 -05:00
if (post && post.topic && Array.isArray(post.topic.tags) && post.topic.tags.length) {
2016-12-19 21:40:09 +03:00
hasAllTags = hasTags.every(function (tag) {
return post.topic.tags.includes(tag);
2016-12-19 21:40:09 +03:00
});
}
return hasAllTags;
});
}
return posts;
}
function sortPosts(posts, data) {
2017-02-08 18:23:34 +03:00
if (!posts.length || !data.sortBy || data.sortBy === 'relevance') {
return;
}
data.sortDirection = data.sortDirection || 'desc';
2015-10-11 21:56:28 -04:00
var direction = data.sortDirection === 'desc' ? 1 : -1;
if (data.sortBy === 'timestamp') {
posts.sort(function (p1, p2) {
2015-10-11 21:56:28 -04:00
return direction * (p2.timestamp - p1.timestamp);
});
return;
}
var firstPost = posts[0];
var fields = data.sortBy.split('.');
if (!fields || fields.length !== 2 || !firstPost[fields[0]] || !firstPost[fields[0]][fields[1]]) {
return;
}
2015-10-11 21:56:28 -04:00
var isNumeric = utils.isNumber(firstPost[fields[0]][fields[1]]);
if (isNumeric) {
posts.sort(function (p1, p2) {
2015-10-11 21:56:28 -04:00
return direction * (p2[fields[0]][fields[1]] - p1[fields[0]][fields[1]]);
});
} else {
posts.sort(function (p1, p2) {
2015-09-27 14:56:27 -04:00
if (p1[fields[0]][fields[1]] > p2[fields[0]][fields[1]]) {
return direction;
} else if (p1[fields[0]][fields[1]] < p2[fields[0]][fields[1]]) {
return -direction;
}
return 0;
});
}
}
2015-03-14 23:00:24 -04:00
function getSearchCids(data, callback) {
if (!Array.isArray(data.categories) || !data.categories.length) {
2015-02-05 14:55:36 -05:00
return callback(null, []);
}
if (data.categories.includes('all')) {
return categories.getCidsByPrivilege('categories:cid', data.uid, 'read', callback);
}
2017-05-26 22:28:12 -04:00
async.waterfall([
function (next) {
async.parallel({
watchedCids: function (next) {
if (data.categories.includes('watched')) {
2017-05-26 22:28:12 -04:00
user.getWatchedCategories(data.uid, next);
} else {
next(null, []);
}
},
childrenCids: function (next) {
if (data.searchChildren) {
getChildrenCids(data.categories, data.uid, next);
} else {
next(null, []);
}
},
}, next);
2015-02-05 14:55:36 -05:00
},
2017-05-26 22:28:12 -04:00
function (results, next) {
2017-06-25 20:00:05 -04:00
var cids = results.watchedCids.concat(results.childrenCids).concat(data.categories).filter(Boolean);
cids = _.uniq(cids);
2017-05-26 22:28:12 -04:00
next(null, cids);
2017-02-17 19:31:21 -07:00
},
2017-05-26 22:28:12 -04:00
], callback);
2015-02-05 14:55:36 -05:00
}
function getChildrenCids(cids, uid, callback) {
2017-05-26 22:28:12 -04:00
async.waterfall([
function (next) {
categories.getChildren(cids, uid, next);
},
function (childrenCategories, next) {
var childrenCids = [];
var allCategories = [];
childrenCategories.forEach(function (childrens) {
categories.flattenCategories(allCategories, childrens);
2018-10-21 19:33:46 -04:00
childrenCids = childrenCids.concat(allCategories.map(category => category && category.cid));
2017-05-26 22:28:12 -04:00
});
2015-02-05 14:55:36 -05:00
2017-05-26 22:28:12 -04:00
next(null, childrenCids);
},
], callback);
2015-02-05 14:55:36 -05:00
}
2015-03-14 23:00:24 -04:00
function getSearchUids(data, callback) {
if (data.postedBy) {
2015-03-15 01:45:24 -04:00
user.getUidsByUsernames(Array.isArray(data.postedBy) ? data.postedBy : [data.postedBy], callback);
2015-03-14 23:00:24 -04:00
} else {
callback(null, []);
}
}