Reputation: 135
I am developing a small social network applications and i have the following nodes and relationships in my neo4j graph db:
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?
Upvotes: 1
Views: 67
Reputation: 11735
Aside from the lack of direction on the relationships, it looks fine.
The lack of direction is of course slightly less self-explanatory (even if there's an implicit direction, as the User
isn't POSTED_BY
the Post
), but it also changes the semantics of the query: because the SHARED_POST
relationship can be traversed in any direction, of course you'll get the original post in case of a share, but you'll also get all shares in case it is an original post. That's not what you said you wanted.
Upvotes: 1