From 3ec05eeaf880ee4e784a05a540e06f08711d5614 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Thu, 21 May 2020 22:57:48 -0400 Subject: [PATCH] fix: user faster method if sorting by smallest set --- src/database/mongo/sorted/intersect.js | 50 +++++++++----------------- 1 file changed, 17 insertions(+), 33 deletions(-) diff --git a/src/database/mongo/sorted/intersect.js b/src/database/mongo/sorted/intersect.js index 32be949ae7..54c2f1f6f5 100644 --- a/src/database/mongo/sorted/intersect.js +++ b/src/database/mongo/sorted/intersect.js @@ -76,48 +76,32 @@ module.exports = function (module) { async function intersectSingle(params) { 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 }, { projection: { _id: 0, value: 1 }, }).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 }; if (params.withScores) { project.score = 1; } - - if (sortSet !== params.counts.smallestSet) { - // move sortSet to the end of array - otherSets.push(otherSets.splice(otherSets.indexOf(sortSet), 1)[0]); - for (let i = 0; i < otherSets.length; i++) { - /* eslint-disable no-await-in-loop */ - const cursor = objects.find({ _key: otherSets[i], value: { $in: items.map(i => i.value) } }); - // at the last step sort by sortSet - if (i === otherSets.length - 1) { - cursor.project(project).sort({ score: params.sort }).skip(params.start).limit(params.limit); - } else { - cursor.project({ _id: 0, value: 1 }); - } - items = await cursor.toArray(); - } - } else { - 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 []; + const otherSets = params.sets.filter(s => s !== params.counts.smallestSet); + // move sortSet to the end of array + otherSets.push(otherSets.splice(otherSets.indexOf(sortSet), 1)[0]); + for (let i = 0; i < otherSets.length; i++) { + /* eslint-disable no-await-in-loop */ + const cursor = objects.find({ _key: otherSets[i], value: { $in: items.map(i => i.value) } }); + // at the last step sort by sortSet + if (i === otherSets.length - 1) { + cursor.project(project).sort({ score: params.sort }).skip(params.start).limit(params.limit); + } else { + cursor.project({ _id: 0, value: 1 }); } - 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(); + items = await cursor.toArray(); } - if (!params.withScores) { items = items.map(i => i.value); }