4

我在为这种社交网络类型的应用程序编写 Cypher 查询时遇到问题。它涉及添加帖子(本质上是带有描述的图像)的用户,用户可以查看这些帖子。

在 Cypher 中,图形模型是这样的: (user)-[:WROTE_REVIEW]->(review)-[:EVALUATES]->(post)

我正在尝试编写的查询应该返回特定用户已查看的所有帖子,以及有关帖子评论的一些一般信息。

这包括:

  1. (帖子的ID)
  2. 帖子的图片
  3. 帖子的描述
  4. 该帖子的评论总数
  5. 这篇文章的审稿人总数
  6. 本帖最后6位审稿人头像,按审稿日期排序,降序排列

我想我已经完成了前五项,但第 6 项给我带来了麻烦。下面的查询给了我所有的头像,而我只需要最后 6 个。

START user=node(2515)
MATCH (user)-[:WROTE_REVIEW]->()-[:EVALUATES]->(post)
WITH distinct post
MATCH (review)-[:EVALUATES]->(post)
WITH post, count(review) as reviews
MATCH (reviewer)-[:WROTE_REVIEW]->()-[:EVALUATES]->(post)
WITH post, reviews, count(distinct reviewer) as reviewers, collect(distinct reviewer.Avatar) as avatars
ORDER BY post.CreationTime DESC
RETURN post.Id, post.Image, post.Description, reviews, reviewers, avatars;

有人可以告诉我如何按审查日期(即review.CreationTime)降序排列头像,并取前六项吗?

谢谢!

4

2 回答 2

4

您可以先对行进行排序,然后取出审阅者集合的前 6 个,而不是对集合进行排序。所以把最后一场比赛改成这样,

MATCH (reviewer)-[:WROTE_REVIEW]->(review)-[:EVALUATES]->(post)
With distinct post, reviewer, review
ORDER BY post.CreationTime DESC, review.CreationTime DESC
Return post, count(reviewer) as reviewers, collect(reviewer.Avatar)[0..5] as avatars

访问索引如[0..5]的集合需要2.0 M5的版本。

于 2013-10-17T13:36:00.667 回答
3

Neo4j 2.0 中的集合切片是关键。由于出现了一些重复项,因此我不得不distinct在结果中添加一些子句。这就是我最终的结果:

START user=node(2515)
MATCH (user)-[:WROTE_REVIEW]->()-[:EVALUATES]->(post)
WITH distinct post
MATCH (review)-[:EVALUATES]->(post)
WITH post, count(review) as reviews
MATCH (reviewer)-[:WROTE_REVIEW]->(review)-[:EVALUATES]->(post)
WITH distinct post, reviewer, review, reviews
ORDER BY review.CreationTime, post.CreationTime DESC
RETURN post.Id AS postId, post.Image AS postImage, post.Description AS postDescription, reviews AS Reviews, count(distinct reviewer) AS Reviewers, collect(distinct reviewer.Avatar)[0..5] AS Avatars
于 2013-10-22T13:09:53.453 回答