cancel
Showing results for 
Search instead for 
Did you mean: 

Join the community at Nodes 2022, our free virtual event on November 16 - 17.

Best way to rank order and display posts based on connections/friends using NEO4J

vader_219k
Node Link

I am working on an app which uses NEO4J. There are mainly two types of Nodes

  1. User - User nodes can be connected among each other with relationship 'FRIENDS'
  2. 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,

  1. Consider there are 3 users A,B,C and 4 posts B1,B2 (from user B) and C1,C2 (from user C)
  2. A and B are connected with 'FRIENDS'
  3. 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

1 ACCEPTED SOLUTION

Cobra
Ninja
Ninja

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

View solution in original post

4 REPLIES 4

Cobra
Ninja
Ninja

Hello @vader.219k

This query should solve your use case?

MATCH (a:User)-[:FRIENDS_WITH]-(:User)-[:OWNED_BY]-(p:Post)
WHERE a.name = 'A'
RETURN p

Regards,
Cobra

Hi Cobra,

Thank you so much for your response.

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.

Cobra
Ninja
Ninja

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

Hi Cobra,

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