我在为这种社交网络类型的应用程序编写 Cypher 查询时遇到问题。它涉及用户添加帖子(基本上是带有描述的图像),用户可以查看这些帖子。
在 Cypher 中,图形模型是这样的:(user)-[:WROTE_REVIEW]->(review)-[:EVALUATES]->(post)
我尝试编写的查询应返回特定用户已评论的所有帖子,以及有关帖子评论的一些一般信息。
这包括:
我想我已经设法完成了前五个项目,但是项目 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
)降序排列头像,并取前六项?谢谢!
最佳答案
您可以先对行进行排序,然后选取审阅者集合中的前 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 版本。
关于Neo4j Cypher 查询 : order collection, 取前 n 个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19422779/