Neo4J - 链接列表:无订单返回结果

时间:2014-06-29 18:35:54

标签: neo4j

我正在使用Neo4j服务器,我必须解决这个用例:

我有一个评论的链接列表,我希望得到他们作者的最新评论以及喜欢他们的作者的姓名,所以我的链接列表如下:

(Resource)-[:COMMENTS]->(Comment)-[:NEXT]->(Comment)-...-[:NEXT]->(Comment)

(User)-[:LIKES]->(Comment)

(User)-[:MAKES]->(Comment)

我希望Neo4j为每个评论检索:

{text: comment_text,
 date: comment_date,
 author: comment_author,
 likes: [users_who_like_the_comment] }

我提出了这个问题:

MATCH (r)-[:COMMENTS|NEXT*0..3]->(cc:Comment)<-[:LIKES]-(a:User)
WHERE id(r)=6468
WITH r,a, collect(cc) AS Likes
MATCH r-[:COMMENTS|NEXT*0..3]->(c:Comment), (u:User)-[:MAKES]->c
WITH Likes,c,u,a
RETURN {text: c.text, date: c.date, author: u.name, likes: CASE WHEN (c) in Likes THEN collect(a.name) END } as Comments

问题是我得到了正确的结果,但是没有顺序,默认情况下应该在链表中提供,所以我想我做错了但是我无法弄明白... < / p>

1 个答案:

答案 0 :(得分:1)

这个怎么样:

MATCH (r)-[:COMMENTS]->()-[NEXT*0..3]->(c:Comment)<-[:MAKES]-(u:User)
WHERE id(r)=6468
OPTIONAL MATCH (c)<-[:LIKES]-(a:User)
RETURN {text: c.text, date: c.date, author: u.name, likes: collect(a.name)} as Comments

如果没有保留订单,您可以将其更改为:

MATCH path=(r)-[:COMMENTS]->()-[*NEXT*0..3]->(c:Comment)<-[:MAKES]-(u:User)
WHERE id(r)=6468
OPTIONAL MATCH (c)<-[:LIKES]-(a:User)
WITH {text: c.text, date: c.date, author: u.name, likes: collect(a.name)} as Comments, 
     length(path) as len
ORDER BY len ASC
RETURN Comments

对于好的衡量,这里的所有内容都是GraphGist