Need help to structure nodes and build cypher query


(Abhishinde01) #1

My node structure is like this -
User -> FOLLOWS -> User
User -> FOLLOWS_TEAM -> Team

By using following code I can get Messages POSTED by User and Team I am following.

MATCH (me:User)-[u_rel_source:FOLLOWS_TEAM|:FOLLOWS*0..1]->(users)-[u_rel:POSTED]->(msg:Message)

Question -
Now I want to add one more node :Organisation above Team like
and User can follow Organisation, in that case User should get messages posted by Teams of followed Organisation.
How can I do it in one query and how relationships will be?

I am not able to find logic for same.
Thanks in advance

(Andrew Bowman) #2

What data do you want to return? Just the messages, or do you need the relationships returned too? Are you interested only in the graph result view or do you have a specific tabular output as a requirement?

(Abhishinde01) #3

I want to return relationships and messages both in graph result view.

(Andrew Bowman) #4

Give this a try:

MATCH (me:User)-[:FOLLOWS_TEAM|:FOLLOWS*0..1]->(userOrTeamOrOrg)-[:HAS*0..1]->(userOrTeam)-[:POSTED]->(msg:Message)

This way, if the thing being followed is an :Organization, then there's a :HAS relationship to traverse to get to the :Team. Otherwise, if the node is a :Team or :User there will be no outgoing :HAS relationship.


Fixed direction of :HAS relationship.

(Abhishinde01) #5

I tried same,
Its not working.
Here its not checking if me following org.

(Andrew Bowman) #6

Ah, you didn't mention you were using a separate relationship type for that. Add that in, see if it works.

(Abhishinde01) #7

yes I tried that as well.
Like this -

MATCH (me:User)-[post_rel_source:FOLLOWS_TEAM|:FOLLOWS*0..1]->(u)<-[:EVENTS_OF_ORG*0..1]-(users)<-[f:EVENTS_OF_ORG]-(me)

its not showing result of followed teams and users also.

(Andrew Bowman) #8

Again, you never mentioned this relationship. Can you provide the relationships in use around the :Organization node and how they're used?

I had assumed you use something like


Which would mean we have to traverse the following paths:


So the patterns to traverse would be as previously posted (with a quick fix to the direction of :HAS):

MATCH (me:User)-[:FOLLOWS_TEAM|FOLLOWS_ORG|FOLLOWS*0..1]->(userOrTeamOrOrg)-[:HAS*0..1]->(userOrTeam)-[:POSTED]->(msg:Message)