我将如何实现一个排名算法在我的网站数据库数据进行排序? [英] How would I implement a ranking algorithm in my website to sort database data?

查看:102
本文介绍了我将如何实现一个排名算法在我的网站数据库数据进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要实现在一个网站,我一直在努力,并决定去与黑客新闻的算法一个排名系统。我的理由选择这个算法很简单,因为它被描述这里

I want to implement a ranking system on a website I've been working on and have decided to go with the Hacker News algorithm. My reasoning for choosing this algorithm is simply because it's been described here.

我在看这条巨蟒code和无法弄清楚如何我会实现它(我用建立我的网站的语言)。

I was looking at this Python code (the language I'm using to build my site) and couldn't figure out how I would implement it.

def calculate_score(votes, item_hour_age, gravity=1.8):
    return (votes - 1) / pow((item_hour_age+2), gravity)

里的表:

posts:
    id | title | time_submitted

votes:
    id | postid | userid | score

我怎么会拉从数据库中的数据?理想的解决方案(最有效的),将构造一个MySQL查询检索前10个职位采用算法排名。但考虑到黑客新闻有它的弧实现的,这让我觉得他们拉出所有职位,然后通过算法运行它们来对他们进行排名。

How would I pull the data from the database? The ideal solution (most efficient) would be to construct a MySQL query to retrieve the top 10 posts ranked using the algorithm. But given that Hacker News has it implemented in Arc, it makes me think they pull out all the posts then run them through the algorithm to rank them.

书签交易也想到这个......他们用一个非关系型数据库模式,所以我会认为他们也像黑客新闻,进行排名在他们的code - 而不是数据库

Reddit also comes to mind for this... They use a non-relational database schema so I would assume they, like Hacker News, perform the rankings in their code - not the database.

你将如何实现这一点?

编辑:的一个职位能有多少票,因为我想记录哪些用户投票决定其职位

one post can have many votes as I would like to log which user votes on which post.

推荐答案

您可以使用您在 ORDER需要通过子句中的数据。

You can use the data you need in the ORDER BY clause.

SELECT p.id, p.title, p.time_submitted, SUM(v.score) as num_votes 
  FROM posts p, votes v
 WHERE v.postid = p.id
GROUP BY p.id
ORDER BY 
   (SUM(v.score) - 1) / POW(TIMESTAMPDIFF(HOUR,p.time_submitted,NOW()) + INTERVAL 2 HOUR, 1.8) DESC
LIMIT 100

这篇关于我将如何实现一个排名算法在我的网站数据库数据进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆