Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find most recent & closest posts, limit 20

Let's say I have a bunch of posts (for a feed, like a Twitter/Facebook/foursquare feed) in MongoDB, and each post has a location & a timestamp.

What's the best way to get the most recent & closest posts, limited to 20 posts?

This is also a subjective question. Let's say that you can specify $maxDistance and the max time since now (I'm not sure how you'd do it otherwise.). How would you specify them? Would you sort by most recent or closest, or keep it random or sort some other way? Which sorting algorithm do you think is most interesting?

like image 338
ma11hew28 Avatar asked May 14 '11 18:05

ma11hew28


1 Answers

I suppose you ultimately end up with a list of posts that have two discrete ranking dimensions, i.e.:

{ age: 86400, distance: 1000 }
{ age: 172800, distance: 5000 }
{ age: 57600, distance: 20000 }
{ age: 288000, distance: 8000 }

Doesn't really matter what the units are, lets say seconds and metres. If you want both to affect the sorting rank then you end up with a ranking algorithm, at its simplest something like this:

rank = (C1 * age) + (C2 * distance)

Where C1 and C2 are constants you can tweak to tune the weightings. The values will depend what units you're using, and how much ranking influence you assign to each dimension.

Another option could be ordering first by a time aggregate then distance, so all posts from today ordered by distance; followed by yesterday's ordered by distance, and so on. Or vice-versa, ordering by a distance range, then age, so all within (0 - 1000m) ordered by age; followed by all within (1001 - 2000m), and so on.

like image 147
Chris Fulstow Avatar answered Oct 06 '22 10:10

Chris Fulstow