proper score aggregation

v1.18.x
barisusakli
parent 5e51895982
commit b7fcde446b

@ -163,15 +163,27 @@ module.exports = function(db, module) {
stop = -1;
}
var limit = stop - start + 1;
if (limit < 0) {
if (limit <= 0) {
limit = 0;
}
db.collection('objects').find({_key:{$in: sets}}, {fields: {_id: 0, value: 1, score: 1}})
.limit(limit)
.skip(start)
.sort({score: sort})
.toArray(function(err, data) {
var pipeline = [
{ $match: { _key: {$in: sets}} },
{ $group: { _id: {value: '$value'}, totalScore: {$sum : "$score"}} },
{ $sort: { totalScore: sort} }
];
if (start) {
pipeline.push({ $skip: start });
}
if (limit > 0) {
pipeline.push({ $limit: limit });
}
pipeline.push({ $project: { _id: 0, value: '$_id.value' }});
db.collection('objects').aggregate(pipeline, function(err, data) {
if (err || !data) {
return callback(err);
}
@ -179,11 +191,6 @@ module.exports = function(db, module) {
data = data.map(function(item) {
return item.value;
});
data = data.filter(function(value, index, array) {
return array.indexOf(value) === index;
});
callback(null, data);
});
}

Loading…
Cancel
Save