1
votes

here is my cypher query :

MATCH (a : User), (user : User {username : 'lebron'}) 
WHERE a.phoneNumber IN ['757488374748','+9035115','+90390320303'] 
  AND a.username <> 'lebron' 
OPTIONAL MATCH (user)-[f:FOLLOWS]->(a), (a)-[p : POSTS]->(b) 
RETURN DISTINCT COLLECT(b.postId) AS postId, a.username AS membername, 
  f.followRequestStatus AS followRequestStatus, user.private AS userPrivate;

'a' node is user node, 'a' can posts photo, follow other users, like other users photo, pretty much like instagram. What's happening here is that it would give followRequestStatus (f.followRequestStatus) correct only if the relation - [p : POSTS] between nodes a and b > exists. If relation 'p' does not exists, it returns null for relation > 'f' also, even if relation f does exist.

1
Your actual question isn't very clear to me. Are you asking how to implement the correct output of followRequestStatus? And since b is a newly introduced variable (the posts from a) am I correct in that what this comes down to is outputting followRequestStatus only if a has posted something (which could be detected by the collection of postIds not being empty)?InverseFalcon
followRequestStatus has to be irrespective of whether a has posted anything or not, it can be null, 0 or 1 but in this case for all conditions where its not able to match (a)-[p : POSTS]->(b), followRequestStatus comes out as null, even if it has some value.Rishik Rohan
Okay, got it. I had misunderstood your description as your requirements rather than the problem.InverseFalcon
what am i doing wrong? @InverseFalconRishik Rohan
@InverseFalcon : it worked! Thanks a lot.Rishik Rohan

1 Answers

3
votes

Have you tried separating your two OPTIONAL MATCH patterns into two separate OPTIONAL MATCHes?

...
OPTIONAL MATCH (user)-[f:FOLLOWS]->(a)
OPTIONAL MATCH (a)-[p : POSTS]->(b) 
...

In the OPTIONAL MATCH description from the developer documentation:

The difference [compared to MATCH] is that if no matches are found, OPTIONAL MATCH will use a null for missing parts of the pattern.

When you use two separate patterns separated by a comma, my assumption is that if both matches are not found, null will be used for all parts of the pattern. Since you want to :FOLLOWS relationships even if there are no :POSTS from a, you need separate OPTIONAL MATCHes.