mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-01 11:35:55 +01:00
proper score aggregation #1562
This commit is contained in:
@@ -163,28 +163,35 @@ module.exports = function(db, module) {
|
|||||||
stop = -1;
|
stop = -1;
|
||||||
}
|
}
|
||||||
var limit = stop - start + 1;
|
var limit = stop - start + 1;
|
||||||
if (limit < 0) {
|
if (limit <= 0) {
|
||||||
limit = 0;
|
limit = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
db.collection('objects').find({_key:{$in: sets}}, {fields: {_id: 0, value: 1, score: 1}})
|
var pipeline = [
|
||||||
.limit(limit)
|
{ $match: { _key: {$in: sets}} },
|
||||||
.skip(start)
|
{ $group: { _id: {value: '$value'}, totalScore: {$sum : "$score"}} },
|
||||||
.sort({score: sort})
|
{ $sort: { totalScore: sort} }
|
||||||
.toArray(function(err, data) {
|
];
|
||||||
if (err || !data) {
|
|
||||||
return callback(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
data = data.map(function(item) {
|
if (start) {
|
||||||
return item.value;
|
pipeline.push({ $skip: start });
|
||||||
});
|
}
|
||||||
|
|
||||||
data = data.filter(function(value, index, array) {
|
if (limit > 0) {
|
||||||
return array.indexOf(value) === index;
|
pipeline.push({ $limit: limit });
|
||||||
});
|
}
|
||||||
|
|
||||||
callback(null, data);
|
pipeline.push({ $project: { _id: 0, value: '$_id.value' }});
|
||||||
|
|
||||||
|
db.collection('objects').aggregate(pipeline, function(err, data) {
|
||||||
|
if (err || !data) {
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
data = data.map(function(item) {
|
||||||
|
return item.value;
|
||||||
});
|
});
|
||||||
|
callback(null, data);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
Reference in New Issue
Block a user