mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-02 03:55:55 +01:00
part 2 of search changes
This commit is contained in:
@@ -64,7 +64,9 @@ module.exports = function(db, module) {
|
|||||||
if (!id) {
|
if (!id) {
|
||||||
return callback();
|
return callback();
|
||||||
}
|
}
|
||||||
db.collection('search').remove({key: key, id: id}, callback);
|
db.collection('search').remove({key: key, id: id}, function(err, res) {
|
||||||
|
callback(err);
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
module.flushdb = function(callback) {
|
module.flushdb = function(callback) {
|
||||||
|
|||||||
@@ -2,19 +2,17 @@
|
|||||||
|
|
||||||
module.exports = function(redisClient, module) {
|
module.exports = function(redisClient, module) {
|
||||||
module.searchIndex = function(key, data, id, callback) {
|
module.searchIndex = function(key, data, id, callback) {
|
||||||
if (key === 'post') {
|
var method = key === 'post' ? module.postSearch : module.topicSearch;
|
||||||
module.postSearch.index(data, id, callback);
|
|
||||||
} else if(key === 'topic') {
|
method.index(data, id, function(err, res) {
|
||||||
module.topicSearch.index(data, id, callback);
|
callback(err);
|
||||||
}
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
module.search = function(key, data, limit, callback) {
|
module.search = function(key, data, limit, callback) {
|
||||||
if (key === 'post') {
|
var method = key === 'post' ? module.postSearch : module.topicSearch;
|
||||||
module.postSearch.query(data, 0, limit - 1, callback);
|
|
||||||
} else if(key === 'topic') {
|
method.query(data, 0, limit - 1, callback);
|
||||||
module.topicSearch.query(data, 0, limit - 1, callback);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
module.searchRemove = function(key, id, callback) {
|
module.searchRemove = function(key, id, callback) {
|
||||||
@@ -22,12 +20,11 @@ module.exports = function(redisClient, module) {
|
|||||||
if (!id) {
|
if (!id) {
|
||||||
return callback();
|
return callback();
|
||||||
}
|
}
|
||||||
|
var method = key === 'post' ? module.postSearch : module.topicSearch;
|
||||||
|
|
||||||
if (key === 'post') {
|
method.remove(id, function(err, res) {
|
||||||
module.postSearch.remove(id, callback);
|
callback(err);
|
||||||
} else if(key === 'topic') {
|
});
|
||||||
module.topicSearch.remove(id, callback);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
module.flushdb = function(callback) {
|
module.flushdb = function(callback) {
|
||||||
|
|||||||
@@ -104,7 +104,7 @@ console.log(results)
|
|||||||
privileges.posts.filter('read', results.pids, data.uid, next);
|
privileges.posts.filter('read', results.pids, data.uid, next);
|
||||||
},
|
},
|
||||||
function(pids, next) {
|
function(pids, next) {
|
||||||
filterAndSort(pids, data, results.searchCategories, next);
|
filterAndSort(pids, data, next);
|
||||||
},
|
},
|
||||||
function(pids, next) {
|
function(pids, next) {
|
||||||
matchCount = pids.length;
|
matchCount = pids.length;
|
||||||
@@ -124,18 +124,15 @@ console.log(results)
|
|||||||
}
|
}
|
||||||
|
|
||||||
function filterAndSort(pids, data, callback) {
|
function filterAndSort(pids, data, callback) {
|
||||||
async.parallel({
|
getMatchedPosts(pids, data, function(err, posts) {
|
||||||
posts: function(next) {
|
|
||||||
getMatchedPosts(pids, data, next);
|
|
||||||
}
|
|
||||||
}, function(err, results) {
|
|
||||||
if (err) {
|
if (err) {
|
||||||
return callback(err);
|
return callback(err);
|
||||||
}
|
}
|
||||||
if (!results.posts) {
|
|
||||||
|
if (!Array.isArray(posts) || !posts.length) {
|
||||||
return callback(null, pids);
|
return callback(null, pids);
|
||||||
}
|
}
|
||||||
var posts = results.posts.filter(Boolean);
|
posts = posts.filter(Boolean);
|
||||||
|
|
||||||
posts = filterByPostcount(posts, data.replies, data.repliesFilter);
|
posts = filterByPostcount(posts, data.replies, data.repliesFilter);
|
||||||
posts = filterByTimerange(posts, data.timeRange, data.timeFilter);
|
posts = filterByTimerange(posts, data.timeRange, data.timeFilter);
|
||||||
@@ -155,20 +152,14 @@ function getMatchedPosts(pids, data, callback) {
|
|||||||
var topicFields = [];
|
var topicFields = [];
|
||||||
var categoryFields = [];
|
var categoryFields = [];
|
||||||
|
|
||||||
if (data.postedBy) {
|
|
||||||
postFields.push('uid');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (data.sortBy && data.sortBy.startsWith('category.')) {
|
|
||||||
topicFields.push('cid');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (data.replies) {
|
if (data.replies) {
|
||||||
topicFields.push('postcount');
|
topicFields.push('postcount');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (data.sortBy) {
|
if (data.sortBy) {
|
||||||
if (data.sortBy.startsWith('topic.')) {
|
if (data.sortBy.startsWith('category')) {
|
||||||
|
topicFields.push('cid');
|
||||||
|
} else if (data.sortBy.startsWith('topic.')) {
|
||||||
topicFields.push(data.sortBy.split('.')[1]);
|
topicFields.push(data.sortBy.split('.')[1]);
|
||||||
} else if (data.sortBy.startsWith('user.')) {
|
} else if (data.sortBy.startsWith('user.')) {
|
||||||
postFields.push('uid');
|
postFields.push('uid');
|
||||||
@@ -281,25 +272,6 @@ function getMatchedPosts(pids, data, callback) {
|
|||||||
], callback);
|
], callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
function filterByUser(posts, postedByUid) {
|
|
||||||
if (postedByUid) {
|
|
||||||
postedByUid = parseInt(postedByUid, 10);
|
|
||||||
posts = posts.filter(function(post) {
|
|
||||||
return parseInt(post.uid, 10) === postedByUid;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return posts;
|
|
||||||
}
|
|
||||||
|
|
||||||
function filterByCategories(posts, searchCategories) {
|
|
||||||
if (searchCategories.length) {
|
|
||||||
posts = posts.filter(function(post) {
|
|
||||||
return post.topic && searchCategories.indexOf(post.topic.cid) !== -1;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return posts;
|
|
||||||
}
|
|
||||||
|
|
||||||
function filterByPostcount(posts, postCount, repliesFilter) {
|
function filterByPostcount(posts, postCount, repliesFilter) {
|
||||||
postCount = parseInt(postCount, 10);
|
postCount = parseInt(postCount, 10);
|
||||||
if (postCount) {
|
if (postCount) {
|
||||||
@@ -475,8 +447,8 @@ search.searchQuery = function(index, content, cids, uids, callback) {
|
|||||||
plugins.fireHook('filter:search.query', {
|
plugins.fireHook('filter:search.query', {
|
||||||
index: index,
|
index: index,
|
||||||
content: content,
|
content: content,
|
||||||
cids: cids,
|
cid: cids,
|
||||||
uids: uids
|
uid: uids
|
||||||
}, callback);
|
}, callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user