Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.1k views
in Technique[技术] by (71.8m points)

neo4j - Unwind with Multiple OPTIONAL MATCH returns duplicates

My graph has the following entities:

  • Post
  • User

And has the following relationship examples

  • User-[:POSTED]->Post
  • User-[:LIKE|:DISLIKE]->Post
  • User-[:POSTED]->Post-[:REPOST_OF]->Post
  • User-[:POSTED]->Post[:REPLY_TO]->Post
  • User-[:FRIEND_OF]-User

OBJECTIVE

  • Get a Post by their ID property
  • Get the counts of LIKE, DISLIKE, REPOSTS, and REPLIES to this Post
  • Get my reaction to this post, [Dislike | Like]
  • Find which of my friends LIKE or DISLIKE this Post
  • Find whatever replies, or reposts by Friends have made to this post
  • Get the counts of LIKE, DISLIKE, REPOSTS, and REPLIES to each reply by my friend to this post
  • Get my reaction to the each of my friend's reposts and replies

I've been able to do all of this except the last two objectives.

Excluding the last two, This is what I did.

  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, postData, poster, myReaction, upvotes, count(downvote) AS downvotes
  
  OPTIONAL MATCH (:Post)-[repost:REPOST_OF]->(post)
  WITH DISTINCT post, postData, poster, myReaction, upvotes, downvotes, count(repost) AS reposts
  
  OPTIONAL MATCH (:Post)-[reply: REPLY_TO]->(post)
  WITH DISTINCT post, postData, poster, myReaction, upvotes, downvotes, reposts, count(reply) AS replies
  
  OPTIONAL MATCH (me)-[:COLLEAGUE_OF]-(colleague: User)
  WITH DISTINCT COLLECT(colleague) AS colleagues, post, poster, postData, upvotes, downvotes, myReaction, reposts, 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,
 post, postData, upvotes, downvotes, replies, reposts, myReaction, poster
 
  RETURN DISTINCT post, myReaction, poster, postData, upvotes, downvotes, replies, reposts, cInteractions, cReactions

This works perfectly fine.

I attempted the latter objectives, by unwinding the cInteractions and doing OPTIONAL MATCHes for the values and it keeps duplicating the result.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...