mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 16:46:12 +01:00
fix: user faster method if sorting by smallest set
This commit is contained in:
@@ -76,48 +76,32 @@ module.exports = function (module) {
|
|||||||
|
|
||||||
async function intersectSingle(params) {
|
async function intersectSingle(params) {
|
||||||
const objects = module.client.collection('objects');
|
const objects = module.client.collection('objects');
|
||||||
|
const sortSet = params.sets[params.weights.indexOf(1)];
|
||||||
|
if (sortSet === params.counts.smallestSet) {
|
||||||
|
return await intersectBatch(params);
|
||||||
|
}
|
||||||
|
|
||||||
let items = await objects.find({ _key: params.counts.smallestSet }, {
|
let items = await objects.find({ _key: params.counts.smallestSet }, {
|
||||||
projection: { _id: 0, value: 1 },
|
projection: { _id: 0, value: 1 },
|
||||||
}).toArray();
|
}).toArray();
|
||||||
const sortSet = params.sets[params.weights.indexOf(1)];
|
|
||||||
const otherSets = params.sets.filter(s => s !== params.counts.smallestSet);
|
|
||||||
const project = { _id: 0, value: 1 };
|
const project = { _id: 0, value: 1 };
|
||||||
if (params.withScores) {
|
if (params.withScores) {
|
||||||
project.score = 1;
|
project.score = 1;
|
||||||
}
|
}
|
||||||
|
const otherSets = params.sets.filter(s => s !== params.counts.smallestSet);
|
||||||
if (sortSet !== params.counts.smallestSet) {
|
// move sortSet to the end of array
|
||||||
// move sortSet to the end of array
|
otherSets.push(otherSets.splice(otherSets.indexOf(sortSet), 1)[0]);
|
||||||
otherSets.push(otherSets.splice(otherSets.indexOf(sortSet), 1)[0]);
|
for (let i = 0; i < otherSets.length; i++) {
|
||||||
for (let i = 0; i < otherSets.length; i++) {
|
/* eslint-disable no-await-in-loop */
|
||||||
/* eslint-disable no-await-in-loop */
|
const cursor = objects.find({ _key: otherSets[i], value: { $in: items.map(i => i.value) } });
|
||||||
const cursor = objects.find({ _key: otherSets[i], value: { $in: items.map(i => i.value) } });
|
// at the last step sort by sortSet
|
||||||
// at the last step sort by sortSet
|
if (i === otherSets.length - 1) {
|
||||||
if (i === otherSets.length - 1) {
|
cursor.project(project).sort({ score: params.sort }).skip(params.start).limit(params.limit);
|
||||||
cursor.project(project).sort({ score: params.sort }).skip(params.start).limit(params.limit);
|
} else {
|
||||||
} else {
|
cursor.project({ _id: 0, value: 1 });
|
||||||
cursor.project({ _id: 0, value: 1 });
|
|
||||||
}
|
|
||||||
items = await cursor.toArray();
|
|
||||||
}
|
}
|
||||||
} else {
|
items = await cursor.toArray();
|
||||||
for (let i = 0; i < otherSets.length; i++) {
|
|
||||||
/* eslint-disable no-await-in-loop */
|
|
||||||
items = await module.client.collection('objects').find({
|
|
||||||
_key: otherSets[i], value: { $in: items.map(i => i.value) },
|
|
||||||
}, { projection: { _id: 0, value: 1 } }).toArray();
|
|
||||||
}
|
|
||||||
if (!items.length) {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
items = await objects.find({ _key: sortSet, value: { $in: items.map(i => i.value) } })
|
|
||||||
.project(project)
|
|
||||||
.sort({ score: params.sort })
|
|
||||||
.skip(params.start)
|
|
||||||
.limit(params.limit)
|
|
||||||
.toArray();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!params.withScores) {
|
if (!params.withScores) {
|
||||||
items = items.map(i => i.value);
|
items = items.map(i => i.value);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user