Sorting mongodb by reddit ranking algorithm

后端 未结 2 560
忘了有多久
忘了有多久 2020-12-17 03:56

Here is a js code to rank items according to Reddit\'s ranking algorithm.

My question is: how do I use this code to rank my mongodb documents ?

(Reddit\'s ra

2条回答
  •  独厮守ぢ
    2020-12-17 04:38

    Theres a problem with your function:

    new Date(1970, 1, 1) // Sun Feb 01 1970 00:00:00 GMT-0300 (BRT)
    

    Yep, month 1 is February, and it uses the systems timezone as well. Epoch in JavaScript is

    var epoch = new Date(Date.UTC(1970, 0, 1))
    

    Since

    epoch.getTime() // 0
    

    The function

    function epochSeconds(d){
        return (d.getTime() - new Date(1970,1,1).getTime())/1000;
    }
    

    should be just

    function epochSeconds(d){
        return d.getTime()/1000;
    }
    

    Compressing a bit, this returns exactly the same results as the python function in http://amix.dk/blog/post/19588

    function hot (ups, downs, date){
      var score = ups - downs;
      var order = Math.log(Math.max(Math.abs(score), 1)) / Math.LN10;
      var sign = score > 0 ? 1 : score < 0 ? -1 : 0;
      var seconds = (date.getTime()/1000) - 1134028003;
      var product = order + sign * seconds / 45000;
      return Math.round(product*10000000)/10000000;
    }
    

提交回复
热议问题