简体   繁体   English

CouchDB - 具有排名的分层评论。黑客新闻风格

[英]CouchDB - hierarchical comments with ranking. Hacker News style

I'm trying to implement a basic way of displaying comments in the way that Hacker News provides, using CouchDB. 我正在尝试使用CouchDB以Hacker News提供的方式实现显示评论的基本方式。 Not only ordered hierarchically, but also, each level of the tree should be ordered by 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. 我的想法是,我想要一个视图按照我的顺序返回它,而不是例如进行许多Ajax调用,以检索它们并使它们看起来像是正确排序的。

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. 每个注释都有一个属性path ,它是一个包含其所有父项的有序列表。

So for example, imagine I have 4 comments (with _id 1 , 2 , 3 and 4 ). 因此,例如,想象我有4个注释(以_id 1234 )。 Comment 2 is children of 1 , comment 3 is children of 2 , and comment 4 is also children of 1 . 评论21孩子,评论32孩子,评语4也是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 the way I want it. 一个简单的view已经按照我想要的方式返回订单。

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. 因此,例如,文档24属于同一分支,但在该级别上按其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. 考虑“点”变量将随时间变化。

Because each level needs to be sorted recursively by score, Couch needs to know the score of each parent to make this work the way you want it to. 因为每个级别需要按分数递归排序,所以Couch需要知道每个父级的分数,以使其按照您希望的方式工作。

Taking your example with the following scores (1: 10, 2: 10, 3: 10, 4: 20 ) 以下面的分数为例(1:10,2:10,3:10,4: 20

In this case you'd want the ordering to come out like the following: 在这种情况下,您希望订购如下:

.1
.1.4
.1.2
.1.2.3

Your document needs a scores array like this: 您的文档需要像这样的分数数组:

{ _id: 1, path: [1], scores: [10] },
{ _id: 2, path: [1, 2], scores: [10,10] },
{ _id: 3, path: [1, 2, 3], scores: [10,10,10] },
{ _id: 4, path: [1, 4], scores: [10,20] }

Then you'll use the following sort key in your view. 然后,您将在视图中使用以下排序键。

emit([doc.scores, doc.path], doc)

The path gets used as a tiebreaker because there will be cases where sibling comments have the exact same score. 路径被用作决胜局,因为会出现兄弟评论具有完全相同分数的情况。 Without the tiebreaker, their descendants could lose their grouping (by chain of ancestry). 没有决胜局,他们的后代可能会失去他们的分组(通过祖先链)。

Note: This approach will return scores from low-to-high, whereas you probably want scores (high to low) and path/tiebreaker(low to high). 注意:此方法将从低到高返回分数,而您可能需要分数(从高到低)和路径/决胜局(从低到高)。 So a workaround for this would be to populate the scores array with the inverse of each score like this: 因此,解决方法是使用每个得分的倒数填充得分数组,如下所示:

{ _id: 1, path: [1], scores: [0.1] },
{ _id: 2, path: [1, 2], scores: [0.1,0.1] },
{ _id: 3, path: [1, 2, 3], scores: [0.1,0.1,0.1] },
{ _id: 4, path: [1, 4], scores: [0.1,0.2] }

and then use descending=true when you request the view. 然后在请求视图时使用descending=true

Maybe anybody interestingly the thread on this question with variants of solutions: 也许有人有趣的是这个问题的线程与解决方案的变体:

http://mail-archives.apache.org/mod_mbox/couchdb-dev/201205.mbox/thread -> theme "Hierarchical comments Hacker News style" 16/05/2012 http://mail-archives.apache.org/mod_mbox/couchdb-dev/201205.mbox/thread - >主题“分层评论黑客新闻风格”16/05/2012

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM