mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 16:46:12 +01:00
refactor: dont save partial searches
This commit is contained in:
@@ -97,12 +97,26 @@ searchController.search = async function (req, res, next) {
|
|||||||
res.render('search', searchData);
|
res.render('search', searchData);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const searches = {};
|
||||||
|
|
||||||
async function recordSearch(data) {
|
async function recordSearch(data) {
|
||||||
const { query, searchIn } = data;
|
const { query, searchIn } = data;
|
||||||
if (query) {
|
if (query) {
|
||||||
const cleanedQuery = String(query).trim().toLowerCase().substr(0, 255);
|
const cleanedQuery = String(query).trim().toLowerCase().substr(0, 255);
|
||||||
if (['titles', 'titlesposts', 'posts'].includes(searchIn) && cleanedQuery.length > 2) {
|
if (['titles', 'titlesposts', 'posts'].includes(searchIn) && cleanedQuery.length > 2) {
|
||||||
await db.sortedSetIncrBy('searches:all', 1, cleanedQuery);
|
searches[data.uid] = searches[data.uid] || { timeoutId: 0, queries: [] };
|
||||||
|
searches[data.uid].queries.push(cleanedQuery);
|
||||||
|
if (searches[data.uid].timeoutId) {
|
||||||
|
clearTimeout(searches[data.uid].timeoutId);
|
||||||
|
}
|
||||||
|
searches[data.uid].timeoutId = setTimeout(async () => {
|
||||||
|
const copy = searches[data.uid].queries.slice();
|
||||||
|
const filtered = searches[data.uid].queries.filter(
|
||||||
|
q => !copy.find(query => query.startsWith(q) && query.length > q.length)
|
||||||
|
);
|
||||||
|
await Promise.all(filtered.map(query => db.sortedSetIncrBy('searches:all', 1, query)));
|
||||||
|
delete searches[data.uid];
|
||||||
|
}, 5000);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user