I am working on an app which uses NEO4J. There are mainly two types of Nodes
User - User nodes can be connected among each other with relationship 'FRIENDS'
Post - Posts nodes consists of posts created by the users and has a relationship of 'OWNED_BY' between user who created the post and the actual post node
For example,
Consider there are 3 users A,B,C and 4 posts B1,B2 (from user B) and C1,C2 (from user C)
A and B are connected with 'FRIENDS'
For user A the post display order should be B1,B2,C1,C2 on his/her dashboard ( i.e. we should display the posts which are created by the immediate network followed by posts which are not from his/her network)
Need some guidance on how this can be achieved using CYPHER query
What I'm trying to do
MATCH(n:User)-[r:FRIENDS_WITH]-(u1:User) where n.name = 'A' with u1 match(s:Post)-[r1:OWNED_BY]-(u1) UNION (Trying to figure out how to select Post which are not in s ) I'm trying to address based on my SQL knowledge. I'm getting posts of all users who are FRIENDS_WITH 'A' and after this I want to do a UNION of missing posts, but unable to figure this out
Please do let me know if there's a better way of achieving the outcome. Thank you so much
Unfortunately this doesn't solve my problem because the above only gives me all the posts which are created/owned by a particular user friend's circle.
What I am looking for is to have a list of all posts available in the DB , I want to display my friends posts first followed by others.
Like this, you should get your friend posts first then posts of other users
MATCH (a:User)-[:FRIENDS_WITH]-(b:User)-[:OWNED_BY]-(p:Post)
WHERE a.name = 'A'
WITH collect(p) AS posts, collect(b) + a AS users
MATCH (u:User)-[:OWNED_BY]-(p:Post)
WHERE u NOT IN users
RETURN posts + collect(p) AS posts
Thank you so much for helping me out. Just a minor syntax error in the above and also modified query to include user 'A' posts as well. Also did an UNWIND to get back the original rows
MATCH (a:User)-[:FRIENDS_WITH]-(b:User)-[:OWNED_BY]-(p:Post)
WHERE a.name = 'A'
WITH collect(p) AS posts, collect(b) AS users
MATCH (u:User)-[:OWNED_BY]-(p:Post)
WHERE NOT u IN users
WITH posts + collect(p) AS allposts
unwind allposts as post
return post