Hi Nick, This in fact was also something I came up with. I ditched it because, like you said, it requires to update all the descendants. Which means a lot of extra overhead. Not only the direct descendants, but all the descendants. So a top comment could easily be replied more than 20 times, and each of those other 20, etc. etc. This would mean that I would have to update more than 50 documents each time a user clicks on the "upvote" button - not to mention the conflicts.
Isn't there a better way? On Wed, May 16, 2012 at 2:43 PM, Nick North <nort...@gmail.com> wrote: > I may be misunderstanding the problem, but would it work if you represent > each comment by a list of its score and its id? Say comments 1, 2, 3, 4 > have scores 40, 30, 20, 10 respectively, then the documents would become: > > { _id: 1, path: [[40,"1"]] }, > { _id: 2, path: [[40,"1"], [30,"2"]] }, > { _id: 3, path: [[40,"1"], [30,"2"], [20,"3"]] } > { _id: 4, path: [[40,"1"], [10,"4"]] } > Then indexing on the path will now put comment 4 before comment 2. If two > comments with the same parent have the same score, they will be ordered by > id. If you want the date in the ordering, that's just another element in > the list. > The problem with this is that any change to the score of a parent comment > requires the paths of all its descendants to be updated. But it feels as if > that might be inevitable, as changing the parent score may move an entire > subtree around in the ordering so the index of all elements of the subtree > must change. > > Nick > > On 16 May 2012 12:27, Luca Matteis <lmatt...@gmail.com> wrote: > >> I know how the ranking works actually. I described how I'd like to >> concentrate on simply ranking each level by its points. >> The issue is actually in the adapting this structure to Couch. >> >> On Wed, May 16, 2012 at 12:26 PM, Tim McNamara >> <paperl...@timmcnamara.co.nz> wrote: >> > It may be worth looking at the reddit source code ( >> github.com/reddit/reddit) >> > to get some tips. Then you could adapt this into Couch. >> > On May 16, 2012 9:41 PM, "Luca Matteis" <lmatt...@gmail.com> wrote: >> > >> >> I'm trying to implement a basic way of displaying comments in the way >> >> that Hacker News provides, using CouchDB. Not only ordered >> >> hierarchically, but also, each level of the tree should be ordered by >> >> a "points" variable. If you're familiar with sites such as Hacker News >> >> or Reddit you know that each level of the comment tree is ordered by >> >> the amount of points and also the date they were posted - however, for >> >> sake of simplicity I'll just talk about a "points" variable. >> >> >> >> The idea is that I want a view to return it in the order I except, and >> >> not make many Ajax calls for example, to retrieve them and make them >> >> look like they're ordered correctly. >> >> >> >> This is what I got so far: Each document is a "comment". Each comment >> >> has a property `path` which is an ordered list containing all its >> >> parents. So for example, imagine I have 4 comments (with _id `1`, `2`, >> >> `3` and `4`). Comment `2` is children of `1`, comment `3` is children >> >> of `2`, and comment `4` is also children of `1`. This is what the data >> >> would look like: >> >> >> >> { _id: 1, path: ["1"] }, >> >> { _id: 2, path: ["1", "2"] }, >> >> { _id: 3, path: ["1", "2", "3"] } >> >> { _id: 4, path: ["1", "4"] } >> >> >> >> This works quite well for the hierarchy. A simple `view` will already >> >> return things ordered with each comment underneath the correct one. >> >> >> >> The issue comes when I want to order each "level" of the tree >> >> independently. So for example documents `2` and `4` belong to the same >> >> branch, but are ordered, on that level, by their ID. Instead I want >> >> them ordered based on a "points" variable that I want to add to the >> >> path - but can't seem to understand where I could be adding this >> >> variable for it to work the way I want it. >> >> >> >> Is there a way to do this? Consider that the "points" variable will >> >> change in time. >> >> >> >> Thank you, >> >> Luca >> >> >>