javascript - Meteor - publish a collection sorted on personalized score -
i want publish collection, documents scored according combination of fields , context determined user; , returned result sorted based on score, , limited top results.
this shows how add transform filter on collection @ moment of publication. however, i'm not sure transform efficient approach computing field want use in sort..
finally, awesome use mongo map reduce return recommended items based on computed score, can done in parallel.
@brett-mclain pointed out how map reduce sorting can done in pure mongo. there few meteor packages out there extend meteor collections, not find how use them in meteor publication (vs in method) besides, here map reduce output send collection apparently overwritten each time method called.
here logic i'd achieve:
/* server */ meteor.publish('getrecommendeditems', function() { var u = users.findone(this.userid); var scoreitem = function(item, u) { ... }; return items.find( {}, { transforminparallel: function(doc) { doc.score = scoreitem(doc); }, sort: {score: -1}, limit: 10, } ); }); /* client template*/ template.templatename.oncreated(function() { this.subscribe('getrecommendeditems'); this.items = items.find({}, sort: {score: -1}); }); where except score accessible in client without doing computation there.
it seems easier custom things in meteor.methods, meteor.publish feels natural place this; because it's meteor's magical reactivity takes place.
transforminparallel: function(doc) { doc.score = scoreitem(doc); }, sort: {score: -1}, limit: 10,
this result in full-database scan.
time-decay solutions, common programming test questions, aren't appropriate use case.
if scale small, precompute full set of
{score: ..., userid: ..., item: ...} documents in separate collection. indices billion small documents these fit entirely in single database server's ram; suitable @ e.g. 30,000 items , 30,000 users.
if needs exceed this, have think part of score calculation can shared between users. if no part of calculation can shared, should read how facebook implements social graphs on conventional databases.
Comments
Post a Comment