Unwind with Multiple OPTIONAL MATCH 返回重复项

问题描述

我的图表具有以下实体:

并且有以下关系的例子

目标

  • 通过他们的 ID 属性获取帖子
  • 获取此帖子的 LIKE、disLIKE、REPOSTS 和 REPLIES 计数
  • 了解我对这篇文章的反应,[不喜欢 |喜欢]
  • 找出我的哪些朋友喜欢或不喜欢此帖子
  • 查找朋友对此帖子所做的任何回复或转发
  • 获取我的朋友对该帖子的每个回复的“喜欢”、“不喜欢”、“回复”和“回复”的计数
  • 了解我对我朋友的每个转帖和回复的反应

除了最后两个目标之外,我已经能够完成所有这些。

除了最后两个,这就是我所做的。

  MATCH (me: User {id: "17450808-0f08-771c-b1cb-9e2480869625"})
  
  MATCH (post: Post {id: "17452c1e-37c2-21a0-df56-69be1b63418a"})
  
  MATCH (post)<-[postData:POSTED]-(poster: User)
  
  OPTIONAL MATCH (me)-[myReaction:UPVote|:DOWNVote]->(post)

  OPTIONAL MATCH (:User)-[upVote:UPVote]->(post)
  WITH disTINCT post,postData,poster,myReaction,count(upVote) AS upVotes
  
  OPTIONAL MATCH (:User)-[downVote:DOWNVote]->(post)
  WITH disTINCT post,upVotes,count(downVote) AS downVotes
  
  OPTIONAL MATCH (:Post)-[repost:REPOST_OF]->(post)
  WITH disTINCT post,downVotes,count(repost) AS reposts
  
  OPTIONAL MATCH (:Post)-[reply: REPLY_TO]->(post)
  WITH disTINCT post,reposts,count(reply) AS replies
  
  OPTIONAL MATCH (me)-[:COLLEAGUE_OF]-(colleague: User)
  WITH disTINCT COLLECT(colleague) AS colleagues,post,replies
 
  UNWIND colleagues AS colleague
 
  OPTIONAL MATCH (colleague)-[:POSTED]->(colleagueInteraction:Post)-
  [colleagueInteractionRelation:REPLY_TO|:REPOST_OF]->(post)
 
  OPTIONAL MATCH (colleague)-[colleagueReaction:UPVote|:DOWNVote]->(post)
  WITH  COLLECT(disTINCT colleagueInteraction) AS cInteractions,COLLECT(disTINCT colleagueReaction) AS cReactions,replies,poster
 
  RETURN disTINCT post,cInteractions,cReactions

这工作得很好。

我尝试了后一个目标,通过展开 cInteractions 并对值进行 OPTIONAL MATCHes 并不断复制结果。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)