Neo4j Cypher查询:订单集合,取前n个元素 [英] Neo4j Cypher query: order collection, take first n elements
问题描述
我无法为此社交网络类型的应用程序编写Cypher查询.它涉及到用户添加帖子(基本上是带有说明的图片),用户可以查看这些帖子.
I'm having trouble writing a Cypher query for this social networking type of application. It involves users that add posts (essentially an image with a description), which users can review.
在Cypher中,图形模型是这样的:
(user)-[:WROTE_REVIEW]->(review)-[:EVALUATES]->(post)
In Cypher the graph model is this:
(user)-[:WROTE_REVIEW]->(review)-[:EVALUATES]->(post)
我要编写的查询应返回特定用户已审阅的所有帖子,以及有关帖子评论的一些常规信息.
The query I'm trying to write should return all the posts that a particular user has reviewed, along with some general information about the reviews of a post.
这包括:
- (帖子的ID)
- 帖子的图片
- 帖子的描述
- 此帖子的评论总数
- 此帖子的审阅者总数
- 此帖子的最后6位评论者的头像,按评论日期排序,降序
- (the post's ID)
- the post's image
- the post's description
- the total number of reviews of this post
- the total number of reviewers of this post
- the avatars of the last 6 reviewers of this post, ordered by the date of the review, descending
我认为我已经完成了前五个项目,但是第6个项目给我带来了麻烦.下面的查询为我提供了所有的化身,而我只需要最后的6个.
I think I've managed to complete the first five items, but item 6 is giving me trouble. The query below gives me all of the avatars, while I only need the last 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
)降序排列化身,并获取前六个项目吗?
Can someone show me how to order the avatars by review date (i.e. review.CreationTime
) descending, and take the first six items?
谢谢!
推荐答案
Neo4j 2.0中的集合片是关键.由于出现了一些重复项,因此我必须在结果中放入一些distinct
子句.这就是我最终得到的:
The collection slices in Neo4j 2.0 were the key. I had to put in some distinct
clauses in my results since some duplicates were coming up. This is what I ended up with:
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
这篇关于Neo4j Cypher查询:订单集合,取前n个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!