mirror of
				https://github.com/NodeBB/NodeBB.git
				synced 2025-10-31 11:05:54 +01:00 
			
		
		
		
	closes #4945
This commit is contained in:
		| @@ -595,4 +595,84 @@ module.exports = function(db, module) { | ||||
| 			callback | ||||
| 		); | ||||
| 	}; | ||||
|  | ||||
|  | ||||
| 	module.getSortedSetIntersect = function(params, callback) { | ||||
| 		params.sort = 1; | ||||
| 		getSortedSetRevIntersect(params, callback); | ||||
| 	}; | ||||
|  | ||||
| 	module.getSortedSetRevIntersect = function(params, callback) { | ||||
| 		params.sort = -1; | ||||
| 		getSortedSetRevIntersect(params, callback); | ||||
| 	}; | ||||
|  | ||||
| 	function getSortedSetRevIntersect (params, callback) { | ||||
| 		var sets = params.sets; | ||||
| 		var start = params.hasOwnProperty('start') ? params.start : 0; | ||||
| 		var stop = params.hasOwnProperty('stop') ? params.stop : -1; | ||||
| 		var weights = params.weights || []; | ||||
| 		var aggregate = {}; | ||||
|  | ||||
| 		if (params.aggregate) { | ||||
| 			aggregate['$' + params.aggregate.toLowerCase()] = '$score'; | ||||
| 		} else { | ||||
| 			aggregate.$sum = '$score'; | ||||
| 		} | ||||
|  | ||||
| 		var limit = stop - start + 1; | ||||
| 		if (limit <= 0) { | ||||
| 			limit = 0; | ||||
| 		} | ||||
|  | ||||
| 		var pipeline = []; | ||||
|  | ||||
| 		pipeline.push({ $match: { _key: {$in: sets}} }); | ||||
|  | ||||
| 		weights.forEach(function(weight, index) { | ||||
| 			if (weight !== 1) { | ||||
| 				pipeline.push({ | ||||
| 					$project: { | ||||
| 						value: 1, | ||||
| 						score: { | ||||
| 							$cond: { if: { $eq: [ "$_key", sets[index] ] }, then: { $multiply: [ '$score', weight ] }, else: '$score' } | ||||
| 						} | ||||
| 					} | ||||
| 				}); | ||||
| 			} | ||||
| 		}); | ||||
|  | ||||
| 		pipeline.push({ $group: { _id: {value: '$value'}, totalScore: aggregate, count: {$sum: 1}} }); | ||||
| 		pipeline.push({ $match: { count: sets.length} }); | ||||
| 		pipeline.push({ $sort: { totalScore: params.sort} }); | ||||
|  | ||||
| 		if (start) { | ||||
| 			pipeline.push({ $skip: start }); | ||||
| 		} | ||||
|  | ||||
| 		if (limit > 0) { | ||||
| 			pipeline.push({ $limit: limit }); | ||||
| 		} | ||||
|  | ||||
| 		var project = { _id: 0, value: '$_id.value'}; | ||||
| 		if (params.withScores) { | ||||
| 			project.score = '$totalScore'; | ||||
| 		} | ||||
| 		pipeline.push({ $project: project }); | ||||
|  | ||||
| 		db.collection('objects').aggregate(pipeline, function(err, data) { | ||||
| 			if (err || !data) { | ||||
| 				return callback(err); | ||||
| 			} | ||||
|  | ||||
| 			if (!params.withScores) { | ||||
| 				data = data.map(function(item) { | ||||
| 					return item.value; | ||||
| 				}); | ||||
| 			} | ||||
|  | ||||
| 			callback(null, data); | ||||
| 		}); | ||||
| 	} | ||||
|  | ||||
| }; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user