I am developing a small social network applications and i have the following nodes and relationships in my neo4j graph db:
- User nodes
- Post nodes. Post node is connected to User node by POSTED_BY relationship and Post node might connected to another Post node by SHARING relationship in case a post shares another post.
I would like to retrieve all posts posted by a specific user and for each post also get the original post in case it is a share (and the publisher of the original post).
I managed to get the required info using optional match but i am new to neo4j and not sure if this is the correct way to go:
match (p:Post)-[r: POSTED_BY]-(publisher:User)
where publisher.userId = {userId}
optional match (p:Post)-[r2: SHARED_POST]-(sharedPost:Post)-[r3: POSTED_BY]-(sharedPostPublisher: User)
return p as post,publisher, sharedPost, sharedPostPublisher
Is this the correct way to retrieve this info or should i use other methods?