|
|
@ -14,29 +14,16 @@ module.exports = function (module) {
|
|
|
|
projection: { _id: 0, value: 1 },
|
|
|
|
projection: { _id: 0, value: 1 },
|
|
|
|
}).toArray();
|
|
|
|
}).toArray();
|
|
|
|
|
|
|
|
|
|
|
|
items = items.map(i => i.value);
|
|
|
|
|
|
|
|
const otherSets = keys.filter(s => s !== counts.smallestSet);
|
|
|
|
const otherSets = keys.filter(s => s !== counts.smallestSet);
|
|
|
|
if (otherSets.length === 1) {
|
|
|
|
for (let i = 0; i < otherSets.length; i++) {
|
|
|
|
return await objects.countDocuments({
|
|
|
|
|
|
|
|
_key: otherSets[0], value: { $in: items },
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
items = await intersectValuesWithSets(items, otherSets);
|
|
|
|
|
|
|
|
return items.length;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async function intersectValuesWithSets(items, sets) {
|
|
|
|
|
|
|
|
for (let i = 0; i < sets.length; i++) {
|
|
|
|
|
|
|
|
/* eslint-disable no-await-in-loop */
|
|
|
|
/* eslint-disable no-await-in-loop */
|
|
|
|
items = await module.client.collection('objects').find({
|
|
|
|
const query = { _key: otherSets[i], value: { $in: items.map(i => i.value) } };
|
|
|
|
_key: sets[i], value: { $in: items },
|
|
|
|
if (i === otherSets.length - 1) {
|
|
|
|
}, {
|
|
|
|
return await objects.countDocuments(query);
|
|
|
|
projection: { _id: 0, value: 1 },
|
|
|
|
}
|
|
|
|
}).toArray();
|
|
|
|
items = await objects.find(query, { projection: { _id: 0, value: 1 } }).toArray();
|
|
|
|
items = items.map(i => i.value);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return items;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async function countSets(sets, limit) {
|
|
|
|
async function countSets(sets, limit) {
|
|
|
|
const objects = module.client.collection('objects');
|
|
|
|
const objects = module.client.collection('objects');
|
|
|
@ -92,30 +79,49 @@ module.exports = function (module) {
|
|
|
|
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();
|
|
|
|
if (!items.length) {
|
|
|
|
const sortSet = params.sets[params.weights.indexOf(1)];
|
|
|
|
return [];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const otherSets = params.sets.filter(s => s !== params.counts.smallestSet);
|
|
|
|
const otherSets = params.sets.filter(s => s !== params.counts.smallestSet);
|
|
|
|
items = await intersectValuesWithSets(items.map(i => i.value), otherSets);
|
|
|
|
|
|
|
|
if (!items.length) {
|
|
|
|
|
|
|
|
return [];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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 sortSet = params.sets[params.weights.indexOf(1)];
|
|
|
|
|
|
|
|
let res = await objects
|
|
|
|
if (sortSet !== params.counts.smallestSet) {
|
|
|
|
.find({ _key: sortSet, value: { $in: items } }, { projection: project })
|
|
|
|
// move sortSet to the end of array
|
|
|
|
.sort({ score: params.sort })
|
|
|
|
otherSets.push(otherSets.splice(otherSets.indexOf(sortSet), 1)[0]);
|
|
|
|
.skip(params.start)
|
|
|
|
for (let i = 0; i < otherSets.length; i++) {
|
|
|
|
.limit(params.limit)
|
|
|
|
/* eslint-disable no-await-in-loop */
|
|
|
|
.toArray();
|
|
|
|
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 [];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
res = res.map(i => i.value);
|
|
|
|
items = items.map(i => i.value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
return items;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async function intersectBatch(params) {
|
|
|
|
async function intersectBatch(params) {
|
|
|
@ -159,7 +165,7 @@ module.exports = function (module) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async function intersectAggregate(params) {
|
|
|
|
async function intersectAggregate(params) {
|
|
|
|
var aggregate = {};
|
|
|
|
const aggregate = {};
|
|
|
|
|
|
|
|
|
|
|
|
if (params.aggregate) {
|
|
|
|
if (params.aggregate) {
|
|
|
|
aggregate['$' + params.aggregate.toLowerCase()] = '$score';
|
|
|
|
aggregate['$' + params.aggregate.toLowerCase()] = '$score';
|
|
|
|