if posts of users consists comments and likes and date of created, how can I implement some sort of ranking that involve number of comments and likes and date of created and returning posts from highest post to lowest.
What you are looking for is called Ranked Feeds. Ranked feeds allows for custom ranking of activities; you can use your own formula to calculates the score of each activity. As long as your activities have the required information (eg. likes_count
, comments_count
) than you can use this to rank your feeds.
Here's a simple example of a ranking formula:
{
"score": "likes_count + comments_count",
"defaults": {
"likes_count": 0,
"comments_count": 0
}
}
A very common approach is to combine some popularity score with the age of the activity. To do this you can use one of the time decay functions and multiply the results with the sum of likes and comments.
For example:
{
"functions":{
"popularity_gauss":{
"base":"decay_gauss",
"scale":"5d",
"offset":"1d",
"decay":"0.3"
}
},
"defaults": {
"likes_count": 0,
"comments_count": 0
},
"score":"popularity_gauss(time) * (likes_count + comments_count)"
}
The example is using the decay_gauss
function with scale, offset and decay parameters. With the chosen parameters, activities will get their score decreased as they cross the 1 day old threshold. The score will be reduced to a third for activities that are 5 days old.
Syncing data
The two counters that we used in the example likes_count
and comments_count
are clearly changing over time. For Ranked Feeds to work, you need to synchronize these values. Activity data can be updated using the APIs.
Note: Since the activity update API does not support partial updates, it is recommended to not update activities transactionally (eg. user clicks on like -> send API call to update counter from X to X+1) but to perform updates in bulk every X seconds.
Crontab example every 5 minutes:
*/5 * * * * /path/to/script/update_activities_counters