Amit Sharma
Amit Sharma

Reputation: 2487

Get followers' post along with own posts in neo4j

I am using neo4j for my social projects where User can create a post, and can follow each other. I have to fetch my follower' post along with my own post. To fetch my followers' post I am using the following query:

MATCH (me:User {UserId: '39434e4d-d501-4fba-8aae-16cf652deb3e'})-[:FOLLOWSS|CREATED]->(f:User)-(p:Posts) RETURN p LIMIT 25 

above query returns my followers' post but I am unable to get my own post. Can someone tell me what I am missing to get my own posts along with my followers' post

MATCH (me:User {UserId: '39434e4d-d501-4fba-8aae-16cf652deb3e'})-[:FOLLOWSS]->(f:User)-[fc:CREATED|LIKED]-(p:Posts)<-[:CREATED]-(n) RETURN p LIMIT 25

I tried above query but it doesn't return any records.

Any help would be highly appreciated.

Thanks.

Upvotes: 1

Views: 204

Answers (3)

Tezra
Tezra

Reputation: 8833

MATCH (me:User {UserId: '39434e4d-d501-4fba-8aae-16cf652deb3e'})-[:FOLLOWSS]->(f:User)-[fc:CREATED]-(p:Posts)<-[:CREATED]-(n) RETURN p LIMIT 25

is returning every post created by you AND someone else. You need to separate variables that are different items

MATCH (mine:Posts)<-[:CREATED]-(me:User {UserId: '39434e4d-d501-4fba-8aae-16cf652deb3e'})-[:FOLLOWSS]->(f:User)-[fc:CREATED]-(p:Posts) RETURN mine, p LIMIT 25

But, this returns info in a vary redundant way (Every your post against every their post), so I would recommend compressing it with COLLECT like this.

MATCH (mine:Posts)<-[:CREATED]-(me:User {UserId: '39434e4d-d501-4fba-8aae-16cf652deb3e'})-[:FOLLOWSS]->(f:User)-[fc:CREATED]-(p:Posts)
WITH mine, p LIMIT 25
WITH COLLECT(mine)+COLLECT(p) as posts
UNWIND posts as p
RETURN p LIMIT 25

Upvotes: 0

Christophe Willemsen
Christophe Willemsen

Reputation: 20185

Building up on @InverseFalcon's answer, here is a query that returns data in embedded collections :

MATCH (n:User {name:"ikwattro"})<-[:FOLLOWS*0..1]-(x)-[:CREATED]->(p:Post)
RETURN n, 
[x IN collect(p) WHERE (n)-[:CREATED]->(x) | x.title] AS userPosts, 
[x IN collect(p) WHERE NOT (n)-[:CREATED]->(x) | x.title] AS followersPosts


╒═══════════════════╤═══════════╤═════════════════════════╕
│"n"                │"userPosts"│"followersPosts"         │
╞═══════════════════╪═══════════╪═════════════════════════╡
│{"name":"ikwattro"}│["Post1"]  │["Post4","Post3","Post2"]│
└───────────────────┴───────────┴─────────────────────────┘

Upvotes: 1

InverseFalcon
InverseFalcon

Reputation: 30407

The easiest way to do this is with an optional variable-length relationship, which can be achieved by using a lower bound of 0:

MATCH (me:User {UserId: '39434e4d-d501-4fba-8aae-16cf652deb3e'})-[:FOLLOWSS*0..1]->(f:User)-[:CREATED]-(p:Posts) 
RETURN p LIMIT 25 

The [:FOLLOWSS*0..1] is the key, as this means that it will include patterns that have the :FOLLOWSS relationship to another :User node, as well as patterns where no relationship is traversed (meaning your me user is the same node as the f :User node).

Upvotes: 1

Related Questions