我有这样的查询:
MATCH (a)-[:Shout]->(b)
WHERE a.user_id=1
WITH b.post_id as b_post_id, a, b.post as b_post
MATCH (a)-[:Friend]->(c)-[:Shout]->(d)
WITH d.post_id as d_post_id, b_post_id, d.post as d_post, b_post
order by d_post_id desc, b_post_id desc
RETURN collect(distinct d_post) + collect(distinct b_post) as p
我想返回来自用户和朋友的所有帖子,因此我将2匹配结合起来
第一个匹配(a)-[:Shout]->(b)
将返回来自用户的帖子,第二个匹配(a)-[:Friend]->(c)-[:Shout]->(d)
将返回来自朋友的帖子。
(a)-[:Shout]->(b)
的帖子是
post_id: 5, post: nana
post_id: 2, post: hi
(a)-[:Friend]->(c)-[:Shout]->(d)
的帖子是
post_id: 6, post: lala
post_id: 4, post: hello
post_id: 3, post: hanson
所以,当我RETURN collect(distinct d_post) + collect(distinct b_post) as p
时,它会
P: nana, hi, lala, hello, hanson
应该是:lala, nana, hello, hanson, hi
或6,5,4,3,2
请帮帮我。 感谢。
答案 0 :(得分:1)
像
这样的东西MATCH (a {user_id:1})-[:Friend*0..1]->()-[:Shout]->(b)
WITH b.post_id as post_id, b.post as post ORDER BY post_id desc
RETURN collect(post) as posts
修改强>
根据评论,如果您想要返回-[:Shout]->(b)
节点的ID,您可以为可选的朋友位置添加标识符
MATCH (a {user_id:1})-[:Friend*0..1]->(friendOrUser)-[:Shout]->(b)
WITH friendOrUser, b ORDER BY b.post_id desc
RETURN friendOrUser.user_id, b.post
此处friendOrUser
绑定用户a
和他的朋友。
答案 1 :(得分:0)
请尝试以下操作。查询不是很干净但会完成这项工作。
MATCH (a)-[:Shout]->(b)
WHERE a.user_id=1
WITH b,a
MATCH (a)-[:Friend]->(c)-[:Shout]->(d)
WITH b,d
MATCH (dummy) WHERE (dummy = b OR dummy = d)
WITH dummy ORDER BY dummy.post_id DESC
RETURN collect(distinct dummy.post) AS p