graph databases - neo4j match nodes and relationships if exists -


i developing small social network applications , have following nodes , relationships in neo4j graph db:

  1. user nodes
  2. post nodes. post node connected user node posted_by relationship , post node might connected post node sharing relationship in case post shares post.

i retrieve posts posted specific user , each post original post in case share (and publisher of original post).

i managed required info using optional match new neo4j , not sure if correct way go:

match (p:post)-[r: posted_by]-(publisher:user) publisher.userid = {userid} optional match (p:post)-[r2: shared_post]-(sharedpost:post)-[r3: posted_by]-(sharedpostpublisher: user) return p post,publisher, sharedpost, sharedpostpublisher 

is correct way retrieve info or should use other methods?

aside lack of direction on relationships, looks fine.

the lack of direction of course less self-explanatory (even if there's implicit direction, user isn't posted_by post), changes semantics of query: because shared_post relationship can traversed in direction, of course you'll original post in case of share, you'll shares in case is original post. that's not said wanted.


Comments